在活动Android中保存Button的状态 [英] Save state of Button in activity Android
本文介绍了在活动Android中保存Button的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的活动中有两个按钮(button1
,button2
). botton1
是'Enabled'(true),而另一个不是.
I have two buttons(button1
, button2
) in my activty. botton1
is 'Enabled'(true) , while the other one isn't.
当我单击button1
时,button2
变为启用".我想在此状态下记录此活动,以供我的应用程序下次使用.
When I click on button1
, button2
becomes 'Enabled'. I want to record this activity in this state for the next utilization of my application.
推荐答案
您可以将状态保存在sharedPrefrences
中,请尝试
You can save state in sharedPrefrences
, try this
SharedPreferences prefs = getSharedPreferences("Your preference name here", MODE_PRIVATE)
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
b2.setEnabled(true);
prefs.edit().putBoolean("b2enabled", true).commit();//saving State here
}
});
现在您要在其中使用此状态的地方,只需简单地获取状态即可.
And now where you want to use this state simple get the state as.
Boolean b2state= prefs.getBoolean("b2enabled", false); //false is the default value
如果要启用或禁用结果按钮
And if you want to enable or disable button on result do
if (b2state){
b2.enabled(true);
}
else{
b2.enabled(false);
}
这篇关于在活动Android中保存Button的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文