java 如何将用户输入从 EditText 保存到要在不同活动上使用的变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31090558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 18:05:45  来源:igfitidea点击:

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

javaandroidxml

提问by sonic

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. I then want to add this variable to a textview in my main activity.

我在 Android Studio 中有一个编辑文本,我希望用户输入一个名称,然后可以将其存储为变量。然后我想将此变量添加到我的主要活动中的 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?

好的,我要改变我的方法。在我的主要活动中,我有两个文本视图。如果我将它们更改为编辑文本,那么如何在没有保存按钮的情况下将它们从主活动中保存,以便用户输入的内容仍然存在?

回答by Rick Sanchez

Save it in Shared Preferencesand then retrieve it from the other activity.

将其保存在共享首选项中,然后从其他活动中检索它。

To save a String in shared preferences, you can create a method like the following:

要在共享首选项中保存字符串,您可以创建一个如下所示的方法:

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:
setUsername(this,myEditText.getText().toString());

已编辑:
在包含 EditText 的活动中,您可以调用方法如下:
setUsername(this,myEditText.getText().toString());

And in the Activitiy that contains the TextView:
myTextView.setText( getUsername(this) );

在包含 TextView 的 Activitiy 中:
myTextView.setText( getUsername(this) );

回答by sonic

you ?a use edit.getText().toString()tout get the user input.

你?使用edit.getText().toString()tout获取用户输入。

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);
     }


}