android基本控件RadioButton/CheckBox
来源:互联网 发布:清理后台的软件 编辑:程序博客网 时间:2024/06/01 09:05
MainActivity.xml
<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;@Overrideprotected 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(){@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {// TODO Auto-generated method stubif(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(){@Overridepublic void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {// TODO Auto-generated method stubif(isChecked){Toast.makeText(MainActivity.this, "run", Toast.LENGTH_LONG).show();}}});write.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){@Overridepublic void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {// TODO Auto-generated method stubif(isChecked){Toast.makeText(MainActivity.this, "write", Toast.LENGTH_LONG).show();}}});swim.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){@Overridepublic void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {// TODO Auto-generated method stubif(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>
阅读全文
0 0
- android基本控件RadioButton/CheckBox
- Android-基本控件(CheckBox,RadioGroup,RadioButton)
- Android控件CheckBox、RadioButton
- android控件之radiobutton checkbox
- Android控件之CheckBox、RadioButton
- Android控件CheckBox和RadioButton
- Android控件RadioButton&RadioGroup&CheckBox
- android常用控件------checkBox RadioButton ImageView Spinner
- Android控件之CheckBox、RadioButton探究
- Android 基础控件之CheckBox与RadioButton
- Android控件之CheckBox、RadioGroup、RadioButton
- Android基础控件 - RadioButton与CheckBox
- Android控件中CheckBox、RadioButton详解
- Android控件之Checkbox和RadioButton
- 03 - 基本控件:RadioButton单选框、CheckBox复选框
- Android基本控件RadioGroup和RadioButton学习
- Android常见控件之RadioGroup,RadioButton,CheckBox和Toast
- Android[初级教程]第三篇 RadioButton和CheckBox控件
- dijkstra求次短路径
- java中int、String的类型转换
- opencv-视频读取
- ARP协议
- Matlab产生随机数函数rand和randn的“state”和“seed”参数
- android基本控件RadioButton/CheckBox
- 【每日一题】实现一个栈Stack,要求实现Push(出栈)、Pop(入栈)、 Min(返回最小值的操作)的时间复杂度为O(1)
- 数数
- 欢迎使用CSDN-markdown编辑器
- Mybatis(二)— 使用Mapper动态代理方式进行开发
- cache浅析
- 剑指offer面试题56 链表中环的入口节点(java实现)
- Qt On Android + protobuf3的使用
- session机制解析