android基本控件RadioButton/CheckBox
2017-07-20 12:22
435 查看
MainActivity.xml
MainActivity.java
string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Activity_04</string>
<string name="hello_world">单选和多选</string>
<string name="female">female</string>
<string name="male">male</string>
<string name="swim">swim</string>
<string name="run">run</string>
<string name="write">write</string>
</resources>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="${relativePackage}.${activityClass}" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <RadioGroup android:id="@+id/radioGroup" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:orientation="vertical" > <RadioButton android:id="@+id/female" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/female" > </RadioButton> <RadioButton android:id="@+id/male" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/male" > </RadioButton> </RadioGroup> <CheckBox android:id="@+id/swim" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/swim" /> <CheckBox android:id="@+id/run" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/run" /> <CheckBox android:id="@+id/write" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/write" /> </LinearLayout>
MainActivity.java
import android.app.Activity; import android.os.Bundle; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Toast; public class MainActivity extends Activity { private RadioGroup radioGroup; private RadioButton female; private RadioButton male; private CheckBox run,swim,write; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); radioGroup=(RadioGroup)findViewById(R.id.radioGroup); female=(RadioButton)findViewById(R.id.female); male=(RadioButton)findViewById(R.id.male); run=(CheckBox)findViewById(R.id.run); swim=(CheckBox)findViewById(R.id.swim); write=(CheckBox)findViewById(R.id.write); /*监听器*/ radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){ @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // TODO Auto-generated method stub if(female.getId() == checkedId){ System.out.println("女生"); Toast.makeText(MainActivity.this, "女生", Toast.LENGTH_LONG).show(); } else if(male.getId()==checkedId){ System.out.println("男生"); Toast.makeText(MainActivity.this, "男生", Toast.LENGTH_LONG).show(); } } }); /*给每个checkBox都要加监听器*/ run.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){ @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if(isChecked){ Toast.makeText(MainActivity.this, "run", Toast.LENGTH_LONG).show(); } } }); write.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){ @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if(isChecked){ Toast.makeText(MainActivity.this, "write", Toast.LENGTH_LONG).show(); } } }); swim.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){ @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if(isChecked){ Toast.makeText(MainActivity.this, "swim", Toast.LENGTH_LONG).show(); } } }); } }
string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Activity_04</string>
<string name="hello_world">单选和多选</string>
<string name="female">female</string>
<string name="male">male</string>
<string name="swim">swim</string>
<string name="run">run</string>
<string name="write">write</string>
</resources>
相关文章推荐
- Android控件RadioButton&RadioGroup&CheckBox
- [android]控件CheckBox/RadioGroup/RadioButton的常用属性---单复选控件
- 使用 IntraWeb (17) - 基本控件之 TIWRadioButton、TIWRadioGroup、TIWCheckBox
- Android 按钮类控件大集锦:Button ToggleButton CheckBox RadioButton
- (转)android常用控件(二) RadioGroup RadioButton CheckBox Toast的使用
- Android-基本控件(CheckBox,RadioGroup,RadioButton)
- Android 按钮类控件大集锦:Button ToggleButton CheckBox RadioButton
- 系出名门Android(5) - 控件(View)之TextView, Button, ImageButton, ImageView, CheckBox, Ra
- 基本控件:checkbox,radio,日期选择器,时间选择器
- android开发学习笔记——稍复杂控件 RadioGroup,CheckBox,Toast
- 系出名门Android(5) - 控件(View)之TextView, Button, ImageButton, ImageView, CheckBox, RadioButton, AnalogClock, DigitalClock
- Android控件 - TextView、Button、EditText、CompoundButton、CheckBox简介
- Android控件系列之RadioButton&RadioGroup
- Android入门(5)——多状态按钮ToggleButton、多选按钮CheckBox、RadioGroup与RadioButtont按钮
- Android基础控件之Button的基本使用
- Android控件系列之RadioButton&RadioGroup
- Android2.1--UI基本控件<2>--Button
- 安卓初识基本控件_RadioGroup&RadioButton
- 一天一天学 windows phone 控件 之 RadioButton + CheckBox(十三)