我想改变单选按钮的圆圈颜色,我无法理解要设置的属性.我所拥有的背景颜色是黑色,因此它变得不可见.我想将圆圈的颜色设置为白色.
有没有办法在android中设置RadioGroup的选定索引,除了循环子单选按钮并选择检查所选索引处的单选按钮?
注意:我在运行时填充单选按钮组.
根据我的理解,要确定是否"单击"复选框并查找是否已选中,可以使用以下代码:
cb=(CheckBox)findViewById(R.id.chkBox1);
cb.setOnCheckedChangeListener(this);
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
cb.setText("This checkbox is: checked");
}
else {
cb.setText("This checkbox is: unchecked");
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我无法弄清楚如何对无线电组进行上述操作的逻辑.
这是我的RadioGroup的xml:
<RadioGroup android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radio1" android:checked="true"
android:text="RadioButton1">
</RadioButton>
<RadioButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radio2" android:text="RadioButton2" android:checked="true">
</RadioButton>
<RadioButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radio3" android:text="RadioButton3">
</RadioButton>
</RadioGroup>
Run Code Online (Sandbox Code Playgroud)
问题:我是否需要设置另一个监听器,或者监听器是否已经"注册"了这个组?
此外,应该在RadioGroup还是RadioButton上设置监听器?
android listener radio-group android-radiogroup android-radiobutton
我想要实现的是:在活动开始后,我希望没有选择/检查RadioButton.
我的问题是:当活动开始时,总是选择/检查第一个RadioButton.
我radioButton1.setChecked(false)在初始化radiobutton之后尝试(在onCreate里面),但是当活动开始时,我无法手动检查/选择第一个radiobutton.直到我选择第二或第三个单选按钮,我现在可以选择/检查第一个单选按钮.
我在radiogroup中创建了一个单选按钮,但是当我尝试运行应用程序时,可以一直选择所有单选按钮,并且如何一次只能选择一个单选按钮?
我正在使用片段
RadioGroup radioGroup = (RadioGroup) rootView.findViewById(R.id.RGroup);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// find which radio button is selected
if(checkedId == R.id.Abdominal) {
Toast.makeText(getActivity().getApplicationContext(), "choice: A",
Toast.LENGTH_SHORT).show();
} else if(checkedId == R.id.Arm) {
Toast.makeText(getActivity().getApplicationContext(), "choice: B",
Toast.LENGTH_SHORT).show();
} else if(checkedId == R.id.Back){
Toast.makeText(getActivity().getApplicationContext(), "choice: C",
Toast.LENGTH_SHORT).show();
} else if(checkedId == R.id.Chest){
Toast.makeText(getActivity().getApplicationContext(), "choice: D",
Toast.LENGTH_SHORT).show();
} else if(checkedId == R.id.Leg){
Toast.makeText(getActivity().getApplicationContext(), "choice: E",
Toast.LENGTH_SHORT).show();
} else if(checkedId == R.id.Shoulder){
Toast.makeText(getActivity().getApplicationContext(), "choice: F",
Toast.LENGTH_SHORT).show(); …Run Code Online (Sandbox Code Playgroud) 我知道这听起来很怪异,所以这是图片.
它保持正确的价值.选择(部分)正确的单选按钮.其中的所有逻辑OnCheckedChangeListener都正确执行.我完全惊呆了.为什么没有完全检查单选按钮?
我唯一能想到的就是我正在使用 Rx2Firebase
periodRetriever = FirebaseHelper.getInstance(getContext()).getPeriod()
.defaultIfEmpty(0)
.distinctUntilChanged()
.subscribe(new Consumer<Integer>() {
@Override
public void accept(@NonNull Integer headerType) throws Exception {
getRadioViewChecked(headerType).setChecked(true);
}
});
Run Code Online (Sandbox Code Playgroud)
EDIT1
EDIT2
布局:
<RadioGroup
android:id="@+id/rgPeriod"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.v7.widget.AppCompatRadioButton
android:id="@+id/rbMonth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="@string/month" />
<android.support.v7.widget.AppCompatRadioButton
android:id="@+id/rbWeek"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="@string/week" />
<android.support.v7.widget.AppCompatRadioButton
android:id="@+id/rb4Weeks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="@string/four_weeks" />
</RadioGroup>
Run Code Online (Sandbox Code Playgroud) android android-layout android-radiogroup android-radiobutton
我将图像设置为RadioButton的drawableLeft,但它很大.我想设置图像的高度,宽度和scaleType,以使它看起来不错,就像在ImageView中一样:
android:layout_width="30dip"
android:layout_height="30dip"
android:scaleType="fitXY"
Run Code Online (Sandbox Code Playgroud)
但我发现当它在drawableLeft中设置时,没有图像属性.
有没有办法解决这个问题.
是否有可能在XML中处理这个问题?
我创建一个RadioGroup与RadioButton小号动态的,需要有一个单选按钮默认为选中.
我通过使用radioButton.setChecked(true)和完成了这个radioButton.toggle();
我遇到的问题是,当我在运行时选择另一个单选按钮时,第一个单选按钮保持选中状态,因此我最终在收音机组中有两个选中的单选按钮.
有谁有这个问题,知道如何解决它?
private RadioButton addRadioButton(String type, String price){
RadioButton radio = new RadioButton(Order.this);
radio.setText(type + " (" + Utils.formatCurrency(price) + ")");
radio.setTextAppearance(Order.this, R.style.portalCellTextStyle);
radio.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
radio.setTag(price);
if(type.toLowerCase().equals("m"))
radio.toggle();
return radio;
}
Run Code Online (Sandbox Code Playgroud) 在我取消选中它们或单击组中的另一个 RadioButton 后,我的 RadioGroup 中的 RadioButton 会留下一个黑色的选中圆圈。我如何防止这种情况发生?
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Test"
android:textAlignment="textStart"
android:layoutDirection="rtl"
android:layout_gravity="start"
android:background="?android:selectableItemBackground"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="textStart"
android:layoutDirection="rtl"
android:layout_gravity="start"
android:background="?android:selectableItemBackground"/>
</RadioGroup>
Run Code Online (Sandbox Code Playgroud)
发生在我的 API 19 真实设备上,而不是我的 API 27
编辑:_________________________________________________
曾尝试使用不起作用的自定义选择器
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/qrmenu_toolbar"
android:orientation="vertical">
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Resume"
android:layoutDirection="rtl"
android:layout_gravity="start"
android:drawablePadding="12dp"
android:paddingStart="16dp"
android:paddingTop="12dp"
android:paddingEnd="16dp"
android:paddingBottom="12dp"
app:drawableLeftCompat="@drawable/ic_resume"
android:button="@drawable/radiobutton_selector"
Run Code Online (Sandbox Code Playgroud)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/radio_checked" android:state_checked="true" />
<item android:drawable="@drawable/radio_unchecked" android:state_checked="false" />
</selector>
Run Code Online (Sandbox Code Playgroud)
主题:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorBlack</item>
<item …Run Code Online (Sandbox Code Playgroud) 我正在 for 循环中以编程方式将单选按钮添加到 RadioGroup 中。我希望我的按钮在未选中时具有某种背景颜色,在选中时具有另一种背景颜色(一次只能选择一个)。我的问题是我也希望这个背景是一个圆角矩形。我让它部分工作,但每当我选择一个答案时,它都会将该按钮的背景设置为白色,但不会重置任何其他按钮。
我知道我需要某种类型的选择器对象,以添加单选组所需的切换行为。我找到了一些例子,但它们只展示了如何将纯色设置为背景。我需要使用这个圆角形状。所以,我的麻烦是弄清楚如何将两者结合起来。我的做法可能完全错误。如果有更好的方法来实现我想要的,我宁愿使用它,而不是挽救我已经尝试过的方法。
我通过创建两个 XML 文件来接近这个目标,每个文件都有一个形状对象和它自己的背景颜色。如果选择了单选按钮,其样式将设置为 radio_button_checked" 样式。但是,我知道我正在接近这个错误,因为另一个选中的按钮不会关闭。我基本上可以单击所有按钮并使它们全部具有白色背景。我知道有更好的方法可以做到这一点,我只是不知道它是什么
这是我添加按钮的循环,以及 radiogroup rg 的更改侦听器(rg 是我的 RadioGroup,答案只是字符串的 HashMap。它们不会影响与此问题相关的任何内容。buttonTintList 行只是为了改变圆圈颜色)
for(int i = 0; i < answers.size(); i++)
{
RadioButton rb = new RadioButton(context);
rb.setTextColor(ContextCompat.getColor(context, R.color.colorPrimaryDark));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
rb.setButtonTintList(ColorStateList.valueOf(ContextCompat.getColor( context, R.color.colorAccent)));
}
rb.setBackground(context.getResources().getDrawable(R.drawable.radiobutton_style_unchecked));
rb.setText(answers.get(i));
rb.setWidth(800);
rb.setHeight(150);
rb.setTextSize(18);
rb.setLayoutParams(params);
rg.addView(rb);
}
/* Store the answer */
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb= (RadioButton)group.findViewById(checkedId);
answer = rb.getText().toString();
rb.setBackground(context.getResources().getDrawable(R.drawable.radiobutton_style_checked));
}
});
Run Code Online (Sandbox Code Playgroud)
以下是未选中按钮的 XML (radiobutton_style_unchecked.xml) …