简体   繁体   English

Android,只需查看有关视图重置的问题

[英]Android, simply view question about view reset

I have a button in my activity, when you click the button an if statement is ran against its text label; 我的活动中有一个按钮,当您单击该按钮时,if语句将针对其文本标签运行;

if(BTN_1.getText()=="firsttext"){
    //do some stuff, then...
    BTN_1.setText("secondtext");
}else if(BTN_1.getText()=="secondtext"){
    //do other stuff, then...
    BTN_1.setText("firsttext");
}

Firstly, if I hit the home button and go back to the desktop and then click back onto my app the view has reset its self; 首先,如果我按下主页按钮,然后回到桌面,然后再单击回到我的应用程序,则视图已重置其自身。 if I press the button and leave it in a state where the text of the button is "secondtext", when I return to my app it says "firsttext", how can I stop the view of my app refreshing its self like this? 如果按下按钮并将其保持在按钮的文本为“第二文本”的状态,当我返回到我的应用程序时,它显示为“第一文本”,如何停止应用程序的视图以刷新自身状态?

Secondly, under my XML layout I have defined the buttons text; 其次,在我的XML布局下,我定义了按钮文本; android:text="firsttext" But this won't actually match my if statement above, under onCreate of this app I have: BTN_CONNECT.setText("Connect"); android:text =“ firsttext”但这实际上与上面的if语句不匹配,在此应用程序的onCreate下,我具有:BTN_CONNECT.setText(“ Connect”); But visually the text of the button is exactly the same, why won't it match? 但是在视觉上按钮的文本完全相同,为什么不匹配?

Thanks for reading :) 谢谢阅读 :)

Where do I start? 我从哪里开始?

  • String compares need to be done with equals() , not == . 字符串比较需要使用equals()而不是==
  • String compares to check your state are bad to begin with. 字符串比较开始检查您的状态是否不好。 Use an integer/enum to see manage your state. 使用整数/枚举查看状态。
  • Depending on how long you want your state to persist, you can either do it in Activity.saveInstanceState() (so it will persist if you change orientation), or in the SharedPreferences if you want it to persist forever. 根据您希望状态保持多长时间,您可以在Activity.saveInstanceState() (这样,如果您更改方向,它将保持不变),或者如果您希望状态一直保持不变,则可以在SharedPreferences
  • Don't use hard-coded strings for android:text, use resources, so you can translate them. 请勿对android:text使用硬编码的字符串,而要使用资源,因此可以翻译它们。
  • I don't understand your last point. 我不明白你的最后一点。

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

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