今天继续学习UI组件,CheckBox、Radio、ListView、Spinner
接下来通过实例来了解这几个组件的具体应用:
首先是Radio,也即是添加互斥选项(即单选)组件,
先在radio_layout.xml布局文件中,定义两个Radio组件:
<?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="fill_parent"
android:layout_height="wrap_content"
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/female">
<RadioButton
android:id="@id/female"
android:text="女"/>
<RadioButton
android:id="@+id/male"
android:text="男"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="addRadioButton"
android:id="@+id/appendRadio"/>
</RadioGroup>
</LinearLayout>
</ScrollView>
</LinearLayout>
这里<ScrollView>为下拉滚动条,其子件只能有一个,所以其中的各个标签用<LinearLayout>标签所包裹。
然后在UITest3Activity.java中调用其布局文件,同时实现监听:
package cn.class3g.activity;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class UITest3Activity extends Activity implements OnCheckedChangeListener{
RadioGroup rg = null;
Button addBtn = null;
private static final String TAG = "TAG";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.radio_layout);
findViews();
//指定某个被选中的选项
rg.check(R.id.male);
//获取当前选项组中被选中的选项id
int checkedId = rg.getCheckedRadioButtonId();
RadioButton rb = (RadioButton) this.findViewById(checkedId);
Log.i(TAG,rb.getText().toString());
}
private void findViews() {
rg = (RadioGroup) this.findViewById(R.id.sexRg);
//注册监听器
rg.setOnCheckedChangeListener(this);
addBtn = (Button) this.findViewById(R.id.appendRadio);
addBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//创建RadioButton对象
/*
* 这个方法存在与一个内部类中,所以直接this指向的是内部类的实例
* 此处需要引用一个上下文对象的实例,而其外部类所以加前缀之前其外部类UITest3Activity继承了Activity的类,
* Activity有继承了上下文Context的类,因此UITest3Activity.this
* 就是这里的上下文对象
*
* */
RadioButton newRb = new RadioButton(UITest3Activity.this);
newRb.append("不明");
newRb.setId(3);
//添加到RedioGroup中
rg.addView(newRb);
}
});
}
//覆盖OnCheckedChangeListener接口的抽象方法
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(group.getId() == R.id.sexRg){
RadioButton rb = (RadioButton) this.findViewById(checkedId);
Log.i(TAG,rb.getText().toString());
}
}
}
启动虚拟机:
这里添加了一个button,用于添加Radio组件。
接下来来了解CheckBox,也即添加复选项组件
首先,建立checkbox_layout.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="爱好"/>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:id="@+id/checkbox_layout" >
<TableRow>
<CheckBox
android:id="@+id/cb1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="足球" />
<CheckBox
android:id="@+id/cb2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="篮球" />
</TableRow>
<TableRow>
<CheckBox
android:id="@+id/cb3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="游泳" />
<CheckBox
android:id="@+id/cb4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="武术" />
</TableRow>
</TableLayout>
<Button
android:id="@+id/submitBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提交"/>
</LinearLayout>
然后在CheckBoxDemo.java中调用其布局,实现监听:
package cn.class3g.activity;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class CheckBoxDemo extends Activity implements OnCheckedChangeListener{
private CheckBox cb1,cb2,cb3,cb4;
private Button submitBtn = null;
private ArrayList<CheckBox> list = new ArrayList<CheckBox>();
@Override
protected void onCreate(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(CheckBox cb:list){
/*当前类实现了监听器接口,所以当前类的实例this可以
* 当作一个监听器 对象放入setOnCheckedChangeListener()
* 方法之中做参数
*/
cb.setOnCheckedChangeListener(this);
}
submitBtn = (Button) this.findViewById(R.id.submitBtn);
submitBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String fav = "";
for(CheckBox cb:list){
if(cb.isChecked()){
fav += cb.getText();
}
}
Log.i("TAG",fav);
}
});
}
//覆盖方法
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.i("TAG",buttonView.getText().toString());
}
}
启动虚拟机:
那么,接下来ListView也即列表组件
同样的先建立list_layout.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>
然后是ListViewDemo.java中添加显示内容:
package cn.class3g.activity;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ListViewDemo extends Activity implements OnItemClickListener{
ListView nameList = null;
String[] names = {"张三","李四","王五","宋留","八戒"};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.list_layout);
findViews();
}
private void findViews() {
nameList = (ListView) this.findViewById(R.id.nameList);
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,names);
nameList.setAdapter(adapter);
nameList.setOnItemClickListener(this);
}
//覆盖监听器接口OnItemClickListener的抽象方法
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Log.i("TAG",names[arg2] + "postion= "
+ String.valueOf(arg3) +" "+ "row_id=" + String.valueOf(arg3));
}
}
启动虚拟机:
最后,来了解Spinner也即下拉选项组件
首先,创建spinner.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="请选择一项运动项目"/>
<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>
然后在SpinnerDemo.java文件中实现如下代码:
package cn.class3g.activity;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.TextView;
public class SpinnerDemo extends Activity implements OnItemSelectedListener{
Spinner sportsSp = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.spinner_layout);
findViews();
}
private void findViews() {
sportsSp = (Spinner) this.findViewById(R.id.sportsSp);
sportsSp.setOnItemSelectedListener(this);
//sportsSp.performClick();//直接显示下拉选项
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
TextView tv = (TextView) arg1;
Log.i("TAG",tv.getText().toString());
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
}
启动虚拟机:
点击出现下拉选项: