代码之家  ›  专栏  ›  技术社区  ›  Fahad Saleem

SharedPreferences不工作/保存EditText字段中的数据

  •  2
  • Fahad Saleem  · 技术社区  · 10 年前

    我正在使用共享首选项,但我的代码无法工作。我不知道它有什么问题。我是不是执行了什么错误?

    我的主要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)上单击时显示出来

    请参阅两种方法:onClick和onLongClick以了解。

    代码不起作用,即当我单击文本框时,在LongCLick对话框的EditText字段中写入的文本不会显示。

    代码有什么问题

    4 回复  |  直到 10 年前
        1
  •  3
  •   Danh DC    10 年前

    当你长按calc_monday时,它只显示你的自定义对话框 空的 EditText的值。要在EditText中输入时保存文本,请在自定义对话框中创建一个按钮,并为此按钮调用ClickListener操作,然后将值保存到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;
                }
            }
    );
    
        2
  •  0
  •   CodeMonster    10 年前

    我认为应该是:

    String text = sharedPreferences.getString("Name", " ");
    
        3
  •  0
  •   joshgoldeneagle    10 年前

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

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

    您可以这样调用load方法:

    subjectKey = loadPrefs("YouCanNameThisAnything", subjectKey);
    

    您可以这样调用save方法:

    savePrefs("YouCanNameThisAnything", subjectKey);
    

    有关如何使用共享首选项的更多详细信息,请参阅此处的文档: http://developer.android.com/reference/android/content/SharedPreferences.html

        4
  •  0
  •   meda    10 年前
    //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, "");