1、RadioButton(单选按钮)
单选按钮,就是只能够选中一个,所以我们需要把RadioButton放到RadioGroup按钮组中,从而实现单选功能。先熟悉下如何使用RadioButton,一个简单的性别选择的例子:另外我们可以为外层RadioGroup设置orientation属性,然后设置RadioButton的排列方式,是竖直还是水平。<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#71C2F4" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="请选择性别" android:textColor="#000000" android:textSize="30sp"/> <RadioGroup android:id="@+id/radioGroup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:orientation="vertical"> <RadioButton android:id="@+id/btnMan" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="男" android:textSize="30sp"/> <RadioButton android:id="@+id/btnWoman" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="女" android:textSize="30sp"/> RadioGroup> <Button android:id="@+id/btnPost" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="提交" android:textSize="30sp"/>LinearLayout>
获取选中的值,方法有两种:
1、为 RadioButton 设置一个事件监听器 setOnCheckChangeListener
final RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radioGroup);radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton radioButton = (RadioButton)findViewById(checkedId); Toast.makeText(getApplicationContext(),"按钮组值发生改变,你选了"+radioButton.getText(),Toast.LENGTH_LONG).show(); }});
PS:另外有一点要切记,要为每个 RadioButton 添加一个id,不然单选功能不会生效。
2、通过单击其他按钮获取选中单选按钮的值,当然我们也可以直接获取,这个看需求
Button btnPost = (Button)findViewById(R.id.btnPost);final RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radioGroup);btnPost.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { for (int i = 0; i < radioGroup.getChildCount(); i++){ RadioButton rd = (RadioButton)radioGroup.getChildAt(i); if(rd.isChecked()){ Toast.makeText(getApplicationContext(),"按钮组值发生改变22,你选了"+rd.getText(),Toast.LENGTH_LONG).show(); break; } } }});
代码解析:这里我们为提交按钮设置了一个 setOnClickListener 事件监听器,每次点击的话遍历一次RadioButton判断哪个按钮被选中我们可以通过下述方法获得RadioButton的相关信息
getChildCount()获得按钮组中的单选按钮的数目
getChildAt(i)根据索引值获取我们的单选按钮
isChecked()判断按钮是否被选中
2、CheckBox(复选框)
如题复选框,即可以同时选中多个选项,至于获得选中的值,同样有两种方式:1. 为每个CheckBox添加事件:setOnCheckedChangeListener 2. 弄一个按钮,在点击后对每个CheckBox进行判断:isChecked()自定义点击效果
在drawable目录下,新建Drawable resource file,文件名称为:
checkbox.xml
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_enabled="true" android:state_checked="true" android:drawable="@mipmap/checked"/> <item android:state_enabled="true" android:state_checked="false" android:drawable="@mipmap/unchecked"/>selector>
写好后,我们有两种方法设置:
1. android:button属性设置为上述的selctor
android:button="@drawable/checkbox"
2. 在style中定义一个属性,然后通过android style属性设置,先往style添加下述代码:
<style name="MyCheckBox" parent="@android:style/Widget.CompoundButton.CheckBox"> <item name="android:button">@drawable/checkboxitem>style>
然后布局那里
style="@style/MyCheckBox"
布局文件 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:orientation="vertical" android:paddingLeft="10dp" android:background="#71C2F4"> <CheckBox android:id="@+id/cb_one" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="香蕉" android:textSize="30sp" android:background="@null" android:paddingLeft="20dp"/> <CheckBox android:id="@+id/cb_two" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="西瓜" android:textSize="30sp" style="@style/MyCheckBox" android:background="@null" android:paddingLeft="20dp"/> <CheckBox android:id="@+id/cb_three" android:layout_width="match_parent" android:layout_height="wrap_content" android:button="@drawable/checkbox" android:text="苹果" android:textSize="30sp" android:background="@null" android:paddingLeft="20dp"/> <Button android:id="@+id/btn_send" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="提交" android:textSize="30sp"/>LinearLayout>
CheckBoxActivity
public class CheckBoxActivity extends AppCompatActivity implements View.OnClickListener,CompoundButton.OnCheckedChangeListener{ private CheckBox cb_one; private CheckBox cb_two; private CheckBox cb_three; private Button btn_send; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.checkbox_test); cb_one = (CheckBox)findViewById(R.id.cb_one); cb_two = (CheckBox)findViewById(R.id.cb_two); cb_three = (CheckBox)findViewById(R.id.cb_three); btn_send = (Button)findViewById(R.id.btn_send); cb_one.setOnCheckedChangeListener(this); cb_two.setOnCheckedChangeListener(this); cb_three.setOnCheckedChangeListener(this); btn_send.setOnClickListener(this); } @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { String s = compoundButton.getText().toString(); if(compoundButton.isChecked()){ Toast.makeText(this,s,Toast.LENGTH_LONG).show(); } } @Override public void onClick(View view) { String choose = ""; if(cb_one.isChecked()) choose += cb_one.getText().toString()+" "; if(cb_two.isChecked()) choose += cb_two.getText().toString()+" "; if(cb_three.isChecked()) choose += cb_three.getText().toString()+" "; Toast.makeText(this,choose,Toast.LENGTH_LONG).show(); }}
修改文字与选择框的距离
android:background="@null"android:paddingLeft="20dp"
最终效果