【问题标题】:Change TextView and save on SharedPreferences更改 TextView 并保存在 SharedPreferences
【发布时间】:2018-06-14 03:59:24
【问题描述】:

我有一个 TextView 包含一个文本,我想在按钮单击时更改它并将其保存在 sharedpreferences 中,直到应用程序重新启动然后它会返回到其默认文本。

这是我的代码:

TextView questionText = (TextView)findViewById(R.id.perso);
questionText.setText("Default text");
Button button = (Button) findViewById(R.id.button1);

button.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            resetConsent();
            questionText.setText("You clicked on the button");


        }
    });

【问题讨论】:

    标签: android textview sharedpreferences


    【解决方案1】:

    这里是示例代码,

        public class MainActivity extends AppCompatActivity {
    
        private static final String TAG =MainActivity.class.getSimpleName() ;
        private  TextView questionText ;
        private   Button button ;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
    
            super.onCreate(savedInstanceState);
            Log.v(TAG,"onCreate");
            setContentView(R.layout.activity_main);
            button = findViewById(R.id.button);
            questionText = findViewById(R.id.perso);
            questionText.setText("Default text");
            //Setting curent text into the shared preferance
            setSharedpreferance("ButtonText1",questionText.getText().toString());
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
    
                    questionText.setText("You clicked on the button");
                    setSharedpreferance("ButtonText1",questionText.getText().toString());
                    Log.v(TAG,"Sharedpref value"+getSharedpreferance("ButtonText1"));
                }
            });
        }
    
    
        @Override
        protected void onResume() {
    
            super.onResume();
            Log.v(TAG,"onResume");
            questionText.setText(getSharedpreferance("ButtonText1"));
        }
    
        private void setSharedpreferance(String key, String value)
        {
            SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
            SharedPreferences.Editor editor = pref.edit();
            editor.putString(key, value);  // Saving string
            // Save the changes in SharedPreferences
            editor.apply();
        }
    
        private  String getSharedpreferance(String key){
            //To get the sharedpreferance value
            SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
            return pref.getString(key, null);
        }
    }
    

    您应该需要查看 android 活动生命周期 当一个新活动创建时,如果 OnResume() 在 Activity 类中可用,它将调用以下内容。 Oncreate-> OnResume

    【讨论】:

    • 谢谢你,当我在活动之间跳转时,文本会回到原始文本,直到我重新启动应用程序才会被存储
    • 效果很好,只是 onResume 部分在我第一次打开活动时删除了 oncreate 的默认文本
    • @LoveDroid 是的,它会删除默认文本并从共享首选项中分配字符串。如果答案对您有用,请接受。
    • 我怎样才能让默认文本保持直到我点击按钮,因为在第一次创建时我什至看不到默认文本?默认文本应该在那里,直到我点击按钮
    • @LoveDroid 立即查看。
    【解决方案2】:

    只需使用 SharePreferences 文档here 中的代码,您可以在链接中看到用于读写的代码片段,而不是使用 putInt 和 getInt 使用 putString,而 getString 您需要一个 Key 来识别您刚刚使用的值已保存。

    【讨论】:

      【解决方案3】:

      创建一个扩展 Application 类的单个类,这将保持该值直到您的应用打开,并且还可以在您的应用中的不同活动中访问。如果您确实需要使用 sharedPreferences,您可以扩展相同的概念。

      更多关于机器人Applicationhere

      public class Mydata extends Application {
          private static Mydata d;
          private static String textVal;
          public Mydata getInstance(){
              return d;
          }
          @Override
          public void onCreate() {
              super.onCreate();
              d= this;
          }
      }
      

      单击按钮后,将数据保存在单个类中。

              public void onClick(View v) {
                  resetConsent();
                  questionText.setText("You clicked on the button");
                  Mydata.textVal=questionText.getText().toString();
              }
          });
      

      在 onCreate() 方法中,将TextView 中的值设置为:

        questionText.setText(Mydata.textVal);
      

      您还可以查看新的LifeCycle android 库:https://developer.android.com/topic/libraries/architecture/lifecycle

      【讨论】:

      • 谢谢,我试了代码还是不行。当我退出要将该共享首选项应用于 MainActivity 的活动并返回该活动时,我发现 textview 会返回到原始文本。我想保留新文本,直到我重新启动应用程序,而不是在活动之间跳转
      • @LoveDroid 根据需要修改答案
      猜你喜欢
      • 1970-01-01
      • 2017-10-06
      • 2017-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-07
      • 2020-06-04
      相关资源
      最近更新 更多