【问题标题】:How to make single choice button in android?如何在android中制作单选按钮?
【发布时间】:2014-09-05 06:02:38
【问题描述】:

我是android编程的新手,正在尝试制作一个单选按钮组。

例如, 我的应用程序中有 3 个按钮,当单击一个按钮时,其他按钮应未单击且未着色。

我怎样才能有效地做到这一点?

只是给一个提示:(比如我应该使用什么类或方法..?

【问题讨论】:

标签: android button onclick viewgroup buttongroup


【解决方案1】:

试试这个方法,希望能帮助你解决问题。

/drawable/radio_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/white" android:state_checked="true" android:state_pressed="false"/>
    <item android:drawable="@android:color/black" android:state_checked="false" android:state_pressed="false"/>
    <item android:drawable="@android:color/white" android:state_checked="true" android:state_pressed="true"/>
    <item android:drawable="@android:color/black" android:state_checked="false" android:state_pressed="true"/>
</selector>

/drawable/radio_text_color_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@android:color/black" android:state_checked="true" />
    <item android:color="@android:color/black" android:state_pressed="true"/>
    <item android:color="@android:color/white" />
</selector>

/layout/customradiobutton.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@android:color/darker_gray"
    android:gravity="center">

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@null"
            android:background="@drawable/radio_selector"
            android:text="Radio1"
            android:textColor="@drawable/radio_text_color_selector"
            android:checked="true"
            android:padding="10dp"/>
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@null"
            android:background="@drawable/radio_selector"
            android:text="Radio2"
            android:layout_marginLeft="5dp"
            android:textColor="@drawable/radio_text_color_selector"
            android:padding="10dp"/>
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@null"
            android:background="@drawable/radio_selector"
            android:text="Radio3"
            android:layout_marginLeft="5dp"
            android:textColor="@drawable/radio_text_color_selector"
            android:padding="10dp"/>
        </RadioGroup>

</LinearLayout>

【讨论】:

    【解决方案2】:

    你是这个意思吗:

    Button myButton1;
    Button myButton2;
    Button myButton3;
    
    myButton1 = (Button) findViewById(R.id.button1);
    
    myButton1.setOnClickListener(new OnClickListener() {
    
        @Override
        public void onClick(View arg0) {
    
            myButton1.setBackgroundColor(Color.Green); 
            myButton2.setBackgroundColor(Color.Gray); 
            myButton3.setBackgroundColor(Color.Gray); 
        }
    });
    
    myButton2 = (Button) findViewById(R.id.button1);
    
    myButton2.setOnClickListener(new OnClickListener() {
    
        @Override
        public void onClick(View arg0) {
    
            myButton2.setBackgroundColor(Color.Green); 
            myButton1.setBackgroundColor(Color.Gray); 
            myButton3.setBackgroundColor(Color.Gray); 
        }
    });
    
    myButton3 = (Button) findViewById(R.id.button1);
    
    myButton3.setOnClickListener(new OnClickListener() {
    
        @Override
        public void onClick(View arg0) {
    
            myButton3.setBackgroundColor(Color.Green); 
            myButton1.setBackgroundColor(Color.Gray); 
            myButton22.setBackgroundColor(Color.Gray); 
        }
    });
    

    【讨论】:

      【解决方案3】:

      我也不是经验丰富的开发人员。但答案将取决于你想要达到的目标。您可以使用 andruboy 提到的单选组,但我个人不喜欢单选按钮的外观,所以我经常使用看起来像这样的按钮创建自己的单选组。

      myButton1 = (ImageButton) findViewById(R.id.button1);
      myBytton2 =  (ImageButton) findViewById(R.id.button2);
      myBytton3 =  (ImageButton) findViewById(R.id.button3);
      // this variable indicate which button was clicked as last, u can use this info for next action
      int whichButtonClicked = 1;
      
      myButton1.setOnClickListener(new OnClickListener() {
      
      @Override
      public void onClick(View arg0) {
           whichButtonClicked = 1;
           myButton1.setBackgroundColor(Color.Red);
           myButton2.setBackgroundColor(Color.Gray);
           myButton3.setBackgroundColor(Color.Gray);
      
      }
      
      });
      myButton2.setOnClickListener(new OnClickListener() {
      
      @Override
      public void onClick(View arg0) {
           whichButtonClicked = 2;
           myButton1.setBackgroundColor(Color.Gray);
           myButton2.setBackgroundColor(Color.Red);
           myButton3.setBackgroundColor(Color.Gray);
      
      }
      
      });
      

      第三个按钮的代码看起来很相似。

      我是从头开始写的,如果有什么不好的地方,请告诉我,我稍后会测试它。

      希望这个回答对你有帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-19
        相关资源
        最近更新 更多