如何在整个Android应用程序中保存rediobutton的状态?

如何在整个Android应用程序中保存rediobutton的状态?,第1张

概述我有两个单选组,每个组有两个单选按钮.每个组中的第一个单选按钮的默认值为true(即选中).当用户单击任何单选按钮时,以及当用户从其他活动中恢复时,在这些单选按钮组/单选按钮上所做的选择都将消失.如何保存/恢复单选/单选按钮的选择状态?这是我的Java代码.finalRadioBu

我有两个单选组,每个组有两个单选按钮.
每个组中的第一个单选按钮的默认值为true(即选中).
当用户单击任何单选按钮时,以及当用户从其他活动中恢复时,在这些单选按钮组/单选按钮上所做的选择都将消失.
如何保存/恢复单选/单选按钮的选择状态?
这是我的Java代码.

        final Radiobutton radioOn = ( Radiobutton )findVIEwByID(        R.ID.rbOn );    final Radiobutton radioOff = ( Radiobutton )findVIEwByID(        R.ID.rbOff );    radioOn.setChecked( true );    radioOn.setonClickListener( auto_lock_on_Listener );    radioOff.setonClickListener( auto_lock_off_Listener );

请帮忙.

解决方法:

您需要重写onSaveInstanceState(Bundle savedInstanceState),并将要更改的应用程序状态值写入Bundle参数,如下所示:

@OverrIDe    public voID onSaveInstanceState(Bundle savedInstanceState) {      // Save UI state changes to the savedInstanceState.      // This bundle will be passed to onCreate if the process is      // killed and restarted.      savedInstanceState.putBoolean("MyBoolean", true);      savedInstanceState.putDouble("myDouble", 1.9);      savedInstanceState.putInt("MyInt", 1);      savedInstanceState.putString("MyString", "Welcome back to AndroID");      // etc.      super.onSaveInstanceState(savedInstanceState);    }

Bundle本质上是一种存储NVP(“名称-值对”)映射的方法,它将被传递给onCreate和onRestoreInstanceState,您可以在其中提取如下值:

@OverrIDepublic voID onRestoreInstanceState(Bundle savedInstanceState) {  super.onRestoreInstanceState(savedInstanceState);  // Restore UI state from the savedInstanceState.  // This bundle has also been passed to onCreate.  boolean myBoolean = savedInstanceState.getBoolean("MyBoolean");  double myDouble = savedInstanceState.getDouble("myDouble");  int myInt = savedInstanceState.getInt("MyInt");  String myString = savedInstanceState.getString("MyString");}

通常,您会使用这种技术来存储应用程序的实例值(选择,未保存的文本等).

总结

以上是内存溢出为你收集整理的如何在整个Android应用程序中保存rediobutton的状态?全部内容,希望文章能够帮你解决如何在整个Android应用程序中保存rediobutton的状态?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1072862.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存