SharedPreferences无效/从EditText字段保存数据

时间:2015-12-28 19:20:08

标签: java android android-edittext sharedpreferences android-sharedpreferences

我正在使用共享首选项,但我的代码无效。我不知道它有什么问题。我实施了什么错误吗?

我的主要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字段上写的文本没有显示。

代码有什么问题

4 个答案:

答案 0 :(得分:3)

当您长按calc_monday时,它只会显示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;
            }
        }
);

答案 1 :(得分:0)

我认为它应该是:

String text = sharedPreferences.getString("Name", " ");

答案 2 :(得分:0)

尝试使用以下方法保存和加载带首选项的字符串:

//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);

你可以像这样调用save方法:

savePrefs("YouCanNameThisAnything", subjectKey);

此处的文档中还提供了有关如何使用共享首选项的更多详细信息: http://developer.android.com/reference/android/content/SharedPreferences.html

答案 3 :(得分:0)

//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, "");