我正在尝试在 android 中做一组可以选择并仅激活其中一个的按钮。我需要使用与单选组和单选按钮相同的逻辑。
我尝试了很多选择,但我想要最有效的方法。我该怎么做?
你可以使用这个简单的方法:
1.activity_button_group.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A"
android:id="@+id/btn0"
android:layout_gravity="center_vertical" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B"
android:id="@+id/btn1"
android:layout_gravity="center_vertical" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C"
android:id="@+id/btn2"
android:layout_gravity="center_vertical" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="D"
android:id="@+id/btn3"
android:layout_gravity="center_vertical" />
</LinearLayout>
2.ButtonGroupActivity.java
public class ButtonGroupActivity extends Activity implements View.OnClickListener{
private Button[] btn = new Button[4];
private Button btn_unfocus;
private int[] btn_id = {R.id.btn0, R.id.btn1, R.id.btn2, R.id.btn3};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button_group);
for(int i = 0; i < btn.length; i++){
btn[i] = (Button) findViewById(btn_id[i]);
btn[i].setBackgroundColor(Color.rgb(207, 207, 207));
btn[i].setOnClickListener(this);
}
btn_unfocus = btn[0];
}
@Override
public void onClick(View v) {
//setForcus(btn_unfocus, (Button) findViewById(v.getId()));
//Or use switch
switch (v.getId()){
case R.id.btn0 :
setFocus(btn_unfocus, btn[0]);
break;
case R.id.btn1 :
setFocus(btn_unfocus, btn[1]);
break;
case R.id.btn2 :
setFocus(btn_unfocus, btn[2]);
break;
case R.id.btn3 :
setFocus(btn_unfocus, btn[3]);
break;
}
}
private void setFocus(Button btn_unfocus, Button btn_focus){
btn_unfocus.setTextColor(Color.rgb(49, 50, 51));
btn_unfocus.setBackgroundColor(Color.rgb(207, 207, 207));
btn_focus.setTextColor(Color.rgb(255, 255, 255));
btn_focus.setBackgroundColor(Color.rgb(3, 106, 150));
this.btn_unfocus = btn_focus;
}
}
您仍然可以在单选组中使用单选按钮,并使用属性使每个单选按钮看起来像一个按钮。
在xml中,为每个单选按钮设置
android:button="@null"
,这样圆点就不会可见。您可以添加一些填充以使其看起来不同。
在代码中,给你的radioGroup设置一个CheckedChangeListener,然后用checkedId找到checkedView(RadioButton)的引用。在此示例中,我刚刚更改了视图的背景颜色,但您也可以添加不同的背景。如果 radioButton 不为 null,则表示它已经被更改,所以我将重新设置它的初始状态。
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (radioButton != null) {
radioButton.setBackgroundColor(Color.TRANSPARENT);
radioButton.setButtonDrawable(0); // removes the image
}
radioButton = (RadioButton) group.findViewById(checkedId);
radioButton.setBackgroundColor(Color.YELLOW);
radioButton.setButtonDrawable(R.drawable.icon); //sets the image
}
});
希望这有帮助!
虽然您可以按照 Denny Schuldt 的建议在代码中设置它,但“更清洁”的方法是在 xml 中进行设置(例如
drawable/radio.xml
):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_checked" android:state_checked="true" />
<item android:drawable="@android:color/transparent" />
</selector>
并将其设置为按钮背景:
android:background="@drawable/radio"
自 2015 年以来,我们的绿色机器人小乐园发生了很多变化。 现在(2023),人们使用Segmented Buttons,有了
MaterialButtonToggleGroup
,你可以很容易地通过漂亮的设计来实现:
<com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/my_toggle_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:selectionRequired="false" <!-- Here you can define if at least one should be selected -->
app:singleSelection="true"> <!-- Here you define that only one will be selected -->
<Button
android:id="@+id/a_button"
style="?attr/materialIconButtonOutlinedStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="A" />
<Button
android:id="@+id/b_button"
style="?attr/materialIconButtonOutlinedStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="B" />
<Button
android:id="@+id/c_button"
style="?attr/materialIconButtonOutlinedStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="C" />
<Button
android:id="@+id/d_button"
style="?attr/materialIconButtonOutlinedStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="D" />
<Button
android:id="@+id/e_button"
style="?attr/materialIconButtonOutlinedStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="E" />
</com.google.android.material.button.MaterialButtonToggleGroup>
app:singleSelection
属性定义一次是否只选择一个。
app:selectionRequired
属性定义是否至少应选择一个。