做一个可以独立选择和激活的按钮组的最佳方法是什么?

Ale*_*ana 14 android android-button

我试图在android中做一组可以选择的按钮,只激活其中一个按钮.我需要使用与radiogroup和radiobuttons相同的逻辑.

我尝试了很多替代方案,但我想要最有效的方法.我该怎么做?

Huo*_*eng 15

在此输入图像描述

您可以使用这种简单的方法:

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>
Run Code Online (Sandbox Code Playgroud)

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;
    }
}
Run Code Online (Sandbox Code Playgroud)


ook*_*.kb 6

尽管您可以按照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>
Run Code Online (Sandbox Code Playgroud)

并将其设置为按钮背景: android:background="@drawable/radio"


Den*_*ldt 5

您仍然可以在单选组中使用单选按钮,并使用属性使每个单选按钮看起来像一个按钮。

在xml中,对于每个单选按钮set android:button="@null",点将不可见。您可以添加一些填充以使其看起来有所不同。

在代码中,将CheckedChangeListener设置为radioGroup,然后找到对带有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
  }
});
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!