简体   繁体   English

如何将用户输入从EditText保存到要在其他Activity上使用的变量

[英]How to save user input from EditText to a variable to be used on a different Activity

I have an edit text in Android Studio which I want a user to enter a name which can then be stored as a variable. 我在Android Studio中有一个编辑文本,我希望用户输入一个名称,然后可以将其存储为变量。 I then want to add this variable to a textview in my main activity. 然后,我想在我的主要活动中将此变量添加到textview中。

Right now all a have is a blank activity with an edit text and button to save the user input as a variable. 现在所有具有空白的活动是带有编辑文本和按钮的,用于将用户输入保存为变量。

Edit 编辑

Ok, I am going to change my approach. 好的,我将改变我的方法。 In my main activity, I have two text views. 在我的主要活动中,我有两个文本视图。 If I changed them to edit texts, then how would I save them from the main activity without a save button so that what the user typed would still be there? 如果我将它们更改为编辑文本,那么如何在没有保存按钮的情况下将其从主要活动中保存下来,以便用户键入的内容仍然存在?

Save it in Shared Preferences and then retrieve it from the other activity. 将其保存在“ 共享首选项”中 ,然后从其他活动中检索它。

To save a String in shared preferences, you can create a method like the following: 要将String保存在共享首选项中,可以创建如下所示的方法:

public static void setUsername(Context context, String username) {
    SharedPreferences prefs = context.getSharedPreferences("myAppPackage", 0);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString("username", username);
    editor.commit();
 }

To retreive it : 要恢复它:

public static String getUsername(Context context) {
    SharedPreferences prefs = context.getSharedPreferences("myAppPackage", 0);
    return prefs.getString("username", "");

 }

Edited: 编辑:
In the activity which contains the EditText, you can call the method as follows: 在包含EditText的活动中,可以按以下方式调用方法:
setUsername(this,myEditText.getText().toString()); setUsername(this,myEditText.getText()。toString());

And in the Activitiy that contains the TextView: 在包含TextView的Activitiy中:
myTextView.setText( getUsername(this) ); myTextView.setText(getUsername(this));

you ça use edit.getText().toString() tout get the user input. 您可以使用edit.getText().toString()获取用户输入。

To launch new activity and pass the string to it use 要启动新活动并将字符串传递给它,请使用

Intent intent = new Intent(this, OtherActivity.class);
intent.putExtra("key", theString);
startActivity(intent);

EDIT 编辑

To be more easy you should add a button to your layout so when the user press that button you can launch the next activity with the string. 为了更容易,您应该在布局中添加一个按钮,以便当用户按下该按钮时,您可以使用字符串启动下一个活动。 This should go into your activity 这应该进入您的活动

public class BlankActivity implements OnClickListener {

     private EditText mEditText;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.your_layout);

          //find editText
          mEditText = (EditText) findViewById(R.id.your_edit_text_id);
          //listen button
          findViewById(R.id.your_button_id).setOnClickListener(this);

     }

     @Override
     public void onClick(View v) {
          String theString = mEditText.getText().toString();
          Intent intent = new Intent(this, OtherActivity.class);
          intent.putExtra("key", theString);
          startActivity(intent);
     }


}

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

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