我的RadioGroup名为 statusGroup ,其中包含两个单选按钮,第一个名为 statusDone ,另一个名为 statusNotDone
我的默认检查单选按钮是我的xml文件android:checked="true"
中的statusNotDone,我有重置按钮,我需要将我的单选按钮变量重新初始化为默认值。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RadioGroup
android:id="@+id/statusGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@id/status"
android:orientation="horizontal"
android:layout_marginTop="12dp" >
<RadioButton
android:id="@+id/statusDone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/done_string" />
<RadioButton
android:id="@+id/statusNotDone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/not_done_string" />
</RadioGroup>
</RelativeLayout>
我的java文件
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_todo);
mDefaultStatusButton = (RadioButton) findViewById(R.id.statusNotDone);
mDefaultPriorityButton = (RadioButton) findViewById(R.id.medPriority);
mPriorityRadioGroup = (RadioGroup) findViewById(R.id.priorityGroup);
mStatusRadioGroup = (RadioGroup) findViewById(R.id.statusGroup);
}
// TODO - Set up OnClickListener for the Reset Button
final Button resetButton = (Button) findViewById(R.id.resetButton);
resetButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.i(TAG, "Entered resetButton.OnClickListener.onClick()");
// TODO - Reset data to default values
mTitleText.setText("");
// how to reinitialize to the default radio buttons
// reset date and time
setDefaultDateTime();
}
});
答案 0 :(得分:3)
// Please clear radio group using clearCheck() function
// Check default radio button using check() function
statusGroup.clearCheck();
statusGroup.check(R.id.statusNotDone);