我怎样才能使textview中的值静态化我每次关闭并打开应用程序时都会出现在活动中 [英] How can I make the values in the textview static I mean appear on the activity every time I close and open the app

查看:93
本文介绍了我怎样才能使textview中的值静态化我每次关闭并打开应用程序时都会出现在活动中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在制作和android程序,其中edittext获取输入值并在点击保存按钮后将其放入textview中。我使用共享偏好来保存值并按顺序将它们设置为一个接一个的文本视图数量但是它们在应用程序关闭后消失并且在打开应用程序时不出现,我对这个编程字段非常陌生,请帮助代码< br $> b $ b

我的尝试:



公开class MainActivity扩展AppCompatActivity {
int i = 0;
SharedPreferences sf;
final String preferences =pref;
final String saveIt =savekey;

@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView tv1,tv2,tv3;
tv1 =(TextView)findViewById(R.id.textView);
tv2 =(TextView)findViewById(R.id.textView2);
tv3 =(TextView)findViewById(R.id.textView3);
final EditText et =(EditText)findViewById(R.id.edittext);
按钮按钮=(按钮)findViewById(R.id.button);
sf = getSharedPreferences(preferences,Context.MODE_PRIVATE);

tv1.setText(sf.getString(saveIt,));


tv2.setText(sf.getString(saveIt,));



tv3.setText(sf.getString(saveIt,));




tv1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
i = 0;
}
});
tv2.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
i = 1;
}
} );
tv3.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
i = 2;
}
} );
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
final String store = et.getText()。toString() ;
sf = getSharedPreferences(preferences,Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sf.edit();
editor.putString(saveIt,store);
editor。 apply();
switch(i){
case 0:
tv1.setText(sf.getString(saveIt,));
et.setText() ;
break;
case 1:
tv2.setText(sf.getString(saveIt,));
et.setText();
break ;
案例2:
tv3.setText(sf.getString(saveIt,));
et.setText();
打破;
}
}
});

}
}





我的xml

< RelativeLayout xmlns:android =http://schemas.android.com/apk/res/android
xmlns:tools =http://schemas.android.com/tools
android:id =@ + id / activity_main
android:layout_width =match_parent
android:layout_height =match_parent
android:paddingBottom =@ dimen / activity_vertical_margin
android:paddingLeft =@ dimen / activity_horizo​​ntal_margin
android:paddingRight =@ dimen / activity_horizo​​ntal_margin
android:paddingTop =@ dimen / activity_vertical_margin
tools:context = com.kalla.monu.strings.MainActivity>

< EditText
android:layout_width =match_parent
android:layout_height =wrap_content
android:text =Hello World!
android:id =@ + id / edittext/>

< TextView
android:text =TextView
android:layout_width =match_parent
android:layout_height =wrap_content
android: layout_below =@ + id / textView
android:layout_alignStart =@ + id / textView
android:layout_marginStart =30dp
android:layout_marginTop =49dp
android:id =@ + id / textView2
android:background =@ android:color / holo_blue_bright/>

< TextView
android:text =TextView
android:layout_width =match_parent
android:layout_height =wrap_content
android: layout_marginTop =16dp
android:id =@ + id / textView
android:layout_below =@ + id / edittext
android:layout_alignEnd =@ + id / edittext
android:background =@ android:color / darker_gray/>

<按钮
android:text =按钮
android:layout_width =wrap_content
android:layout_height =wrap_content
android: id =@ + id / button
android:layout_alignParentBottom =true
android:layout_centerHorizo​​ntal =true/>

< TextView
android:text =TextView
android:layout_width =match_parent
android:layout_height =wrap_content
android: id =@ + id / textView3
android:background =@ android:color / holo_blue_light
android:layout_centerVertical =true
android:layout_alignParentStart =true/>
< / RelativeLayout>

解决方案

您的代码似乎没有任何问题。为了说服自己,我在AVD上进行了测试,并且结论是工作正常。

+++++ [round 2] +++++

引用:

当按下主页按钮关闭应用程序并打开应用程序保存实例时,会显示存储的值,但是当按下后退按钮关闭应用程序时并打开应用程序将值设置回默认值。文本值设置为hi,你好,如何使用主页按钮是的我可以在打开时看到它们但是后退按钮它们被设置为默认值

你应该早些提到过这个。这与你原来的问题非常不同,这个问题表明根本没有任何效果。

归结为HOME按钮和BACK按钮之间的差异行为。

按住Home将停止并将当前活动移至后台并且系统仍保持其状态,您将恢复恢复后的状态。

至于BACK按钮,这是一个不同的故事,目前的活动被摧毁。因此,当您再次重新启动它时,它会在编辑文本中以默认的hello世界开始,并按照代码的onCreate()中的指示使用先前存储的首选项填充三个textview。

