【问题标题】:Store editText value to another activity将 editText 值存储到另一个活动
【发布时间】:2013-09-24 11:15:45
【问题描述】:

我有超过 3 个编辑文本。当我在 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);

活动一:

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

      }

【问题讨论】:

  • 保存到SqliteDB
  • 您遇到了什么问题。发布错误日志以防万一
  • 您的问题是什么?看来您将其发送到一个新活动并将其保存在 sharedpref 中,因为它应该是......
  • 在editText中输入一些文本后点击保存按钮我得到确认透视切换窗口然后调试模式它指示这一行SharedPreferences.Editor editor = preferences.edit();
  • @Guian:你说得对。

标签: android arrays android-intent sharedpreferences


【解决方案1】:

我没有得到你想要的?

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

你也可以在第一个活动中这样做!

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

【讨论】:

  • 看我的问题。我使用了 intent.putextra 并使用了 sharedPreference。
  • 当点击保存按钮进入调试模式时显示确认透视切换窗口
  • 然后使用打印屏幕拍摄快照,或者如果您有带相机功能的手机,然后拍摄日志照片并在此处发布
【解决方案2】:

您可以在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);

【讨论】:

  • 出现错误?或未保存在首选项中的值?在 logcat 中发布错误。
  • 当点击保存按钮进入调试模式时显示确认透视切换窗口
  • 你的代码中可能有断点,检查一下。还要检查 IDE 的透视设置。
  • 参考这些 S.O.讨论有关此问题的更多信息stackoverflow.com/questions/4330115/… 或这里还有stackoverflow.com/questions/2552568/…
  • 当我使用这一行 String text1= preference.getString("text1");我有错误。所以我习惯了 String text1= preferences.getString("text1", " ");
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-07
    • 2018-06-09
    • 1970-01-01
    • 2019-08-24
    • 1970-01-01
    • 1970-01-01
    • 2018-03-06
    • 1970-01-01
    相关资源
    最近更新 更多