我正在使用共享首选项,但我的代码不起作用。我不知道它有什么问题。我执行错了吗?
我的主要 java 活动(相关代码)是:
public class MainActivity extends AppCompatActivity {
private String s;
public static final String subjectKey = "SubjectID";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final SharedPreferences sharedPreferences = getSharedPreferences(subjectKey, Context.MODE_PRIVATE);
TextView calc_monday = (TextView) findViewById(R.id.monday_calc);
calc_monday.setOnClickListener(
new Button.OnClickListener(){
public void onClick(View v){
CustomDialogClass cdd = new CustomDialogClass(MainActivity.this);
cdd.show();
TextView text1 = (TextView) cdd.findViewById(R.id.Subject_ID);
String text = sharedPreferences.getString(subjectKey, " ");
if(text != " ")
{
text1.setText(text); /* Edit the value here*/
}
TextView text2 = (TextView) cdd.findViewById(R.id.Room_ID);
text2.setText("6 (SEECS)");
TextView text3 = (TextView) cdd.findViewById(R.id.Time_ID);
text3.setText("09:00am - 09:50am");
}
}
);
calc_monday.setOnLongClickListener(
new Button.OnLongClickListener() {
public boolean onLongClick(View v) {
SettingDialogClass SDC = new SettingDialogClass(MainActivity.this);
SDC.show();
EditText texii = (EditText) SDC.findViewById(R.id.set_Subject_ID);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(subjectKey, texii.getText().toString());
editor.apply();
return true;
}
}
);
基本上我希望当我长按一个文本框(calc_monday)时,应该会出现一个对话框。现在我应该可以在此对话框中出现的 EditText 字段上写一些东西了。无论我写什么都应该被存储,然后在我单击同一个文本框时显示(calc_monday)
请看 2 种方法:onClick 和 onLongClick 来了解。
代码不起作用,即当我单击文本框时,我在 onLongCLick 对话框的 EditText 字段上写的文本没有显示。
代码有什么问题