radiobutton 怎么获取选中状态
1个回答
展开全部
用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
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
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询