Android成长的故事——Android常用UI组件1_RadioButton&&CheckBox&&ListView&&Spinner

今天介绍一下Android中常用的几种组件,都是Android中经常用的组件。

       首先是单选框(RadioButton):

       利用RadioGroup 进行分组,在RadioGroup 内定义若该RadioButton 选项。

要完成单选框显示,我们需要使用到RadioGroup 和RadioButton(单选框),RadioGroup

用于对单选框进行分组,相同组内的单选框只有一个单选框能被选中。常用方法如下:

RadioGroup.check(intid);将指定的RadioButton 设置成选中状态。

(RadioButton)findViewById(radioGroup.getCheckedRadioButtonId()); 获取被

选中的单选框。

RadioButton.getText();获取单选框的值

调用setOnCheckedChangeListener() 方法, 处理单选框被选择事件, 把RadioGroup.OnCheckedChangeListener 实例作为参数传入。

具体应用见后面综合示例。

在布局文件中的应用示例如下:

<?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="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello"
	    />
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/sex"
        />
     <RadioGroup
                android:id="@+id/sexRg"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:checkedButton="@+id/female"
                android:orientation="vertical" >

                <RadioButton
                    android:id="@+id/male"
                    android:text="男" />
                
                 <RadioButton
                    android:id="@id/female"
                    android:text="女" />
                
            </RadioGroup>
	</LinearLayout>

效果图:


说明:

android:checkedButton="@+id/woman"指定id为woman的RadioButton为默认选定项。注

意“@”后有“+”;

android:id="@id/woman" />

被指定为默认选定的RadioButton 的id 不用“+”,直接引用前面定义过的标识即可。

运行效果如图:

“男”和“女”两个选项只能选其一。

 

可以在虚拟机中测试一下效果,利用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 RadioDemo 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();
	
	}
	Private void findViews(){
	rg = (RadioGroup) this.findViewById(R.id.sexRg);
	rg.setOnCheckedChangeListener(this);
}

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());
		}
	}
}

点击两个选项,日志中会有如下显示:

多选框(CheckBox)

Android 平台为我们提供了多选框的实现方法,每个多选框都是独立的,可以通过迭代

所有多选框,然后根据其状态是否被选中再获取其值。CheckBox 类常用方法: CheckBox.setChecked(true);将CheckBox 设置成选中状态。

CheckBox.getText();获取多选框的值

CheckBox.isChecked();判断该选项是否被选中

调用setOnCheckedChangeListener()方法,处理多选框被选择事件,把

CompoundButton.OnCheckedChangeListener 实例作为参数传入,下面是例子:

首先是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="爱好"
        android:textSize="20dp" />

    <TableLayout
        android:id="@+id/tableLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="*" >

        <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>

</LinearLayout>

效果图:

在日志中查看的代码如下:

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;
	
	private ArrayList<CheckBox> list = new ArrayList<CheckBox>();
	
	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){
		
			cb.setOnCheckedChangeListener(this);
		}
		
	}

	//覆盖CompoundButton.OnCheckedChangeListener接口的抽象方法
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		Log.i("TAG", buttonView.getText().toString());		
	}
	
	
}

在日志中显示如图:



列表显示(ListView)

ListView 类为AdapterView 的间接子类,可以列表的形式显示数据,至于显示什么数

据以及如何显示数据则需要Adapter 类及其子类的配合了。数据来源可以有多种渠道,因此我们的数据可以来

自自定义的数组、List、数据库、内容提供者(关于从数据库、内容提供者的知识后面会详

细介绍)。至于指定数据在ListView 中的显示方式,我们可以先定义一组元素的预期布局文

件,之后,所有的数据项将按此格式显示。

举例:

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>

效果如图:


在java中写入数据代码如下:

import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ListActivityDemo extends ListActivity {

	String[] names = {"张三","李四","王五","宋六","猪八"};
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		ArrayAdapter adapter = new ArrayAdapter(this, 
				android.R.layout.simple_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) );
	}

	
}

下拉列表框(Spinner)

手机的屏幕较小,因此使用下拉列表来进行选择式输入是一个非常好的方式。Spinner

与ListView 一样,也是AdapterView的一个间接子类,是一个显示数据的窗口。

Spinner 类常用的方法如下:

Spinner.getItemAtPosition(Spinner.getSelectedItemPosition());获取下拉列

表框的值

调用setOnItemSelectedListener() 方法,处理下拉列表框被选择事件, 把

AdapterView.OnItemSelectedListener 实例作为参数传入

可以在Java 代码中通过Adapter绑定数据,也可以在布局文件中直接引用在资源文件

中定义的数组。

编写arrays.xml,定义Spinner 中需要显示的数据。

<?xml version="1.0" encoding="utf-8"?>
<resources>
	<string-array name="sports" >
	    <item>足球</item>
	    <item>篮球</item>
	    <item>乒乓球</item>
	    <item>网球</item>	    
	</string-array>
</resources>
写main.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>

效果如图:


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.OnItemSelectedListener;
import android.widget.Spinner;
import android.widget.TextView;
 
public class SpinnerDemo extends Activity implementsOnItemSelectedListener{
 
    Spinner sportSp= 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,
           longarg3) {
       TextView tv =(TextView) arg1;
       Log.i("TAG",tv.getText().toString());
      
    }
 
    @Override
    public voidonNothingSelected(AdapterView<?> arg0) {
       // TODOAuto-generated method stub
      
    }
 
}

效果如图一:


图二;


2011年12月14日

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值