SharedPreferences 不是来自 EditText 字段的 working/saving 数据
SharedPreferences not working/saving data from EditText field
我正在使用共享首选项,但我的代码无法正常工作。我不知道它有什么问题。我是不是执行错了什么?
我的主要 java activity(相关代码)是:
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 字段上写的文本没有显示。
代码有什么问题
我认为应该是:
String text = sharedPreferences.getString("Name", " ");
尝试使用这些方法来保存和加载带有首选项的字符串:
//save prefs
public void savePrefs(String key, String value){
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
}
//get prefs
public String loadPrefs(String key, String value){
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String data = sharedPreferences.getString(key, value);
return data;
}
你可以这样调用加载方法:
subjectKey = loadPrefs("YouCanNameThisAnything", subjectKey);
你可以这样调用保存方法:
savePrefs("YouCanNameThisAnything", subjectKey);
有关如何使用共享首选项的更多详细信息也可在此处的文档中找到:
http://developer.android.com/reference/android/content/SharedPreferences.html
//create and initialize the intance of shared preference
SharedPreferences sharedPreferences = getSharedPreferences("Session", MODE_PRIVATE);
//save a string
SharedPreferences.Editor edit = sharedPreferences.edit();
edit.putString(subjectKey , texii.getText().toString());
edit.commit();
//retrieve the string
String subject = sharedPreferences.getString(subjectKey, "");
当您长按 calc_monday 时,它只会显示带有 empty EditText 值的自定义对话框。要在 EditText 中输入时保存文本,请在自定义对话框中创建一个按钮,并为此按钮调用 onClickListener 操作,然后将值保存到 SharePreferences。
calc_monday.setOnLongClickListener(
new Button.OnLongClickListener() {
public boolean onLongClick(View v) {
SettingDialogClass SDC = new SettingDialogClass(MainActivity.this);
EditText texii = (EditText) SDC.findViewById(R.id.set_Subject_ID);
Button btnSave = (Button)SDC.findViewById(R.id.your_custom_button);
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(subjectKey, texii.getText().toString());
editor.apply();
SDC.dismiss();
}
});
SDC.show();
return true;
}
}
);
我正在使用共享首选项,但我的代码无法正常工作。我不知道它有什么问题。我是不是执行错了什么?
我的主要 java activity(相关代码)是:
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 字段上写的文本没有显示。
代码有什么问题
我认为应该是:
String text = sharedPreferences.getString("Name", " ");
尝试使用这些方法来保存和加载带有首选项的字符串:
//save prefs
public void savePrefs(String key, String value){
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
}
//get prefs
public String loadPrefs(String key, String value){
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String data = sharedPreferences.getString(key, value);
return data;
}
你可以这样调用加载方法:
subjectKey = loadPrefs("YouCanNameThisAnything", subjectKey);
你可以这样调用保存方法:
savePrefs("YouCanNameThisAnything", subjectKey);
有关如何使用共享首选项的更多详细信息也可在此处的文档中找到: http://developer.android.com/reference/android/content/SharedPreferences.html
//create and initialize the intance of shared preference
SharedPreferences sharedPreferences = getSharedPreferences("Session", MODE_PRIVATE);
//save a string
SharedPreferences.Editor edit = sharedPreferences.edit();
edit.putString(subjectKey , texii.getText().toString());
edit.commit();
//retrieve the string
String subject = sharedPreferences.getString(subjectKey, "");
当您长按 calc_monday 时,它只会显示带有 empty EditText 值的自定义对话框。要在 EditText 中输入时保存文本,请在自定义对话框中创建一个按钮,并为此按钮调用 onClickListener 操作,然后将值保存到 SharePreferences。
calc_monday.setOnLongClickListener(
new Button.OnLongClickListener() {
public boolean onLongClick(View v) {
SettingDialogClass SDC = new SettingDialogClass(MainActivity.this);
EditText texii = (EditText) SDC.findViewById(R.id.set_Subject_ID);
Button btnSave = (Button)SDC.findViewById(R.id.your_custom_button);
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(subjectKey, texii.getText().toString());
editor.apply();
SDC.dismiss();
}
});
SDC.show();
return true;
}
}
);