首页 > 解决方案 > 让用户在edittext中输入一个简单的文本,然后在更多页面的textview中显示如何R.id string sharedpreferences

问题描述

我希望有人可以帮助我理解和使用 Android Studio 中的共享首选项字符串。我有一个文本视图(在更多页面上)和编辑文本(在设置中)(和一个按钮)我想让用户在编辑文本中放置一些文本并将信息存储在共享首选项中,以便它可以显示在所有页面上的 textview 部分。我怎样才能让这个工作?提前致谢

标签: javascriptxmlandroid-studiosharedpreferences

解决方案


据我了解,SharedPreferences 只是您创建、编辑和获取值的文件。我将向您展示为您阐明其含义并帮助您实施它的步骤:

public class FirstActivity extends AppCompatActivity {

    Button saveInputBtn;
    EditText userInputET;
    SharedPreferences sharedPrefs;
    String userInput;

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

        saveInputBtn = findViewById(R.id.save_user_input);
        userInputET = findViewById(R.id.user_input);

        // 1) Affect an "onClickListener" to your button, that will inform your app when the user clicks on this button. 
        // The method saveTextAndLaunchSecondActivity() will only be launched if the button is clicked.
        saveInputBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveTextAndLaunchSecondActivity();
            }
        });
    }

    private void saveTextAndLaunchSecondActivity() {
        // 2) Get the input entered by the user inside your EditText
        String userInput = userInputET.getEditableText().toString();

        // 3) Create a file called sharedPrefName to stock values, that is "private" 
        // (so it cannot be accessed by other application than your)
        sharedPrefs = getSharedPreferences("sharedPrefName", Context.MODE_PRIVATE);

        // 4) Create an "Editor", that will allow you to edit the SharedPrerences 
        // file you just create.
        SharedPreferences.Editor editor = sharedPrefs.edit();

        // 5) Use the Editor to put a String value inside the file. 
        // The SharedPreferences use a "key-value" system to store values,
        // below, "KEYNAME" is the key and userInput is the value that we
        // fetched inside the EditText 
        editor.putString("KEYNAME", userInput);
        editor.apply();

        // 6) Create a intent that specify you want to go on another Activity
        // and use the "startActivity" method with the created Intent inside
        Intent intent = new Intent(this, SecondActivity.class);
        startActivity(intent);
    }
}

现在我们已经从 EditText 中提取了值并将其存储在 SharedPreferences 中,再加上启动 SecondActivity,我们需要检索这个 userInput。

在 SecondActivity 你可以使用这个:

public class SecondActivity extends AppCompatActivity {

    SharedPreferences sharedPrefs;
    TextView secondActivity_TV;

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

        secondActivity_TV = findViewById(R.id. TV_second_activity);

        // 7) Get the SharedPreferences file with its key : "sharedPrefName"
        sharedPrefs = getSharedPreferences("sharedPrefName", Context.MODE_PRIVATE);

        // 8) Get the userInput that you stored inside those SP
        // The key has to match the one you created, the value "hey" is the 
        // default value = the value that the SharedPreferences will give 
        // you if the key does not match
        String userInputReceived = sharedPrefs.getString("KEYNAME","hey");

        // 9) And now you can assign it to your TextView
        secondActivity_TV.setText(userInputReceived);
    }
}

然后,在 FirstActivity 中创建的 SharedPreferences 可以在任何其他活动中使用,只要您创建的键与您用来获取值的键相匹配。我真的希望这会对您有所帮助,如果您有任何疑问,请告诉我们进展如何!


推荐阅读