今天,咱们的主题是基础控件RadioButton。在开始以前,咱们仍是以官方文档为开端来开始咱们的讲解,下面是Android文档中对RadioButton的简介:android
看过上一篇文章的应该能够了解到,这个和咱们的CheckBox是十分相似的,不一样的点在于,这个控件能够由非选中状态经过点击事件转为选中状态,可是不能经过点击实现逆向的状态转换,一个默认样式RadioButton控件的非选中和选中状态以下:程序员
其组成和CheckBox同样,咱们一样能够分别对其中的字体和Button进行设置,实现达到和CheckBox同样的效果。在上面咱们在简介中得知,这个控件能经过点击事件实现的效果以下(不能逆向改变状态):app
接下来,咱们对其基本属性进行设置,改变一下它的样式:ide
下面咱们就结合一个小例子来实际的应用一下,这个小例子就是实现多项单选功能,运行的效果以下:布局
布局文件与控制逻辑以下:字体
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="aoto.com.commonwidgetandlayout.basic_widget.radioButton.RadioButtonActivity"> <TextView android:layout_marginTop="20dp" android:textSize="20sp" android:text="请选择您最爱的职业:" android:layout_width="match_parent" android:layout_height="wrap_content" /> <RadioButton android:textSize="25sp" android:id="@+id/radio_button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/colorPrimaryDark" android:layout_marginStart="16dp" android:layout_marginTop="20dp" android:text="程序员" /> <RadioButton android:textSize="25sp" android:id="@+id/radio_button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/colorPrimaryDark" android:layout_marginStart="16dp" android:layout_marginTop="11dp" android:text="政治家" /> <RadioButton android:textSize="25sp" android:id="@+id/radio_button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/colorPrimaryDark" android:layout_marginStart="16dp" android:layout_marginTop="11dp" android:text="富二代" /> </LinearLayout>
package aoto.com.commonwidgetandlayout.basic_widget.radioButton; import android.graphics.Color; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.widget.CompoundButton; import android.widget.RadioButton; import aoto.com.commonwidgetandlayout.R; /** * @author why * @date 2019-5-13 20:44:28 */ public class RadioButtonActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener { private static final String TAG = "RadioButtonActivityWhy"; RadioButton radioButton1; RadioButton radioButton2; RadioButton radioButton3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_radio_button); radioButton1=findViewById(R.id.radio_button1); radioButton2=findViewById(R.id.radio_button2); radioButton3=findViewById(R.id.radio_button3); radioButton1.setOnCheckedChangeListener(this); radioButton2.setOnCheckedChangeListener(this); radioButton3.setOnCheckedChangeListener(this); } @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked){ disableOthers(buttonView.getId()); Log.e(TAG, "您最喜欢的职业是: "+buttonView.getText().toString() ); buttonView.setTextColor(getResources().getColor(R.color.colorPrimary)); } else { buttonView.setTextColor(Color.BLACK); } } private void disableOthers(int viewId) { if(R.id.radio_button1!=viewId&&radioButton1.isChecked()){ radioButton1.setChecked(false); } if(R.id.radio_button2!=viewId&&radioButton2.isChecked()){ radioButton2.setChecked(false); } if(R.id.radio_button3!=viewId&&radioButton3.isChecked()){ radioButton3.setChecked(false); } } }
可见,咱们为了实现一个单选功能作了不少逻辑控制,而这样的场景又很是多,没有关系,咱们接着官方文档关于对其的介绍继续向下看:this
* <p> * Radio buttons are normally used together in a * {@link android.widget.RadioGroup}. When several radio buttons live inside * a radio group, checking one radio button unchecks all the others.</p>
说这个RadioButton常常会结合RadioGroup一块儿使用,实现的功能正是咱们上面所要实现的多项单选功能的操做。那下面就来看看如何使用RadioGroup实现上述例子的功能:3d
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="aoto.com.commonwidgetandlayout.basic_widget.radioButton.RadioButton2Activity"> <TextView android:layout_marginLeft="40dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="请选择您最爱的职业:" android:textSize="20sp" /> <RadioGroup android:id="@+id/job_list" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal"> <RadioButton android:id="@+id/radio_button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginTop="20dp" android:text="程序员" android:textColor="@color/colorPrimaryDark" android:textSize="25sp" /> <RadioButton android:id="@+id/radio_button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginTop="11dp" android:text="政治家" android:textColor="@color/colorPrimaryDark" android:textSize="25sp" /> <RadioButton android:id="@+id/radio_button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginTop="11dp" android:text="富二代" android:textColor="@color/colorPrimaryDark" android:textSize="25sp" /> </RadioGroup> <Button android:id="@+id/clear_all" android:text="都不喜欢" android:onClick="clearAll" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
说这个RadioButton常常会结合RadioGroup一块儿使用,实现的功能正是咱们上面所要实现的多项单选功能的操做。那下面就来看看如何使用RadioGroup实现上述例子的功能:code
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="aoto.com.commonwidgetandlayout.basic_widget.radioButton.RadioButton2Activity"> <TextView android:layout_marginLeft="40dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="请选择您最爱的职业:" android:textSize="20sp" /> <RadioGroup android:id="@+id/job_list" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal"> <RadioButton android:id="@+id/radio_button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginTop="20dp" android:text="程序员" android:textColor="@color/colorPrimaryDark" android:textSize="25sp" /> <RadioButton android:id="@+id/radio_button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginTop="11dp" android:text="政治家" android:textColor="@color/colorPrimaryDark" android:textSize="25sp" /> <RadioButton android:id="@+id/radio_button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginTop="11dp" android:text="富二代" android:textColor="@color/colorPrimaryDark" android:textSize="25sp" /> </RadioGroup> <Button android:id="@+id/clear_all" android:text="都不喜欢" android:onClick="clearAll" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
逻辑部分:orm
package aoto.com.commonwidgetandlayout.basic_widget.radioButton; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.RadioButton; import android.widget.RadioGroup; import aoto.com.commonwidgetandlayout.R; /** * @author why * @date 2019-5-13 21:43:42 */ public class RadioButton2Activity extends AppCompatActivity { private static final String TAG = "RadioButton2ActivityWhy"; RadioGroup radioGroup; RadioButton radioButton1; RadioButton radioButton2; RadioButton radioButton3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_radio_button2); radioGroup=findViewById(R.id.job_list); radioButton1=findViewById(R.id.radio_button1); radioButton2=findViewById(R.id.radio_button2); radioButton3=findViewById(R.id.radio_button3); radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { getYourFavorite(checkedId); } }); } /** * 根据ID,执行相应的逻辑 * @param buttonId */ private void getYourFavorite(int buttonId){ switch (buttonId){ case R.id.radio_button1: if(radioButton1.isChecked()) { Log.e(TAG, "你最爱的职业是: " + radioButton1.getText().toString()); } break; case R.id.radio_button2: if(radioButton2.isChecked()) { Log.e(TAG, "你最爱的职业是: " + radioButton2.getText().toString()); } break; case R.id.radio_button3: if(radioButton3.isChecked()) { Log.e(TAG, "你最爱的职业是: " + radioButton3.getText().toString()); } break; } } /** * 清除选型 * @param view */ public void clearAll(View view){ radioGroup.clearCheck(); } }
在布局部分,咱们只须要把以前放置在布局中的RadioButton放置在RadioGroup中便可:
<RadioGroup android:id="@+id/job_list" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal"> <RadioButton android:id="@+id/radio_button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginTop="20dp" android:text="程序员" android:textColor="@color/colorPrimaryDark" android:textSize="25sp" /> <RadioButton android:id="@+id/radio_button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginTop="11dp" android:text="政治家" android:textColor="@color/colorPrimaryDark" android:textSize="25sp" /> <RadioButton android:id="@+id/radio_button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginTop="11dp" android:text="富二代" android:textColor="@color/colorPrimaryDark" android:textSize="25sp" /> </RadioGroup>
逻辑部分咱们首先为RadioGroup设置状态变化监听:
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { getYourFavorite(checkedId); } });
根据选择的RadioButton的ID执行具体的逻辑代码:
/** * 根据ID,执行相应的逻辑 * @param buttonId */ private void getYourFavorite(int buttonId){ switch (buttonId){ case R.id.radio_button1: if(radioButton1.isChecked()) { Log.e(TAG, "你最爱的职业是: " + radioButton1.getText().toString()); } break; case R.id.radio_button2: if(radioButton2.isChecked()) { Log.e(TAG, "你最爱的职业是: " + radioButton2.getText().toString()); } break; case R.id.radio_button3: if(radioButton3.isChecked()) { Log.e(TAG, "你最爱的职业是: " + radioButton3.getText().toString()); } break; } }
注意到在这里咱们只实现了数据的获取(RadioButton的文本内容),RadioGroup中的RadioButton之间的状态管理(单选)是RadioGroup内部本身管理的,这为咱们节省不少的开发逻辑,也是咱们用它的主要目的。此外,这里,咱们还能够经过调用clearCheck()实现清除选择状态。
/** * 根据ID,执行相应的逻辑 * @param buttonId */ private void getYourFavorite(int buttonId){ switch (buttonId){ case R.id.radio_button1: if(radioButton1.isChecked()) { Log.e(TAG, "你最爱的职业是: " + radioButton1.getText().toString()); } break; case R.id.radio_button2: if(radioButton2.isChecked()) { Log.e(TAG, "你最爱的职业是: " + radioButton2.getText().toString()); } break; case R.id.radio_button3: if(radioButton3.isChecked()) { Log.e(TAG, "你最爱的职业是: " + radioButton3.getText().toString()); } break; } }
注意到在这里咱们只实现了数据的获取(RadioButton的文本内容),RadioGroup中的RadioButton之间的状态管理(单选)是RadioGroup内部本身管理的,这为咱们节省不少的开发逻辑,也是咱们用它的主要目的。此外,这里,咱们还能够经过调用clearCheck()实现清除选择状态。
radioGroup.clearCheck()
运行结果以下所示:
一样,若是你以为RadioButton中的Button样式很差看,你能够自定义一种,这里,咱们仍是选用上一篇中的样式代码,执行效果以下:
修改按钮样式是经过android:button属性:
<RadioButton android:id="@+id/radio_button1" android:button="@drawable/check_box_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginTop="20dp" android:text="程序员" android:textColor="@color/colorPrimaryDark" android:textSize="25sp" />
其中的check_box_back.xml代码以下:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true" android:drawable="@drawable/chosen"></item> <item android:state_checked="false" android:drawable="@drawable/non_chosen_big"></item> </selector>
该控件的开源项目在网上找了一下,感受没有什么比较好的,主要是由于它的封装程度已经很高了,若是只是想改动一下显示样式和逻辑,咱们本身彻底能够实现。好了,关于RadioButton到这里的简单介绍就介绍了。