Android 将editText值存储到另一个活动

Android 将editText值存储到另一个活动,android,arrays,android-intent,sharedpreferences,Android,Arrays,Android Intent,Sharedpreferences,我有三条以上的文字。当我在edittext中输入内容时,我需要使用SharedReferences将其保存到另一个屏幕。在将editText值传递给另一个活动之前,我使用了Intent。但我需要稍后保存editText值以进行编辑 代码: 活动: et=(EditText)findViewById(R.id.et); et1=(EditText)findViewById(R.id.et1); btn=(Button)findViewById(

我有三条以上的文字。当我在edittext中输入内容时,我需要使用SharedReferences将其保存到另一个屏幕。在将editText值传递给另一个活动之前,我使用了Intent。但我需要稍后保存editText值以进行编辑

代码:

活动:

 et=(EditText)findViewById(R.id.et);

            et1=(EditText)findViewById(R.id.et1);

            btn=(Button)findViewById(R.id.btn);

    btn.setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View v) {

SharedPreferences preferences = getSharedPreferences("sample",0);
                  SharedPreferences.Editor editor = preferences.edit();

   String[] day_array = new String[] {et.getText().toString(), et1.getText().toString() };

                  editor.putInt("array_size", day_array.length);
                  for (int i = 0; i < day_array.length; i++)
                      editor.putString("array_" + i, day_array[i]);
                  editor.commit();


                  Intent intent = new Intent(Save.this, Get.class);
                  startActivity(intent);
et=(EditText)findViewById(R.id.et);
et1=(EditText)findViewById(R.id.et1);
btn=(按钮)findViewById(R.id.btn);
btn.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
SharedReferences首选项=GetSharedReferences(“示例”,0);
SharedReferences.Editor=首选项.edit();
String[]day_array=新字符串[]{et.getText().toString(),et1.getText().toString()};
putInt(“数组大小”,天数组长度);
对于(int i=0;i
活动1:

  SharedPreferences preferences = getSharedPreferences("sample",0);

  int size = preferences.getInt("array_size", 0);
      String[] Display_Room_array = new String[size];

      for (int i = 0; i < size; i++) {

          name = preferences.getString("array_" + i, Display_Room_array[i]);
          Display_Room_array[i] = name;

          txt=(TextView)findViewById(R.id.txt);
          txt.setText(Display_Room_array[i]);

          Toast.makeText(getApplicationContext(), name, Toast.LENGTH_SHORT).show();

      }
SharedReferences首选项=GetSharedReferences(“示例”,0);
int size=preferences.getInt(“数组大小”,0);
字符串[]显示\房间\数组=新字符串[大小];
对于(int i=0;i
我没有得到你想要的

您说过要在首选项中保存3个编辑文本值,好吗

您也可以在第一个活动中完成此操作


但是,如果您想在其他活动中保存edittext值,那么您应该使用intent.put extra将值传递给下一个活动,这样您就可以将值存储在共享首选项中!这有什么大不了的?

我没有得到您想要的

您说过要在首选项中保存3个编辑文本值,好吗

您也可以在第一个活动中完成此操作


但是,如果您想在其他活动中保存edittext值,那么您应该使用intent.put extra将值传递给下一个活动,这样您就可以将值存储在共享首选项中!这有什么大不了的?

您可以在
共享首选项中保存一些值,并按如下所示访问后者

et=(EditText)findViewById(R.id.et);

et1=(EditText)findViewById(R.id.et1);

btn=(Button)findViewById(R.id.btn);

btn.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {

        Intent intent = new Intent(Save.this, Get.class);
            String[] myStrings = new String[] {txtt.getText().toString(),et.getText().toString() ,txtt1.getText().toString(),et1.getText().toString() };
                intent.putExtra("strings", myStrings);


                SharedPreferences preferences = getSharedPreferences("default", MODE_PRIVATE);
                  SharedPreferences.Editor editor = preferences.edit();
                  editor.putString("text1",txtt.getText().toString());
                  editor.putString("text2",et.getText().toString());
           .....
           ......   
                  editor.commit();


         startActivity(intent);
在下一个活动中,您可以访问该首选项,如下所示

.......
......

SharedPreferences preferences = getSharedPreferences("default", MODE_PRIVATE);

String text1= preference.getString("text1"," ");
String text2= preference.getString("text2"," ");
....
.....

txt.setText(text1);
txt2.setText(text2);

您可以在
SharedReferences
中保存一些值,并按如下方式访问后者

et=(EditText)findViewById(R.id.et);

et1=(EditText)findViewById(R.id.et1);

btn=(Button)findViewById(R.id.btn);

btn.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {

        Intent intent = new Intent(Save.this, Get.class);
            String[] myStrings = new String[] {txtt.getText().toString(),et.getText().toString() ,txtt1.getText().toString(),et1.getText().toString() };
                intent.putExtra("strings", myStrings);


                SharedPreferences preferences = getSharedPreferences("default", MODE_PRIVATE);
                  SharedPreferences.Editor editor = preferences.edit();
                  editor.putString("text1",txtt.getText().toString());
                  editor.putString("text2",et.getText().toString());
           .....
           ......   
                  editor.commit();


         startActivity(intent);
在下一个活动中,您可以访问该首选项,如下所示

.......
......

SharedPreferences preferences = getSharedPreferences("default", MODE_PRIVATE);

String text1= preference.getString("text1"," ");
String text2= preference.getString("text2"," ");
....
.....

txt.setText(text1);
txt2.setText(text2);

创建一个新类,将字符串作为静态字符串,并将文本值保存在该类中。 还将setter和getter函数放在该类中。
在下一个活动中,只需创建该类的对象并获取这些值。

创建一个带有静态字符串的新类,并将文本值保存在该类中。 还将setter和getter函数放在该类中。

在下一个活动中,只需创建该类的对象并获取这些值。

将其保存到SQLITEDB中您会遇到什么问题。在案例中发布错误日志您遇到了什么问题?您似乎将其发送到了一个新活动,并按原样保存在SharedRef中…在editText中输入一些文本后,单击“保存”按钮我得到了确认切换窗口然后调试模式它指示此行SharedReferences.Editor=preferences.edit();@Guian:没错。将其保存到sqlitedba中您遇到了什么问题。在这种情况下发布错误日志您遇到了什么问题?似乎您将其发送到了一个新的活动,并按原样保存在SharedRef中…在editText中输入一些文本后,单击保存按钮我得到了确认透视切换窗口,然后调试模式它指示这一行haredPreferences.Editor=preferences.edit();@Guian:你是对的。请看我的问题。我使用了intent.putextra和used sharedPreference。单击“保存”按钮时,它会进入调试模式,显示确认透视开关窗口,然后使用打印屏幕拍摄快照,或者如果你有带照相机功能的手机,则拍摄日志并在此处发布查看我的问题。我使用了intent.putextra和used SharedReference。单击save(保存)按钮时,进入调试模式,显示确认透视开关窗口,然后使用print scren(打印屏幕)拍摄快照,或者如果您的手机具有摄像头功能,则拍摄日志并在此发布获取一些错误?或未保存在首选项中的值?在logcat中发布错误。单击save(保存)按钮时,转到deb(删除)ug模式显示确认透视开关窗口。如果代码中有断点,请检查。同时检查IDE的透视设置。有关此问题的更多信息,请参阅这些S.O.讨论,或者在这里使用此行字符串text1=preference.getString(“text1”)时也可以查看;我遇到了错误。所以我使用字符串text1=preferences.getString(“text1”,即“”);获取一些错误?或未保存在首选项中的值?在logcat中发布错误。当单击“保存”按钮时,它将进入调试模式,显示确认透视开关窗口。如果代码中有断点,请检查。还请检查IDE的透视设置。有关此问题的更多信息,请参阅这些S.O.讨论,或在此处查看所以,当我使用这行字符串text1=preference.getString(“text1”);时,我得到了错误。所以我使用字符串text1=preferences.getString(“text1”);