radiobutton 怎么获取选中状态

 我来答
牛永安0HQ
2016-09-29 · 知道合伙人互联网行家
牛永安0HQ
知道合伙人互联网行家
采纳数:1224 获赞数:3261
启明教育老师

向TA提问 私信TA
展开全部
  用savedInstanceState,代码如下:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button button = (Button) findViewById(R.id.button);
RadioButton radiobutton1 = (RadioButton) findViewById(R.id.radiobutton1);
RadioButton radiobutton2 = (RadioButton) findViewById(R.id.radiobutton2);

if (savedInstanceState != null) {

boolean tmp1 = savedInstanceState.getBoolean("radio1");
if (tmp1) {
radiobutton1.setChecked(true);
}
boolean tmp2 = savedInstanceState.getBoolean("radio2");
if (tmp2) {
radiobutton2.setChecked(true);
}
}

if (IsRadioButton1Selected) {
radiobutton1.setChecked(true);
}
if (IsRadioButton2Selected) {
radiobutton2.setChecked(true);
}

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent it = new Intent(MainActivity.this, SecondActivity.class);
startActivity(it);
}
});

}

@Override
protected void onSaveInstanceState(Bundle outState) {

RadioButton radiobutton1 = (RadioButton) findViewById(R.id.radiobutton1);
RadioButton radiobutton2 = (RadioButton) findViewById(R.id.radiobutton2);

if (radiobutton1.isChecked()) {
IsRadioButton1Selected = true;
}
else if (radiobutton2.isChecked()) {
IsRadioButton2Selected = true;
}
else
Toast.makeText(this, "nothing selected", Toast.LENGTH_SHORT).show();

outState.putBoolean("radio1", IsRadioButton1Selected);
outState.putBoolean("radio2", IsRadioButton2Selected);
super.onSaveInstanceState(outState);
}
  更多0
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式