我目前正在從Button1 ... button8保存文本的活動中工作。Android - 具有共享首選項值的顯示按鈕
但它不顯示button.i的文本我不明白。
MainActivity =>
public void getName(){
SharedPreferences preferences = getSharedPreferences("sample",0);
int a=(preferences.getInt("num",0));
String ab=(preferences.getString("Name",""));
if(a==0){
button1.setVisibility(View.GONE);
button1.setVisibility(View.VISIBLE);
button1.setText(ab);
}
if(a==1){
button2.setVisibility(View.GONE);
button2.setVisibility(View.VISIBLE);
button2.setText(ab);
}
if(a==3){
button3.setVisibility(View.GONE);
button3.setVisibility(View.VISIBLE);
button3.setText(ab);
}
}
SetupActivity =>
public void onClick(View v) {
SharedPreferences preferences = getSharedPreferences("Sample", 0);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("Name", editText.getText().toString());
editor.putInt("num", myNum);
editor.commit();
Intent intent = new Intent(Setup.this, MainActivity.class);
startActivity(intent);
}
感謝,但它不工作。 :(,它不PRPREMEM – thang
那麼調試你的代碼,並指定你的問題。例如有「你」期望的值?是editText.getText()。toString()不是「」。是否覆蓋首選項? – FlanschiFox