gpt4 book ai didi

android - 在 RadioGroup 上调用 clearCheck() 后调用 RadioGroup 的 setOnCheckedChangeListener 两次

转载 作者:太空宇宙 更新时间:2023-11-03 11:25:15 25 4
gpt4 key购买 nike

我有一个带有单选按钮的 RadioGroup。要清除选中的项目,我正在调用 radiogroup.clearCheck()。但是,RadioGroup.setOnCheckedChangeListener 回调在调用 clearCheck() 后被触发了两次。

public class MainActivity extends Activity {
private RadioGroup rGroup;
private String str;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rGroup = (RadioGroup) findViewById(R.id.radioGroup1);
rGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if (checkedId == R.id.radio0) {
str = "Radio0";
} else if (checkedId == R.id.radio1) {
str = "Radio1";
} else if (checkedId == R.id.radio2) {
str = "Radio2";
}
}
});
}

public void clearRadio(View v) {
rGroup.clearCheck();
}

}

xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginLeft="48dp"
android:layout_marginTop="58dp" >

<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton1" />

<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton2" />

<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton3" />
</RadioGroup>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/radioGroup1"
android:onClick="clearRadio"
android:text="Clear" />

</RelativeLayout>

提前致谢。

最佳答案

试试这个……

public class MainActivity extends Activity {

private RadioGroup rGroup;
private String str;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rGroup = (RadioGroup) findViewById(R.id.radioGroup1);
rGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if (checkedId == R.id.radio0) {
str = "Radio0";
} else if (checkedId == R.id.radio1) {
str = "Radio1";
} else if (checkedId == R.id.radio2) {
str = "Radio2";
}
}
});
}


}

public void clearRadio(View v) {
rGroup.clearCheck();
}

关于android - 在 RadioGroup 上调用 clearCheck() 后调用 RadioGroup 的 setOnCheckedChangeListener 两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20676199/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com