你好,我已经实现了基于切换按钮选择该应用程序。但是当我关闭该应用程序,然后重新打开它,它会在其默认的选择是关。 因此,可以在任何budy告诉毫瓦我应该有保存toogleButton选择的状态,并基于该切换按钮选择状态有所行动。 。 。 谢谢你。
Hello i have implemented the application based on the toggleButton selection. but while i close that application and then reopen it, it will get in to its default selection that is "off". So can any budy tell mw what should i have to save the state of the toogleButton selection and perform some action based on that toggleButton selection state. . . Thanks.
使用共享preferences。
Use SharedPreferences.
tg = (ToggleButton) findViewById(R.id.toggleButton1);
tg.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
if((tg.isChecked()))
{
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("tgpref", true); // value to store
editor.commit();
}
else
{
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("tgpref", false); // value to store
editor.commit();
}
}
});
这是如何获取的值:
And this is how to retrieve the values:
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
boolean tgpref = preferences.getBoolean("tgpref", true); //default is true
if (tgpref = true) //if (tgpref) may be enough, not sure
{
tg.setChecked(true);
}
else
{
tg.setChecked(false);
}
我没有验证这一点code,但看网上的一些例子,很容易!
I did not verify this code, but look at some examples on the net, it is easy!