它确实如此正是你编码的内容。

听取理查德,阅读Android活动生命周期。


和彼得一样,我测试过它工作正常。我认为你看到的是一个应用程序的效果暂停 d但是没有完全停止,所以当你第二次启动时它不会通过它的 OnCreate 方法,但从上次暂停的位置恢复 s。您可以通过运行应用程序来检查,然后转到设备或模拟器的主页。现在转到设置 - >应用页面和强制停止应用程序。然后再次运行它,您应该看到更改扩散到所有三个文本框。要获得完整的理解,请参阅活动生命周期| Android开发者 [ ^ ]。


我在一个解决类似问题的解决方案中看到它并将其应用于Android studio 2.2,这是一个自动完成的单词


Hi,
I am making and android program in which edittext takes input value and put it in textview after clicking on save button. I used sharedpreferences to save the values and set them to number of textviews one after the other in order but they disappear after app is closed and do not appear on opening the app, I am very much new to this programming field kindly help with code

What I have tried:

public class MainActivity extends AppCompatActivity {
   int i = 0;
    SharedPreferences sf;
    final String preferences = "pref";
    final String saveIt = "savekey";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final TextView tv1,tv2,tv3;
         tv1 = (TextView)findViewById(R.id.textView);
        tv2 = (TextView)findViewById(R.id.textView2);
        tv3 = (TextView)findViewById(R.id.textView3);
        final EditText et = (EditText)findViewById(R.id.edittext);
        Button button = (Button)findViewById(R.id.button);
        sf = getSharedPreferences(preferences, Context.MODE_PRIVATE);

                tv1.setText(sf.getString(saveIt, ""));


                tv2.setText(sf.getString(saveIt, ""));



                tv3.setText(sf.getString(saveIt, ""));
                



        tv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                i = 0;
            }
        });
        tv2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                i = 1;
            }
        });
        tv3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                i = 2;
            }
        });
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String store = et.getText().toString();
                sf = getSharedPreferences(preferences, Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sf.edit();
                editor.putString(saveIt, store);
                editor.apply();
                switch (i){
                    case 0:
                        tv1.setText(sf.getString(saveIt, ""));
                        et.setText(" ");
                        break;
                    case 1:
                        tv2.setText(sf.getString(saveIt, ""));
                        et.setText(" ");
                        break;
                    case 2:
                        tv3.setText(sf.getString(saveIt, ""));
                        et.setText(" ");
                        break;
                }
            }
        });

    }
}



my xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.kalla.monu.strings.MainActivity">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:id="@+id/edittext" />

    <TextView
        android:text="TextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView"
        android:layout_alignStart="@+id/textView"
        android:layout_marginStart="30dp"
        android:layout_marginTop="49dp"
        android:id="@+id/textView2"
        android:background="@android:color/holo_blue_bright" />

    <TextView
        android:text="TextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:id="@+id/textView"
        android:layout_below="@+id/edittext"
        android:layout_alignEnd="@+id/edittext"
        android:background="@android:color/darker_gray" />

    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:text="TextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView3"
        android:background="@android:color/holo_blue_light"
        android:layout_centerVertical="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

解决方案

There did't seem to have any problem in your code. To convince myself, I tested it on an AVD, and the verdict is it is working fine.
+++++[round 2]+++++

Quote:

when close the app by pressing home button and open app the instance saved,stored values appear but, when close the app by pressing back button and open the app the values set back to default. text values set to hi,hello,howru with home button yes i can see them on opening but with back button they get set to default


You should have mentioned this earlier. This is very different from your original question which suggested that nothing worked at all.
It boils down to the difference behaviors between HOME button and BACK button.
Pressing Home will stop and move the current activity to the background and the system still retains its state, you get back what was left off on resume.
As for BACK button, it is a different story, the current activity is destroyed. So when you re-launch it again, it starts all over with the default hello world in edit text and populates the three textviews with previously stored preference as instructed in onCreate() of your code.
It does exactly what you have coded.
Listen to Richard, read up on Android activity life cycle.


Like Peter, I tested and it is working OK. I think what you are seeing is the effect of an app that gets Paused but not stopped completely, so when you start it a second time it does not go through its OnCreate method, but Resumes from where it was last paused. You can check this by running the app, then going to the home page of your device or emulator. Now go to the Settings -> Applications page and force stop the application. Then run it again and you should see the changes proliferated to all three text boxes. For a full understanding see The Activity Lifecycle | Android Developers[^].


I saw it mentioned in a solution for a similar problem and applied it Android studio 2.2, it's an auto complete word


这篇关于我怎样才能使textview中的值静态化我每次关闭并打开应用程序时都会出现在活动中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