Activity常用控件——09
来源:互联网 发布:sql do while的用法 编辑:程序博客网 时间:2024/04/30 23:12
RadioGroup
用于创建一组选中状态相互排斥的单选按钮组。RadioGroup和 RadioButton通常是放在一起使用的
RadioButton
单选按钮是一种双状态的按钮,可以选择或不选中。
CheckBox
CheckBox是有双状态按钮的复选框,可以选中也可 以不选中。
Toast
快速的返回少量信息,浮于程序上,不取得焦点
import java.util.ArrayList; 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; import android.widget.RadioGroup.OnCheckedChangeListener; import android.widget.TextView; public class RadioTest extends Activity { private ArrayList<String> sportList = new ArrayList<String>(); private TextView textView; private RadioGroup genderGroup=null; private RadioButton maleButton=null; private RadioButton femaleButton=null; private CheckBox runBox=null; private CheckBox swimBox=null; private CheckBox readBox=null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView)findViewById(R.id.textView); genderGroup = (RadioGroup)findViewById(R.id.genderGroup); maleButton = (RadioButton)findViewById(R.id.maleButton); femaleButton = (RadioButton)findViewById(R.id.femaleButton); run = (CheckBox)findViewById(R.id.run); swim = (CheckBox)findViewById(R.id.swim); read = (CheckBox)findViewById(R.id.read); run.setOnCheckedChangeListener(new OnChecked()); swim.setOnCheckedChangeListener(new OnChecked()); read.setOnCheckedChangeListener(new OnChecked()); genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { public void onCheckedChanged(RadioGroup group, int checkedId) { // TODO Auto-generated method stub if(femaleButton.getId() == checkedId){ gender = "female"; } else if(maleButton.getId() == checkedId){ gender = "male"; } } }); } class OnChecked implements CompoundButton.OnCheckedChangeListener{ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub String str = ""; if(buttonView == run){ str = "runBox"; } else if(buttonView == swim){ str = "swimBox"; } else if(buttonView == read){ str = "readBox"; } if(isChecked){ if(!sportList.contains(str)){ sportList.add(str); } } else if(!isChecked){ if(sportList.contains(str)){ sportList.remove(str); } } String text ="gender: " + gender + " sport: "; for(String s : sportList){ text += s + " "; } textView.setText(text); Toast.makeText(RadioTest.this, text, Toast.LENGTH_LONG).show(); } } }
配置文件
<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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.activity_06.RadioTest" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <RadioGroup android:id="@+id/genderGroup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <RadioButton android:id="@+id/femaleButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/female" /> <RadioButton android:id="@+id/maleButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/male" /> </RadioGroup> <CheckBox android:id="@+id/swimBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/swimBox" /> <CheckBox android:id="@+id/readBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/readBox" /> <CheckBox android:id="@+id/runBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/runBox" /></LinearLayout>
0 0
- Activity常用控件——09
- Activity--常用控件
- Activity常用控件
- Activity常用控件二——RadioGroup、RadioButton、CheckBox的使用
- 常用控件—UILabel
- Activity常见控件三——progressBar
- android——常用控件
- Android常用控件—WebView
- Android开发学习笔记-4 Activity-3 常用控件
- Android常见控件使用经验之——Activity
- winform控件学习(1)—常用控件
- Android—常用控件(一)文本控件
- Android—常用控件(四)日期/时间控件
- MFC 常用控件使用 —— CListCtrl
- 常用控件——adapter listview spinner
- Android常用控件——10
- PyQt学习笔记——常用控件
- webAPP常用控件之——swiper
- 简单插入排序
- 软件工程——整合与感悟
- JavaScript——BOM(浏览器对象模型),时间间隔和暂停
- Android Studio Gradle相关异常记录
- HDU 1060 Leftmost Digit
- Activity常用控件——09
- hdu-1166
- UITableView Controller报错解决方法
- 【Cocos2d-x3.0学习笔记 07】游戏时间到LitterRunner02
- 程序员面试宝典递归的求解字符串长度
- 理解事务的隔离性(转载)
- 面试经历
- think in java读书笔记--字符串小细节
- 在现有Windows电脑安装Ubuntu 10.04 LTS记录