Android_单选按钮(第一季重制版)
来源:互联网 发布:实体店怎么开淘宝 编辑:程序博客网 时间:2024/04/30 16:18
1.单选按钮(RadioButton)的基本概念
2.RadioGroup与RadioButton
3.OnClickListener与OnCheckedChangeListener监听器
eg):
<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=".MainActivity" > <RadioGroup android:id="@+id/radioGroupId" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:id="@+id/femaleButtonId" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="female"/> <RadioButton android:id="@+id/maleButtonId" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="male" /> </RadioGroup></LinearLayout>
package com.marschen.s01_e10_radio;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.widget.CompoundButton;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.RadioGroup.OnCheckedChangeListener;public class MainActivity extends Activity {private RadioGroup radioGroup;private RadioButton femaleButton; private RadioButton maleButton;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);radioGroup = (RadioGroup)findViewById(R.id.radioGroupId);femaleButton = (RadioButton)findViewById(R.id.femaleButtonId);maleButton = (RadioButton)findViewById(R.id.maleButtonId);RadioGroupListener listener = new RadioGroupListener();radioGroup.setOnCheckedChangeListener(listener);RadioButtonListener radioButtonListener = new RadioButtonListener();femaleButton.setOnCheckedChangeListener(radioButtonListener);}class RadioButtonListener implements android.widget.CompoundButton.OnCheckedChangeListener{@Overridepublic void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {System.out.println("isChecked--->" + isChecked);}}class RadioGroupListener implements OnCheckedChangeListener{@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {if(checkedId == femaleButton.getId()){System.out.println("选中了female");}else if(checkedId == maleButton.getId()){System.out.println("选中了male");}}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.activity_main, menu);return true;}}
0 0
- Android_单选按钮(第一季重制版)
- Android_多选按钮(checkBox) 第一季重制版
- Android_控件布局初步(一)第一季重制版
- Android_深入LinearLayout(第一季重制版)
- Android_相对布局(上)第一季重制版
- Android_相对布局(中)第一季重制版
- Android_相对布局(下)第一季重制版
- Android_时间与日期(第一季重制版)
- Android_长度单位与内外边距(第一季重制版)
- Android(第一季重制版)
- Android体系结构(第一季重制版)
- Android_ProgressBar(第一季重制版)
- Android_Activity初步(一)第一季重制版
- Android_View初步(一)第一季重制版
- Adroid_ImageView的使用方法(第一季重制版)
- Android_SeekBar和RatingBar(第一季重制版)
- 安卓开发重制版第一季
- openpyxl笔记 重制版
- 双重检查加锁单例模式
- Drools学习笔记3-开发环境搭建
- ArrayList和数组间的相互转换
- Drools学习笔记4-第一个例子
- Drools 6.0入门:时钟实例
- Android_单选按钮(第一季重制版)
- Minimax theorem
- Cannot open Eclipse Marketplace
- 设计模式_单例模式
- CodeForces 128A Statues 简单搜索
- Android Studio 安装和使用的一些问题
- python list 系列初识
- 设置input type=range滑块的CSS样式
- C++ 多重继承与虚继承