动态添加单选按钮时在单选组中发出问题
Issue in radiogroup while adding radiobuttons dynamically
我没有看到任何 post 与我的问题类似的问题。我有一个 radiogroup
,它有两个按钮,如下所示,
<RadioGroup
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<RadioButton
android:id="@+id/basic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RadioButton
android:id="@+id/advanced"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RadioGroup>
在我的应用程序中,有时我需要 我动态添加的按钮,例如,
button=new AppCompatRadioButton(Activity.this);
button.setId(R.id.my_custom_id);
radiogroup.addView(button);
该无线电组的 Oncheckedchangelistener 代码,
(radioGroup).setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId== R.id.basic) {
button.setChecked(false);
//my code
}
else if(checkedId== R.id.advanced) {
button.setChecked(false);
//my code
}
else {
//it is newly added button's part
}
}
});
The problem is, when the newly added button is checked, it is not
unchecked when I am clicking other button. I tried to solve this in
OnCheckedChangeListener
by setting button.setChecked(false);
when
other buttons were checked. But it doesn't work. I don't know which makes
problem in that.
谁能帮忙me.Thanks!
不要通过 java 代码以编程方式制作单选按钮,而是这样做,即将该单选按钮放入 xml 文件中,但最初将 android:visibility 设置为消失,这将确保您的最后一个单选按钮不可见 :
<RadioGroup
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<RadioButton
android:id="@+id/basic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RadioButton
android:id="@+id/advanced"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RadioButton
android:id="@+id/new_radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility:"gone"
/>
</RadioGroup>
并且,当您需要使用单选按钮时,然后在 java class 中再次使您的单选按钮可见,如下所示:
button.setVisibility(View.VISIBLE);
然后,您可以使用 setOnCheckedChangeListener 了:
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
//your code
});
在初始化 RadioButton 时使用您的 Activity class 上下文。
而不是 Activity.this
使用 MainActivity.this
这是完整的工作代码..在你的 activity:
RadioGroup group = (RadioGroup) findViewById(R.id.group);
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
Log.d("chk", "id" + checkedId);
if (checkedId == R.id.basic) {
//some code
Toast.makeText(MainActivity.this, "First", Toast.LENGTH_SHORT).show();
} else if (checkedId == R.id.advanced) {
//some code
Toast.makeText(MainActivity.this, "Second", Toast.LENGTH_SHORT).show();
} else if (checkedId == R.id.my_custom_id) {
//some code
Toast.makeText(MainActivity.this, "Third", Toast.LENGTH_SHORT).show();
}
}
});
RadioButton button = new RadioButton(this);
button.setId(R.id.my_custom_id);
button.setText("Dynamic Button");
group.addView(button);
group.clearCheck();
在你的布局中Xml:
<RadioGroup
android:id = "@+id/group"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<RadioButton
android:id="@+id/basic"
android:layout_width="wrap_content"
android:text="Basic"
android:layout_height="wrap_content"
/>
<RadioButton
android:id="@+id/advanced"
android:layout_width="wrap_content"
android:text="Advanced"
android:layout_height="wrap_content"
/>
</RadioGroup>
我没有看到任何 post 与我的问题类似的问题。我有一个 radiogroup
,它有两个按钮,如下所示,
<RadioGroup
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<RadioButton
android:id="@+id/basic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RadioButton
android:id="@+id/advanced"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RadioGroup>
在我的应用程序中,有时我需要 我动态添加的按钮,例如,
button=new AppCompatRadioButton(Activity.this);
button.setId(R.id.my_custom_id);
radiogroup.addView(button);
该无线电组的 Oncheckedchangelistener 代码,
(radioGroup).setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId== R.id.basic) {
button.setChecked(false);
//my code
}
else if(checkedId== R.id.advanced) {
button.setChecked(false);
//my code
}
else {
//it is newly added button's part
}
}
});
The problem is, when the newly added button is checked, it is not unchecked when I am clicking other button. I tried to solve this in
OnCheckedChangeListener
by settingbutton.setChecked(false);
when other buttons were checked. But it doesn't work. I don't know which makes problem in that.
谁能帮忙me.Thanks!
不要通过 java 代码以编程方式制作单选按钮,而是这样做,即将该单选按钮放入 xml 文件中,但最初将 android:visibility 设置为消失,这将确保您的最后一个单选按钮不可见 :
<RadioGroup
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<RadioButton
android:id="@+id/basic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RadioButton
android:id="@+id/advanced"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RadioButton
android:id="@+id/new_radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility:"gone"
/>
</RadioGroup>
并且,当您需要使用单选按钮时,然后在 java class 中再次使您的单选按钮可见,如下所示:
button.setVisibility(View.VISIBLE);
然后,您可以使用 setOnCheckedChangeListener 了:
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
//your code
});
在初始化 RadioButton 时使用您的 Activity class 上下文。
而不是 Activity.this
使用 MainActivity.this
这是完整的工作代码..在你的 activity:
RadioGroup group = (RadioGroup) findViewById(R.id.group);
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
Log.d("chk", "id" + checkedId);
if (checkedId == R.id.basic) {
//some code
Toast.makeText(MainActivity.this, "First", Toast.LENGTH_SHORT).show();
} else if (checkedId == R.id.advanced) {
//some code
Toast.makeText(MainActivity.this, "Second", Toast.LENGTH_SHORT).show();
} else if (checkedId == R.id.my_custom_id) {
//some code
Toast.makeText(MainActivity.this, "Third", Toast.LENGTH_SHORT).show();
}
}
});
RadioButton button = new RadioButton(this);
button.setId(R.id.my_custom_id);
button.setText("Dynamic Button");
group.addView(button);
group.clearCheck();
在你的布局中Xml:
<RadioGroup
android:id = "@+id/group"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<RadioButton
android:id="@+id/basic"
android:layout_width="wrap_content"
android:text="Basic"
android:layout_height="wrap_content"
/>
<RadioButton
android:id="@+id/advanced"
android:layout_width="wrap_content"
android:text="Advanced"
android:layout_height="wrap_content"
/>
</RadioGroup>