嗨我正在尝试将EditText小部件值保存在手机/平板电脑的内部存储器中,以便应用程序在关闭或活动停止时自动检索它们。所有这一切都可以使用保存按钮。(小部件的值由用户输入)。
公共类MainActivity扩展了AppCompatActivity {
EditText pH;
Button b1;
public static final String MyPREFERENCES = "MyPrefs" ;
public static final String PrimeraHora = "nameKey";
SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText pH = (EditText) findViewById(R.id.primeraHora);
Button b1=(Button)findViewById(R.id.guardarCambios1);
sharedPreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String ph = pH.getText().toString();
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(PrimeraHora, ph);
editor.commit();
editor.apply();
Toast.makeText(MainActivity.this,"Thanks",Toast.LENGTH_LONG).show();
}
});
}
}