更改 TextView 并保存在 SharedPreferences

Change TextView and save on SharedPreferences

我有一个包含文本的 TextView,我想在单击按钮时更改它并将其保存在 sharedpreferences 上,直到应用程序重新启动然后它恢复为默认文本。

这是我的代码:

TextView questionText = (TextView)findViewById(R.id.perso);
questionText.setText("Default text");
Button button = (Button) findViewById(R.id.button1);

button.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            resetConsent();
            questionText.setText("You clicked on the button");


        }
    });

创建一个 class 扩展应用程序 class,这将维持价值直到您的应用程序打开,并且还可以在您的应用程序中的不同活动中访问。如果您确实需要使用它,您可以使用 sharedPreferences 扩展相同的概念。

更多关于 androids Application class here

public class Mydata extends Application {
    private static Mydata d;
    private static String textVal;
    public Mydata getInstance(){
        return d;
    }
    @Override
    public void onCreate() {
        super.onCreate();
        d= this;
    }
}

单击按钮将数据保存在单个 class。

        public void onClick(View v) {
            resetConsent();
            questionText.setText("You clicked on the button");
            Mydata.textVal=questionText.getText().toString();
        }
    });

在 onCreate() 方法中将 TextView 中的值设置为:

  questionText.setText(Mydata.textVal);

您还可以查看新的 LifeCycle android 库:https://developer.android.com/topic/libraries/architecture/lifecycle

只需使用 SharePreferences 文档中的代码 here 您可以在 link 中看到用于读取和写入的代码片段,而不是使用 putInt 和 getInt,而是使用 putString,而 getString 您需要一个用于识别您刚刚保存的值的键。

这是示例代码,

    public class MainActivity extends AppCompatActivity {

    private static final String TAG =MainActivity.class.getSimpleName() ;
    private  TextView questionText ;
    private   Button button ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        Log.v(TAG,"onCreate");
        setContentView(R.layout.activity_main);
        button = findViewById(R.id.button);
        questionText = findViewById(R.id.perso);
        questionText.setText("Default text");
        //Setting curent text into the shared preferance
        setSharedpreferance("ButtonText1",questionText.getText().toString());
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                questionText.setText("You clicked on the button");
                setSharedpreferance("ButtonText1",questionText.getText().toString());
                Log.v(TAG,"Sharedpref value"+getSharedpreferance("ButtonText1"));
            }
        });
    }


    @Override
    protected void onResume() {

        super.onResume();
        Log.v(TAG,"onResume");
        questionText.setText(getSharedpreferance("ButtonText1"));
    }

    private void setSharedpreferance(String key, String value)
    {
        SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.putString(key, value);  // Saving string
        // Save the changes in SharedPreferences
        editor.apply();
    }

    private  String getSharedpreferance(String key){
        //To get the sharedpreferance value
        SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
        return pref.getString(key, null);
    }
}

您应该需要查看 android activity 生命周期 当一个新的 activity 创建时,如果 OnResume() 在 Activity class 中可用,它将调用以下内容。 Oncreate->OnResume