单项选择相信都不陌生吧。Android平台也提供了单项选择的组件,可以通过RadioGroup、RadioButton组合起来完成一个 单项选择效果。我们仍然通过一个示例来说明该功能是如何实现的,
其实,要实现这样的效果并不复杂,从运行结果可以看出,一个单项选择由两部分组成,分别是前面的选择按钮和后而的文本。Android平台上的选择按钮可通过RadioButton来实现,而
文本则通过RadioGroup来实现。因此,首先要在布局文件中定义一个RadioGroup和2个RadioButton,在定义 RadioGroup时,己经将文本赋给了每个选项,在用户点击时来判断用户选择的是哪一项,需要设置 其事件监听setOnCheckedChangeListener,代码如下所示。
public class UITestActivity extends Activity implementsOnCheckedChangeListener{
RadioGrouprg=null;
Buttonaddbtn=null;
private staticfinal String TAG="TAG";
public voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.radio_layout);
findViews();
//指定某个选项被选中
rg.check(R.id.female);
//获取当前选项组中被选中的选项的id
intcheckedId=rg.getCheckedRadioButtonId();
RadioButtonrb=(RadioButton) this.findViewById(checkedId);
Log.i(TAG,rb.getText().toString());
}
private voidfindViews() {
rg=(RadioGroup)this.findViewById(R.id.sexRg);
//注册监听器
rg.setOnCheckedChangeListener(this);
addbtn=(Button)this.findViewById(R.id.appendRadio);
addbtn.setOnClickListener(newOnClickListener() {
public voidonClick(View v) {
//创建RadioButton对象
RadioButtonnewRb=new RadioButton(UITestActivity.this);
/*
* 为什么this做参数需要加RadioDemo前缀
* 因为这个方法存在与一个内部类中,所以直接this指向的是内部类的实例
* 此处需要有引用一个上下文对象的实例,而其外部类RadioDemo继承了Activity
* 的类,Activity又是继承了上下文Context的类,因此RadioDemo.this就是
* 这里所需要的上下文对象
*/
newRb.append("不明");
newRb.setId(100);
//添加到RadioGroup
rg.addView(newRb);
}
});
}
//覆盖接口的抽象方法
public voidonCheckedChanged(RadioGroup group, int checkedId) {
if(group.getId()==R.id.sexRg){
RadioButtonrb=(RadioButton) this.findViewById(checkedId);
Log.i(TAG,rb.getText().toString());
}
}
}
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" >
<ScrollView android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Radio Demo"
/>
<RadioGroup
android:id="@+id/sexRg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:checkedButton="@+id/male"
>
<RadioButton
android:id="@id/male"
android:text="男"
/>
<RadioButton
android:id="@+id/female"
android:text="女"
/>
</RadioGroup>
</LinearLayout>
</ScrollView>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="addRadioButton"
android:id="@+id/appendRadio"
/>
</LinearLayout>
CheckBox多选框
实现它的监听类
public class CheckBoxDemo extends Activity implementsOnCheckedChangeListener{
private CheckBoxcb1,cb2,cb3,cb4;
private Buttonbtn;
privateArrayList<CheckBox> list=new ArrayList<CheckBox>();
protected voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.checkbox_layout);
findViews();
}
private void findViews(){
cb1=(CheckBox)this.findViewById(R.id.cb1);
cb2=(CheckBox)this.findViewById(R.id.cb2);
cb3=(CheckBox)this.findViewById(R.id.cb3);
cb4=(CheckBox)this.findViewById(R.id.cb4);
list.add(cb1);
list.add(cb2);
list.add(cb3);
list.add(cb4);
for(CheckBoxcb:list){
//需要监听器对象,this是当前类实例,当前类实现了了监听器接口;所以this可以当作一个监听器对象放入其中
cb.setOnCheckedChangeListener(this);
}
btn=(Button)this.findViewById(R.id.submit);
btn.setOnClickListener(newOnClickListener() {
@Override
public voidonClick(View v) {
Stringfav="";
for(CheckBoxcb:list){
if(cb.isChecked()){
fav+=cb.getText()+", ";
}
}
Log.i("TAG",fav);
}
});
}
//覆盖CompoundButton.OnCheckChangeListener接口的抽象方法
public voidonCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.i("TAG",buttonView.getText().toString());
}
}
1. CheckBox 的 isChecked() 方法
publicabstract boolean isChecked ()
返回值:The current checked state of the view 。当前CheckBox的状态 True:选中 False:未选
2. CheckBox 的 setChecked() 方法
publicabstract void setChecked (boolean checked)
作用:修改 CheckBox 的状态
参数:The new checked state .
Spinner 下拉菜单
android给我们提供了一个spinner控件,这个控件主要就是一个列表,那么我们就来说说这个控件吧,这个控件在以前的也看见过,但今天还是从新介绍一遍吧。Spinner位于 android.widget包下,每次只显示用户选中的元素,当用户再次点击时,会弹出选择列表供用户选择,而选择列表中的元素同样来自适配器。Spinner是View类得一个子类。
public class SpinnerDemo extends Activity implementsOnItemSelectedListener{
SpinnersportSp=null;
protected voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinner_layout);
findViews();
}
private voidfindViews() {
sportSp=(Spinner)this.findViewById(R.id.sportsSp);
sportSp.setOnItemSelectedListener(this);
sportSp.performClick();
}
@Override
public voidonItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3){
TextViewtv=(TextView) arg1;
Log.i("TAG",tv.getText().toString());
}
public voidonNothingSelected(AdapterView<?> arg0) {
}
}
布局文件
<?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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="请选择一项运动项目"
/>
<Spinner
android:id="@+id/sportsSp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:prompt="@string/spinner_prompt"
android:entries="@array/sports"
/>
</LinearLayout>
定义文件
<?xml version="1.0"encoding="utf-8"?>
<resources>
<string-array name="sports">
<item>石家庄市</item>
<item>唐山市</item>
<item>秦皇岛市</item>
<item>张家口市</item>
<item>承德市</item>
<item>廊坊市</item>
<item>沧州市</item>
<item>保定市</item>
<item>邢台市</item>
<item>邯郸市</item>
<item>衡水市</item>
<item>北京市</item>
</string-array>
</resources>
ListView
列表的显示需要三个元素:
1.ListVeiw 用来展示列表的View。
2.适配器 用来把数据映射到ListView上的中介。
3.数据 具体的将被映射的字符串,图片,或者基本组件。
根据列表的适配器类型,列表分为三种,ArrayAdapter,SimpleAdapter和SimpleCursorAdapter
其中以ArrayAdapter最为简单,只能展示一行字。SimpleAdapter有最好的扩充性,可以自定义出各种效果。 SimpleCursorAdapter可以认为是SimpleAdapter对数据库的简单结合,可以方面的把数据库的内容以列表的形式展示出来。
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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="名单:"
/>
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/nameList"
/>
</LinearLayout>
public class ListViewDemo extends Activity implementsOnItemClickListener{
ListView nameList= null;
String[]names={"张三","李四","王五","赵六","猪八戒"};
protected voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_layout);
findViews();
}
private voidfindViews() {
nameList=(ListView)this.findViewById(R.id.nameList);
ArrayAdapteradapter=new ArrayAdapter(this,
android.R.layout.simple_expandable_list_item_1,names);
nameList.setAdapter(adapter);
nameList.setOnItemClickListener(this);
}
//覆盖监听器接口抽象方法
public voidonItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Log.i("TAG",names[arg2]+" position="+String.valueOf(arg2)+" row_id="+String.valueOf(arg3));
}
}
也可不写XML直接继承ListActivity
public class ListActivityDemo extends ListActivity {
String[] names={"张三","李四","王五","赵六","猪八戒"};
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayAdapteradapter = new ArrayAdapter(this,
android.R.layout.simple_expandable_list_item_1,names);
this.setListAdapter(adapter);
}
//覆盖父类方法,无需创建和注册监听器
protected void onListItemClick(ListView l, View v, int position,long id) {
super.onListItemClick(l,v, position, id);
Log.i("TAG",names[position]+" position="+String.valueOf(position)+" row_id="+String.valueOf(id));
}
}
总结:其实UI,都是些布局然后监听,去使用