Android中的常用控件

Android中的常用控件

一、程序目的

展示和使用Android中的各种常用控件,其中的TextView和Button由于,以前例子中经常用就不举例子。

TextView:文本框,相当于JavaGUI中的TextField

Button:按钮

二、例子效果图
Android中的常用控件Android中的常用控件


Android中的常用控件Android中的常用控件

 三、代码编写

1.CheckBox和RadioButton(RadioGroup)

  raido.xml,注意:RadioGroup包裹RadioButton

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<TextView android:id="@+id/textView1" android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:text="@string/hello" />
	<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/swim" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:text="@string/swim" />
	<CheckBox android:id="@+id/run" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:text="@string/run" />
	<CheckBox android:id="@+id/read" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:text="@string/read" />
</LinearLayout>

  对应Activity代码片段

//为RadioGroup设置监听器,需要注意的是,这里的监听器和Button控件的监听器有所不同
        genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if(femaleButton.getId() == checkedId){
					System.out.println("famale");
					Toast.makeText(Component.this, "famle", Toast.LENGTH_SHORT).show();//小提示框
				}
				else if(maleButton.getId() == checkedId)
				{
					System.out.println("male");
				}
			}
		});
        
        //为多选按钮添加监听器
        swimBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked)
				{
					System.out.println("swim is checked");
				}
				else
				{
					System.out.println("swim is unchecked");
				}
			}
		});

2.进度条ProgressBar

layout中的xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<!-- style:指定进度条的类型,其中progressBarStyleHorizontal 水平进度条
	progressBarStyle:旋转的进度条
	android:visibility:设置控件当前的可视情况:gone看不到
	-->
	<ProgressBar
	android:id="@+id/firstProgressBar"
		style="?android:attr/progressBarStyleHorizontal"
		android:layout_width="200dp"
		android:layout_height="wrap_content"
		android:visibility="gone"
	></ProgressBar>
	<ProgressBar
	android:id="@+id/secondProgressBar"
		style="?android:attr/progressBarStyle"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:visibility="gone"
		android:max="200"
	></ProgressBar>
	<Button
		android:id="@+id/myButton"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="@string/go"
	></Button>
</LinearLayout>
 

对应Activity代码片段

 

 class BtnListener implements OnClickListener{
    	
    	int i=0;

		@Override
		public void onClick(View arg0) {
			if(i==0){
				firstProgressBar.setVisibility(View.VISIBLE);
				secondProgressBar.setVisibility(View.VISIBLE);
			}else if(i<firstProgressBar.getMax()){
				firstProgressBar.setProgress(i);
				firstProgressBar.setSecondaryProgress(i+10);
				secondProgressBar.setProgress(i);
			}else{
				firstProgressBar.setVisibility(View.GONE);
				secondProgressBar.setVisibility(View.GONE);
			}
			
			i=i+10;
		}
    	
    }

3.ListView: 官方文档中的demo

layout中的xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="16sp" >
</TextView>

  对应的Activity

