繁体   English   中英

将editText值存储到另一个活动

[英]Store editText value to another activity

我有3个以上的edittext。 当我在edittext中输入内容时,我需要使用SharedPreferences将其保存到另一个屏幕。 在将editText值传递给另一个Activity之前,我使用了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);

活动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();

      }

创建一个以Strings为静态的新类,并将文本值保存在那里。 还将setter和getter函数放在该类中。 在下一个活动中,只需创建该类的对象并获取那些值即可。

我没有得到你想要的?

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

您也可以在第一个活动中执行此操作!

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

您可以在SharedPreferences保存一些值,然后按如下方式访问后者...

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM