Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在android中使用共享首选项在editText中保存数据_Java_Android - Fatal编程技术网

Java 如何在android中使用共享首选项在editText中保存数据

Java 如何在android中使用共享首选项在editText中保存数据,java,android,Java,Android,我有两个edittext我想在edittext中保存数据,直到我没有改变它的制作方法。我尝试过这样的事情,但数据无法保存 e1 = (EditText)findViewById(R.id.editText); e2 = (EditText)findViewById(R.id.editText2); b1 = (Button)findViewById(R.id.button3); sharedpreferences = getSharedPreferences(MyPRE

我有两个edittext我想在edittext中保存数据,直到我没有改变它的制作方法。我尝试过这样的事情,但数据无法保存

e1 = (EditText)findViewById(R.id.editText);
    e2 = (EditText)findViewById(R.id.editText2);
    b1 = (Button)findViewById(R.id.button3);

    sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String homewifi  = e1.getText().toString();
            String officewifi  = e2.getText().toString();

            SharedPreferences.Editor editor = sharedpreferences.edit();

            editor.putString(HOMEWIFI, homewifi);
            editor.putString(OFFICEWIFI, officewifi);
            editor.commit();
            Toast.makeText(MainActivity.this,"Thanks",Toast.LENGTH_SHORT).show();

        }
    });
当活动再次打开时,数据不会显示在edittext中

当活动再次打开时,数据不会显示在EditText中

需要从
SharedReferences
获取数据,并调用
EditText的
method
再次启动
活动时显示该数据。如:

....
e2 = (EditText)findViewById(R.id.editText2);
b1 = (Button)findViewById(R.id.button3);
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
if (sharedpreferences.contains(HOMEWIFI)) {
   String strHomeWifi = settings.getString(HOMEWIFI, "default value");
   e1.setText(strHomeWifi); 
}else if (sharedpreferences.contains(OFFICEWIFI)) {
   String strofficewifi = settings.getString(OFFICEWIFI, "default value");
   e2.setText(strofficewifi); 
}
...

您没有从共享引用中检索数据修改如下代码:

 sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);  
     SharedPreferences.Editor editor = sharedpreferences.edit();

    e1 = (EditText)findViewById(R.id.editText);
    e2 = (EditText)findViewById(R.id.editText2);

    String homewifi = sharedpreferences.getString(HOMEWIFI, "");
    String officeWifi = sharedpreferences.getString(OFFICEWIFI, "");

    e1.setText(homewifi);
    e2.setText(officeWifi);


        b1 = (Button)findViewById(R.id.button3);

       Context.MODE_PRIVATE);

        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String homewifi  = e1.getText().toString();
                String officewifi  = e2.getText().toString();



                editor.putString(HOMEWIFI, homewifi);
                editor.putString(OFFICEWIFI, officewifi);
                editor.commit();
                Toast.makeText(MainActivity.this,"Thanks",Toast.LENGTH_SHORT).show();

            }
        });

我猜你忘了将SharedReference中的字符串设置为edittext

将其从编辑文本保存到共享引用后


您正在保存数据,但未检索data@MasoomBacha看到我的回答了吗?我应该把这个代码放在哪里?@MasoomBacha:不,检查我的答案。在codee1.setText(strhomewi)中的
b1.setOnClickListener(new View.OnClickListener(){
行之前执行此操作;下面是str@MasoomBacha:整个代码是
String strHomeWifi=settings.getString(HOMEWIFI,“默认值”);e1.setText(strHomeWifi)
@MasoomBacha:那行有什么问题吗?看这里我有个问题little@MasoomBacha是的,告诉我它是什么?如果(con.getSSID().toString().equalsIgnoreCase(“\”Ranksol\”)这里我使用的是手动字符串值,我想使用的是从edittextString homewifi=e1.getText().toString()获得的字符串值;
 e1 = (EditText)findViewById(R.id.editText);
            e2 = (EditText)findViewById(R.id.editText2);
            b1 = (Button)findViewById(R.id.button3);

            sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

            if(!TextUtils.isEmpty(sharedpreferences.getString(HOMEWIFI))){
                e1.setText(sharedpreferences.getString(HOMEWIFI));
            }
            if(!TextUtils.isEmpty(sharedpreferences.getString(OFFICEWIFI))){
                e1.setText(sharedpreferences.getString(OFFICEWIFI));
            }

            b1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String homewifi  = e1.getText().toString();
                    String officewifi  = e2.getText().toString();

                    SharedPreferences.Editor editor = sharedpreferences.edit();

                    editor.putString(HOMEWIFI, homewifi);
                    editor.putString(OFFICEWIFI, officewifi);
                    editor.commit();
                    Toast.makeText(MainActivity.this,"Thanks",Toast.LENGTH_SHORT).show();

                }
            });