package linys.component;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class HelloListView extends ListActivity  {
	
	 static final String[] COUNTRIES = new String[] {
		    "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra",
		    "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina",
		    "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan",
		    "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium",
		    "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia",
		    "Bosnia and Herzegovina", "Botswana", "Bouvet Island", "Brazil", "British Indian Ocean Territory",
		    "British Virgin Islands", "Brunei", "Bulgaria", "Burkina Faso", "Burundi",
		    "Cote d'Ivoire", "Cambodia", "Cameroon", "Canada", "Cape Verde",
		    "Cayman Islands", "Central African Republic", "Chad", "Chile", "China",
		    "Christmas Island", "Cocos (Keeling) Islands", "Colombia", "Comoros", "Congo",
		    "Cook Islands", "Costa Rica", "Croatia", "Cuba", "Cyprus", "Czech Republic",
		    "Democratic Republic of the Congo", "Denmark", "Djibouti", "Dominica", "Dominican Republic",
		    "East Timor", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea",
		    "Estonia", "Ethiopia", "Faeroe Islands", "Falkland Islands", "Fiji", "Finland",
		    "Former Yugoslav Republic of Macedonia", "France", "French Guiana", "French Polynesia",
		    "French Southern Territories", "Gabon", "Georgia", "Germany", "Ghana", "Gibraltar",
		    "Greece", "Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guinea", "Guinea-Bissau",
		    "Guyana", "Haiti", "Heard Island and McDonald Islands", "Honduras", "Hong Kong", "Hungary",
		    "Iceland", "India", "Indonesia", "Iran", "Iraq", "Ireland", "Israel", "Italy", "Jamaica",
		    "Japan", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Kuwait", "Kyrgyzstan", "Laos",
		    "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", "Luxembourg",
		    "Macau", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands",
		    "Martinique", "Mauritania", "Mauritius", "Mayotte", "Mexico", "Micronesia", "Moldova",
		    "Monaco", "Mongolia", "Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia",
		    "Nauru", "Nepal", "Netherlands", "Netherlands Antilles", "New Caledonia", "New Zealand",
		    "Nicaragua", "Niger", "Nigeria", "Niue", "Norfolk Island", "North Korea", "Northern Marianas",
		    "Norway", "Oman", "Pakistan", "Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru",
		    "Philippines", "Pitcairn Islands", "Poland", "Portugal", "Puerto Rico", "Qatar",
		    "Reunion", "Romania", "Russia", "Rwanda", "Sqo Tome and Principe", "Saint Helena",
		    "Saint Kitts and Nevis", "Saint Lucia", "Saint Pierre and Miquelon",
		    "Saint Vincent and the Grenadines", "Samoa", "San Marino", "Saudi Arabia", "Senegal",
		    "Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands",
		    "Somalia", "South Africa", "South Georgia and the South Sandwich Islands", "South Korea",
		    "Spain", "Sri Lanka", "Sudan", "Suriname", "Svalbard and Jan Mayen", "Swaziland", "Sweden",
		    "Switzerland", "Syria", "Taiwan", "Tajikistan", "Tanzania", "Thailand", "The Bahamas",
		    "The Gambia", "Togo", "Tokelau", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey",
		    "Turkmenistan", "Turks and Caicos Islands", "Tuvalu", "Virgin Islands", "Uganda",
		    "Ukraine", "United Arab Emirates", "United Kingdom",
		    "United States", "United States Minor Outlying Islands", "Uruguay", "Uzbekistan",
		    "Vanuatu", "Vatican City", "Venezuela", "Vietnam", "Wallis and Futuna", "Western Sahara",
		    "Yemen", "Yugoslavia", "Zambia", "Zimbabwe"
		  };
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	  super.onCreate(savedInstanceState);

	  String[] countries = getResources().getStringArray(R.array.countries_array);
	//从values/strings.xml中取得定义的String数组
	  setListAdapter(new ArrayAdapter<String>(this, R.layout.listview, countries));
	  //使用代码中定义的String数组
//	  setListAdapter(new ArrayAdapter<String>(this, R.layout.listview, COUNTRIES));

	  ListView lv = getListView();
	  lv.setTextFilterEnabled(true);

	  //往ListView中添加项点击监听时间
	  lv.setOnItemClickListener(new OnItemClickListener() {
	    public void onItemClick(AdapterView<?> parent, View view,
	        int position, long id) {
	      // When clicked, show a toast with the TextView text
	      Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
	          Toast.LENGTH_SHORT).show();
	    }
	  });
	}
}
 

values/strings.xml片段

<string-array name="countries_array">
        <item>Bahrain</item>
        <item>Bangladesh</item>
        <item>Barbados</item>
        <item>Belarus</item>
        <item>Belgium</item>
        <item>Belize</item>
        <item>Benin</item>
    </string-array>

自定义的ListView例子

layout中的xml

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <ListView
     android:id="@id/android:list"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:scrollbars="vertical"
    >
    </ListView>
    <!--android:scrollbars 添加滚动条 -->
</LinearLayout>
 

user.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
   <TextView
   	 android:id="@+id/username"
     android:layout_width="180dip"
     android:layout_height="30dip"
   />
   <TextView
     android:id="@+id/password"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:gravity="right"
   />
   <!-- android:gravity 设置重心的位置在这里是把password放在username的右边二者在同一行 -->
</LinearLayout>
 

对应的Activity

package linys.views;

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

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.SimpleAdapter;
import android.widget.Toast;
/**
 * 
 * @Project: Android_ListView
 * @Desciption: 
 * 本Activity继承ListActivity
 * @Author: LinYiSong
 * @Date: 2011-3-25~2011-3-25
 */
public class ListView extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        /*
         * 
         *显示的数据
         *ListView可以看作是不显示字段名得一张表
         *字段名为map中的key
         *每行的数据用Map存放
         */
        
        ArrayList<Map<String,String>> list=new ArrayList<Map<String,String>>();
        //Map中存放的是ListView中每一行的值
        Map<String,String> map=new HashMap<String,String>();
        Map<String,String> map1=new HashMap<String,String>();
        map.put("username","linys");
        map.put("passwd", "linys");
        
        map1.put("username","wang");
        map1.put("passwd", "wang");
        
        list.add(map);
        list.add(map1);
        
        /**
         * 为ListView筛选值
         * R.layout.user指定的是在res/layout中指定的数据每行数据展示的布局
         * new String[]{"username","passwd"}:选取Map中的key,作为表格的字段
         * new int[]{R.id.username,R.id.password}:user与字段对应的控件名
         */
        SimpleAdapter adapter=new SimpleAdapter(this,list,R.layout.user,new String[]{"username","passwd"},new int[]{R.id.username,R.id.password});
        this.setListAdapter(adapter);
        
    }
    /**
     * override onListItemClick添加项点击事件
     */
	@Override
	protected void onListItemClick(android.widget.ListView l, View v,
			int position, long id) {
		super.onListItemClick(l, v, position, id);
		/*
		 * 从ListView的Adapter中取得所选的项
		 * CharSequence 相当于String
		 */
		CharSequence text = l.getAdapter().getItem(position)+"";
		int duration = Toast.LENGTH_SHORT;
		Toast toast = Toast.makeText(this, text, duration);
		toast.show();
	}
}
 

 

 

 

 

 

 

 

你可能感兴趣的:(xml,android,OS,Go)