简体   繁体   English

保存Edittext并使其保留在EditText上

[英]Saving Edittext and making it stay on EditText

I have an app where I take the Name of the user in an editText and I have it storing to firebase but when I get out of the activity and go back into it, the editText field does not show the Name of the user anymore. 我有一个应用程序,我在editText中获取用户的名称,我将它存储到firebase但是当我退出活动并返回到它时,editText字段不再显示用户的名称。 I want their name to stay in the editText field. 我希望他们的名字留在editText字段中。 also how would I do the same thing but for an image in an ImageView that the user puts in. Please help. 另外,我将如何做同样的事情,但是对于用户放入的ImageView中的图像。请帮忙。

IDEA: You can use shared preference. IDEA:您可以使用共享首选项。 When user launches his application first task will be load his user name and password from shared preference. 当用户启动他的应用程序时,第一个任务将从共享首选项加载他的用户名和密码。

By doing this you can also manage a session manager for better user experience. 通过这样做,您还可以管理会话管理器以获得更好的用户体验。

For example: After every successful authentication you can store the basic information of the user that is needed to update the screen everytime. 例如:每次成功验证后,您都可以存储每次更新屏幕所需的用户基本信息。

Benefit: This will help you to show old data on screen if user launches your app without internet enabled. 好处:如果用户在没有启用互联网的情况下启动您的应用,这将帮助您在屏幕上显示旧数据。 You can check the internet and if disabled then simply show the old data from preference. 您可以检查互联网,如果禁用,则只需显示偏好的旧数据。

You Can do something like this 你可以做这样的事情

final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

text.addTextChangedListener(new TextWatcher() {
@Override
  public void onTextChanged(CharSequence s, int start, int before,
                            int count)
{
  prefs.edit().putString("autoSave", s.toString()).commit();
}

@Override
  public void beforeTextChanged(CharSequence s, int start, int count,
                                int after)
{
}

@Override
  public void afterTextChanged(Editable s)
{
   //At here you can call some method to get the text from shared preferences and display it on EDitText

}

or You can save into DB at onTextChanged() method 或者您可以在onTextChanged()方法中保存到DB中

Here is a little activity example which saves the username into SharedPreferences at stop of activity and restores the value to the EditText when activity is (re)started: 这是一个小活动示例,它在活动停止时将用户名保存到SharedPreferences中,并在(重新)启动活动时将值恢复到EditText:

public class Test extends Activity {

    EditText edtUser;
    SharedPreferences preferences;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        preferences = PreferenceManager.getDefaultSharedPreferences(this);

        edtUser = (EditText) findViewById(R.id.editTextUser);
        edtUser.setText(preferences.getString("username", ""));



    }

    @Override
    protected void onStop() {
        super.onStop();

        preferences.edit().putString("username", edtUser.getText().toString()).commit();

    }



}

Set on click listener to edit text. 设置单击侦听器以编辑文本。 Inside on click enable edit text and write to firebase using setValue. 在里面单击启用编辑文本并使用setValue写入firebase。

Set on click to parent layout or check is focus changed of edit text. 设置单击到父布局或检查是否编辑文本的焦点更改。 Inside read from firebase using add value event listener. 使用add value事件监听器从firebase读取内部。

In Oncreate disable edit text and read from firebase using add value event listener. 在Oncreate中禁用编辑文本并使用添加值事件侦听器从firebase读取。

This will auto save and retrieve edit text data seamlessly. 这将无缝地自动保存和检索编辑文本数据。

In OnCreate create method of activity 在OnCreate中创建活动方法

//Initialize edittext //初始化edittext

mEditText = (EditText) findViewById(R.id.edittext); mEditText =(EditText)findViewById(R.id.edittext);

// Disable mEditText //禁用mEditText

mEditText.setEnabled(false); mEditText.setEnabled(假);

//Read from firebase and set text to mEditText if mTextEdit is not in focus //如果mTextEdit不在焦点上,则从firebase读取并将文本设置为mEditText

if (!mEditText.isFocused()){ if(!mEditText.isFocused()){

mFirebaseRef.addValueEventListener(new ValueEventListener() { mFirebaseRef.addValueEventListener(new ValueEventListener(){

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String editTextData = (String) dataSnapshot.getValue();
            mEditText.setText(editTextData);
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });

} }

// Set onclick listener to mEditText //将onclick监听器设置为mEditText

mEditText.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            mEditText.setEnabled(true);
            editTextData = mEditText.getText().toString();
            mFirebaseRef.setValue(editTextData);
        }
    }); 

This works for me and UI looks simple without any extra buttons to switch between edit and save. 这适用于我,UI看起来很简单,没有任何额外的按钮可以在编辑和保存之间切换。

Hope I understood your requirement correctly. 希望我能正确理解你的要求。 Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM