Android 应用软件开发(九)控件续

分享到:

TextView

EditBox

Button

RadioGroup

RadioBox

CheckBox

ProgressBar

ListView

下面是关于RadioBox和CheckBox的用法

注意控件使用的顺序:

1. 什么控件变量

2. 通过控件的ID获取控件句柄

3. 绑定监听器(注意RadioGroup是使用自己定义的内部类而CheckBox是使用公用的CompoundButton的公用类)

<RadioGroup 
    	android:id="@+id/genderGroup"
    	android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:layout_below="@+id/cancel"
    	android:orientation="vertical">
    	<RadioButton 
    		android:id="@+id/maleButton"
    		android:layout_width="wrap_content"
    		android:layout_height="wrap_content"
    		android:text="@string/male"
    	/>
    	<RadioButton 
    		android:id="@+id/femaleButton"
    		android:layout_width="wrap_content"
    		android:layout_height="wrap_content"
    		android:text="@string/female"
    	/>
</RadioGroup>
<CheckBox 
    	android:id="@+id/swim"
    	android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:layout_below="@+id/genderGroup"
    	android:text="swim"
/>
<ProgressBar 
    android:id="@+id/firstBar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/swim"
    android:max="200"
    android:visibility="gone"
/>
<ProgressBar 
    android:id="@+id/secondBar"
    style="?android:attr/progressBarStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/firstBar"
    android:visibility="gone"
/>
<Button 
    android:id="@+id/beginButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/secondBar"
    android:text="begin"
/>
genderGroup = (RadioGroup)findViewById(R.id.genderGroup);
femaleButton = (RadioButton)findViewById(R.id.femaleButton);
maleButton = (RadioButton)findViewById(R.id.maleButton);
swim = (CheckBox)findViewById(R.id.swim);
        
genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				if(femaleButton.getId()==checkedId)
				{
					System.out.println("female");
					Toast.makeText(TableTestActivity.this,"female",Toast.LENGTH_SHORT).show();
				}
				else if(maleButton.getId()==checkedId)
				{
					System.out.println("male");
				}
			}
		});
swim.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				if(isChecked)
					System.out.println("swim button has been selected");
				else
					System.out.println("swim button has not been selected");
			}
		});
class beginListener implements OnClickListener{

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			if(0==i)
			{
				firstBar.setProgress(i);
				//firstBar.setSecondaryProgress(i+2);
				secondBar.setProgress(i);
				firstBar.setVisibility(View.VISIBLE);
				secondBar.setVisibility(View.VISIBLE);
			}
			else if(i<200)
			{
				firstBar.setProgress(i);
				//firstBar.setSecondaryProgress(i+2);
				secondBar.setProgress(i);
			}
			else
			{
				firstBar.setVisibility(View.INVISIBLE);
				secondBar.setVisibility(View.INVISIBLE);
				i=-10;
			}
			i=i+10;
		}
}

ListView控件的用法比较特殊

1. 需要至少两个布局文件,一个用于窗口的整体布局,一个用于ListView内部的布局

2. 其Activity需要继承自ListActivity

见下例

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <ListView
        android:id="@+id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="false"
        android:scrollbars="vertical"/>
</LinearLayout>

user.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:paddingLeft="1dip"
    android:paddingRight="10dip"
    android:paddingTop="1dip"
    android:paddingBottom="1dip">
    <TextView
        android:id="@+id/user_name"
        android:layout_width="180dip"
        android:layout_height="30dip"
        android:textSize="10pt"
        android:singleLine="true"/>
    <TextView
        android:id="@+id/user_id"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:textSize="10pt"
        android:gravity="right"/>
</LinearLayout>

Activity05.java:

package my.activity;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class Activity05 extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ArrayList<HashMap<String,String>> list= new ArrayList<HashMap<String,String>>();
        HashMap<String,String> map1=new HashMap<String,String>();
        HashMap<String,String> map2=new HashMap<String,String>();
        HashMap<String,String> map3=new HashMap<String,String>();
        map1.put("user_name", "zhangsan");
        map1.put("user_id", "192.168.0.1");
        map2.put("user_name", "lisi");
        map2.put("user_id", "192.168.0.2");
        map3.put("user_name", "wangwu");
        map3.put("user_id", "192.168.0.3");
        list.add(map1);
        list.add(map2);
        list.add(map3);
        SimpleAdapter listAdapter = new SimpleAdapter(this, list, 
        		R.layout.user, new String[]{"user_name","user_id"}, 
        		new int[]{R.id.user_name,R.id.user_id});
        setListAdapter(listAdapter);
    }

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		System.out.println("position~~~~~~~~~~"+position);
		System.out.println("id~~~~~~~~~~~~~~~~"+id);
	}
}
昵    称:
验证码:

相关文档: