简体   繁体   English

将editText值存储到另一个活动

[英]Store editText value to another activity

I have more than 3 edittext. 我有3个以上的edittext。 When i enter something inside edittext i need to save this to another screen using SharedPreferences. 当我在edittext中输入内容时,我需要使用SharedPreferences将其保存到另一个屏幕。 I used Intent before pass editText value to another Activity. 在将editText值传递给另一个Activity之前,我使用了Intent。 But i need to save editText value later for editing purpose. 但是我需要稍后保存editText值以进行编辑。

code: 码:

Activity : 活动内容:

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

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

      }

Create a new class with Strings as static and save the text values there. 创建一个以Strings为静态的新类,并将文本值保存在那里。 Also put setter and getter functions in that class. 还将setter和getter函数放在该类中。 In the next activity just create object of that class and get those values. 在下一个活动中,只需创建该类的对象并获取那些值即可。

i didn't get what you want? 我没有得到你想要的?

you said that you want to save 3 edit texts value in preferences ok? 您说要在首选项中保存3个编辑文本值好吗?

you can do this in first activity too! 您也可以在第一个活动中执行此操作!

but if you are saying that you want to save edittext value in other activities then you should use intent.put extra to pass values to next activity and in that you can store values in shared preferences! 但是,如果您要在其他活动中保存edittext值,则应使用intent.put extra将值传递给下一个活动,并且可以将值存储在共享首选项中! what a big deal in that? 那有什么大不了的?

You can save some value in SharedPreferences and access that latter as follow... 您可以在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);

In next Activity you can access that preferences as follow.. 在下一个活动中,您可以按如下方式访问该首选项。

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

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