Android学习笔记 - 控件篇

来源:互联网 发布:加速兔网络加速器 编辑:程序博客网 时间:2024/05/28 03:02
1.控件常用的属性
android:id  //控件ID
android:text  //控件显示的文本
android:grivity  //控件内容位置 top bottom left right center_vertical
android:textSize  //字体大小
android:background  //背景颜色(RGB,如:#aa0000)
android:width  //控件宽度
android:height  //控件高度
android:padding  //控件内边距大小
android:paddingLeft
android:paddingRight
android:paddingTop
android:paddingBottom
android:sigleLine  //单行模式(true / false)
android:layout_weight  //占容器大小权重(比例)

2.TextView

文本浏览控件,相当于Label,用于显示文本信息

android:id:控件ID
android:text:文本控件中的内容

3.Button:按钮
android:id:控件ID(@+id/myTextView)
android:layout_width:fill_parent
android:layout_height:wrap_content


4.EditText:相当于TextBox,用于编辑文本信息
获取文本框中的内容:txt.getText().toString();


5.Menu:菜单,点击菜单键后弹出的选项菜单

[java] view plaincopy
  1. @Override  
  2. public boolean onCreateOptionsMenu(Menu menu) {  
  3.     menu.add(011, R.string.exit); //组ID,项ID,排序ID,文字  
  4.     menu.add(022, R.string.about);  
  5.     return super.onCreateOptionsMenu(menu);  
  6. }  
  7. @Override  
  8. public boolean onOptionsItemSelected(MenuItem item) {  
  9.     if(item.getItemId() == 1){  
  10.         finish(); //表示结束应用程序  
  11.     }  
  12.     return super.onOptionsItemSelected(item);  
  13. }  

 


6.单选框:RadioGroup / RadioButton
一个RadioGroup包括多个RadioButton,用于分组,与HTML一样

[java] view plaincopy
  1. rgroupSex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {  
  2.     public void onCheckedChanged(RadioGroup group, int checkedId) {  
  3.         if (rbtnMale.getId() == checkedId) {  
  4.             Toast.makeText(Control1Activity.this"亲,你是男的喔!", Toast.LENGTH_SHORT).show();  
  5.         }  
  6.     }  
  7. });  




7.复选框:CheckBox

[java] view plaincopy
  1. chkRead.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {  
  2.     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {  
  3.         if(isChecked){  
  4.             Toast.makeText(Control1Activity.this"亲,我也喜欢读书哈~", Toast.LENGTH_SHORT).show();  
  5.         }  
  6.     }  
  7. });  




8.提示信息:Toast
一种消息提示的方式,会在屏幕下方中间显示出一段小提示文本

[java] view plaincopy
  1. Toast.makeText(Control1Activity.this"亲,我也喜欢读书哈~", Toast.LENGTH_SHORT).show();  


9.进度条:ProgressBar
//相关属性
style="?android:attr/progressBarStyleHorizontal" //水平进度条
style="?android:attr/progressBarStyle" //圆形进度图,无法显示进行的状态(百分比)
android:max="200" //进度条最大值,默认为100
android:visibility="gone" //不可见

[java] view plaincopy
  1. progeressBar.setMax(123); //进度条最大值  
  2. progeressBar.setVisibility(View.VISIBLE); //将进度条设置为可见  
  3. progeressBar.setVisibility(View.GONE); //将进度条设置为不可见  
  4. progeressBar.SetProgress(int); //进度条进度(颜色较深)  
  5. progeressBar.SetSecondaryProgress(int); //进度条第二进度(颜色较浅)  

 


10.列表:ListView
//布局文件

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <ListView   
  8.         android:id="@id/android:list"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="fill_parent"  
  11.         android:drawSelectorOnTop="false"  
  12.         android:scrollbars="vertical"  
  13.         android:padding="5px" />  
  14.   
  15. </LinearLayout>  

 

//列表需要继承 ListActivity

[java] view plaincopy
  1. public class Control2Activity extends ListActivity {  
  2.   
  3.     @Override  
  4.     public void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.control2);  
  7.   
  8.         // 组织一下数据源  
  9.         ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();  
  10.         HashMap<String, String> hm1 = new HashMap<String, String>();  
  11.         hm1.put("userName""张三");  
  12.         hm1.put("userAccount""ZhengYongQiang");  
  13.         list.add(hm1);  
  14.         HashMap<String, String> hm2 = new HashMap<String, String>();  
  15.         hm2.put("userName""李四");  
  16.         hm2.put("userAccount""WengJiaXiong");  
  17.         list.add(hm2);  
  18.         HashMap<String, String> hm3 = new HashMap<String, String>();  
  19.         hm3.put("userName""王五");  
  20.         hm3.put("userAccount""LiLiXiang");  
  21.         list.add(hm3);  
  22.   
  23.         // 绑定数据源  
  24.         SimpleAdapter adapter = new SimpleAdapter(this, list,  
  25.                 R.layout.control2list,  
  26.                 new String[] { "userName""userAccount" },   
  27.                 new int[] {R.id.lblUserName, R.id.lblUserAccount });  
  28.         setListAdapter(adapter);  
  29.   
  30.         ListView lv = getListView();  
  31.         lv.setOnItemClickListener(new OnItemClickListener() {  
  32.             public void onItemClick(AdapterView<?> av, View v, int pos, long id) {  
  33.                 Toast.makeText(Control2Activity.this"亲,你点击了:" + id, Toast.LENGTH_SHORT).show();  
  34.             }  
  35.         });  
  36.     }  
  37. }  



11.Spinner的基本使用方法
1.布局文件

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <Spinner  
  8.         android:id="@+id/ddlCity"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content" />  
  11.   
  12. </LinearLayout>  


2.在string.xml创建一个数组

[html] view plaincopy
  1. <string-array name="array_count">  
  2.     <item>1</item>  
  3.     <item>2</item>  
  4. </string-array>  


3.代码绑定

[java] view plaincopy
  1. public class SpinnerActivity extends Activity {  
  2.   
  3.     Spinner ddlCity = null;  
  4.   
  5.     @Override  
  6.     public void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.spinner);  
  9.   
  10.         ddlCity = (Spinner) findViewById(R.id.ddlCity);  
  11.         ddlCity.setOnItemSelectedListener(new OnItemSelectedListener() {  
  12.             public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {  
  13.                 Toast.makeText(SpinnerActivity.this"亲,你选择了" + adapterView.getItemAtPosition(position).toString(), 3000).show();  
  14.             }  
  15.   
  16.             public void onNothingSelected(AdapterView<?> adapterView) {  
  17.                 Toast.makeText(SpinnerActivity.this"亲,你先一个吧!"3000).show();  
  18.             }  
  19.         });  
  20.   
  21.         // 第一种方法:通过string.xml中的数组创建  
  22.         // ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(SpinnerActivity.this,  
  23.         // R.array.array_city, android.R.layout.simple_spinner_item);  
  24.   
  25.         // 第二种方法:通过一个List动态加载数据  
  26.         List<String> list = new ArrayList<String>();  
  27.         list.add("福州");  
  28.         list.add("漳州");  
  29.         list.add("厦门");  
  30.         ArrayAdapter<String> adapter = new ArrayAdapter<String>(SpinnerActivity.this, android.R.layout.simple_spinner_item, list);  
  31.   
  32.         // 设置下拉列表样式  
  33.         adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  
  34.   
  35.         ddlCity.setAdapter(adapter);  
  36.         ddlCity.setPrompt("请选择城市:");  
  37.     }  
  38. }  



12.DatePicker和DatePickerDialog的基本使用
//布局文件

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <LinearLayout  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_marginTop="10dp"  
  11.         android:orientation="horizontal" >  
  12.   
  13.         <TextView  
  14.             android:layout_width="wrap_content"  
  15.             android:layout_height="wrap_content"  
  16.             android:text="@string/remindDate" />  
  17.   
  18.         <EditText  
  19.             android:id="@+id/txtDate"  
  20.             android:layout_width="fill_parent"  
  21.             android:layout_height="wrap_content"  
  22.             android:inputType="date" />  
  23.     </LinearLayout>  
  24.   
  25.     <LinearLayout  
  26.         android:layout_width="fill_parent"  
  27.         android:layout_height="wrap_content"  
  28.         android:layout_marginTop="10dp"  
  29.         android:orientation="horizontal" >  
  30.   
  31.         <TextView  
  32.             android:layout_width="wrap_content"  
  33.             android:layout_height="wrap_content"  
  34.             android:text="@string/remindTime" />  
  35.   
  36.         <EditText  
  37.             android:id="@+id/txtTime"  
  38.             android:layout_width="fill_parent"  
  39.             android:layout_height="wrap_content"  
  40.             android:inputType="time" />  
  41.     </LinearLayout>  
  42.   
  43. </LinearLayout>  


//编码

[java] view plaincopy
  1. public class PickerActivity extends Activity {  
  2.   
  3.     EditText txtDate = null;  
  4.     EditText txtTime = null;  
  5.   
  6.     private static final int DIALOG_DATE_ID = 1;  
  7.     private static final int DIALOG_TIME_ID = 2;  
  8.   
  9.     @Override  
  10.     public void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.picker);  
  13.   
  14.         txtDate = (EditText) findViewById(R.id.txtDate);  
  15.         txtTime = (EditText) findViewById(R.id.txtTime);  
  16.   
  17.         txtDate.setOnFocusChangeListener(new OnFocusChangeListener() {  
  18.             public void onFocusChange(View v, boolean hasFocus) {  
  19.                 if (hasFocus)  
  20.                     showDialog(DIALOG_DATE_ID);  
  21.             }  
  22.         });  
  23.   
  24.         txtTime.setOnFocusChangeListener(new OnFocusChangeListener() {  
  25.             public void onFocusChange(View v, boolean hasFocus) {  
  26.                 if (hasFocus)  
  27.                     showDialog(DIALOG_TIME_ID);  
  28.             }  
  29.         });  
  30.   
  31.         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.activity_list_item, REMIND_LIST);  
  32.         txtRemind.setAdapter(adapter);  
  33.     }  
  34.   
  35.     DatePickerDialog.OnDateSetListener onDateSetListener = new OnDateSetListener() {  
  36.         public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {  
  37.             txtDate.setText(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth);  
  38.         }  
  39.     };  
  40.   
  41.     TimePickerDialog.OnTimeSetListener onTimeSetListener = new OnTimeSetListener() {  
  42.         public void onTimeSet(TimePicker view, int hourOfDay, int minute) {  
  43.             txtTime.setText(hourOfDay + ":" + minute);  
  44.         }  
  45.     };  
  46.   
  47.     @Override  
  48.     protected Dialog onCreateDialog(int id) {  
  49.         switch (id) {  
  50.         case DIALOG_DATE_ID:  
  51.             new DatePickerDialog(this, onDateSetListener, 2012114).show();  
  52.             break;  
  53.         case DIALOG_TIME_ID:  
  54.             new TimePickerDialog(this, onTimeSetListener, 1314true).show();  
  55.             break;  
  56.         }  
  57.         return super.onCreateDialog(id);  
  58.     }  
  59. }  



13.AutoCompleteTextView的基本使用
//布局文件

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <LinearLayout  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_marginTop="10dp"  
  11.         android:orientation="horizontal" >  
  12.   
  13.         <TextView  
  14.             android:layout_width="wrap_content"  
  15.             android:layout_height="wrap_content"  
  16.             android:text="@string/remindText" />  
  17.   
  18.         <AutoCompleteTextView  
  19.             android:id="@+id/txtRemind"  
  20.             android:layout_width="fill_parent"  
  21.             android:layout_height="wrap_content" />  
  22.     </LinearLayout>  
  23.   
  24. </LinearLayout>  


//编码

[java] view plaincopy
  1. public class PickerActivity extends Activity {  
  2.   
  3.     AutoCompleteTextView txtRemind = null;  
  4.   
  5.     private static final String[] REMIND_LIST = new String[] {  
  6.             "闹钟模式""节日提醒""会议提醒""特殊提醒",  
  7.             "ClockRemind""DayRemind""MeetingRemind""OtherRemind"  
  8.     };  
  9.   
  10.     @Override  
  11.     public void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.picker);  
  14.   
  15.         txtDate = (EditText) findViewById(R.id.txtDate);  
  16.         txtTime = (EditText) findViewById(R.id.txtTime);  
  17.         txtRemind = (AutoCompleteTextView) findViewById(R.id.txtRemind);  
  18.   
  19.         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.activity_list_item, REMIND_LIST);  
  20.         txtRemind.setAdapter(adapter);  
  21.     }  
  22. }  


14.ExpandableListActivity的基本使用方法
1.在布局文件中声明对象

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <ExpandableListView  
  8.         android:id="@id/android:list"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="fill_parent"  
  11.         android:drawSelectorOnTop="false" />  
  12.   
  13.     <TextView  
  14.         android:id="@id/android:empty"  
  15.         android:layout_width="fill_parent"  
  16.         android:layout_height="fill_parent"  
  17.         android:text="No Data" />  
  18.   
  19. </LinearLayout>  


2.在而已文件中声明group的样式group.xml

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="horizontal" >  
  6.   
  7.     <TextView   
  8.         android:id="@+id/lblExpListGroup"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"   
  11.         android:layout_marginLeft="40dp"  
  12.         android:layout_marginTop="10dp"  
  13.         android:layout_marginBottom="10dp"  
  14.         android:textSize="20dp" />  
  15.       
  16. </LinearLayout>  


3.在而已文件当中声明子项的样式child.xml

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="horizontal" >  
  6.   
  7.     <TextView   
  8.         android:id="@+id/lblExpListItem"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"   
  11.         android:layout_marginLeft="50dp"  
  12.         android:layout_marginTop="10dp"  
  13.         android:layout_marginBottom="10dp"  
  14.         android:textSize="16dp" />  
  15. </LinearLayout>  


4.创建一个Activity,继承ExpandableListActivity
5.为group创建数据
6.为child创建数据
7.绑定数据

[java] view plaincopy
  1. public class ExpListActivity extends ExpandableListActivity {  
  2.     @Override  
  3.     public void onCreate(Bundle savedInstanceState) {  
  4.         super.onCreate(savedInstanceState);  
  5.         setContentView(R.layout.explist);  
  6.   
  7.         //列表组数据  
  8.         List<Map<String, String>> groups = new ArrayList<Map<String, String>>();  
  9.         Map<String, String> group1 = new HashMap<String, String>();  
  10.         group1.put("name""列表组1");  
  11.         groups.add(group1);  
  12.         Map<String, String> group2 = new HashMap<String, String>();  
  13.         group2.put("name""列表组2");  
  14.         groups.add(group2);  
  15.   
  16.         //列表组1数据  
  17.         List<Map<String, String>> item1 = new ArrayList<Map<String, String>>();  
  18.         Map<String, String> item11 = new HashMap<String, String>();  
  19.         item11.put("name""列表组1子项1");  
  20.         item1.add(item11);  
  21.         Map<String, String> item12 = new HashMap<String, String>();  
  22.         item12.put("name""列表组1子项2");  
  23.         item1.add(item12);  
  24.   
  25.         //列表组2数据  
  26.         List<Map<String, String>> item2 = new ArrayList<Map<String, String>>();  
  27.         Map<String, String> item21 = new HashMap<String, String>();  
  28.         item21.put("name""列表组2子项1");  
  29.         item2.add(item21);  
  30.         Map<String, String> item22 = new HashMap<String, String>();  
  31.         item22.put("name""列表组2子项2");  
  32.         item2.add(item22);  
  33.           
  34.         //列表项数据  
  35.         List<List<Map<String,String>>> items = new ArrayList<List<Map<String,String>>>();  
  36.         items.add(item1);  
  37.         items.add(item2);  
  38.           
  39.         //创建适配器  
  40.         SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(ExpListActivity.this,   
  41.                 groups, R.layout.explistgroup, new String[] { "name" }, new int[] { R.id.lblExpListGroup },  
  42.                 items, R.layout.explistitem, new String[] { "name" }, new int[] { R.id.lblExpListItem });  
  43.           
  44.         //绑定  
  45.         setListAdapter(adapter);  
  46.     }  
  47. }  




15.SeekBar的基本使用方法
进度条,可滑动,设置音量、音乐进度等

1.在布局文件中声明

[html] view plaincopy
  1. <SeekBar  
  2.     android:id="@+id/sbar"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="wrap_content" />  


2.定义一个监听器

[java] view plaincopy
  1. private class SeekBarListener implements SeekBar.OnSeekBarChangeListener{  
  2.     @Override //滑块改变触发  
  3.     public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){  
  4.         System.out.println("Changed : " + progress);  
  5.     }  
  6.     @Override //滑块开始改变时触发  
  7.     public void onStartTrackingTouch(SeekBar seekBar){  
  8.         System.out.println("Start : " + seekBar.getProgress());  
  9.     }  
  10.     @Override //滑块停止改变时触发  
  11.     public void onStopTrackingTouch(SeekBar seekBar){  
  12.         System.out.println("Stop : " + seekBar.getProgress());  
  13.     }  
  14. }  


3.实例

[java] view plaincopy
  1. seekBar = (SeekBar)findViewById(R.id.sbar);  
  2. seekBar.setMax(100); //设置最大进度  
  3. seekBar.setOnSeekBarChangeListener(new SeekBarListener()); //绑定事件  




16.RatingBar的基本使用方法
打分条,几个五角星,可以供用户打分

1.在布局文件中声明

[html] view plaincopy
  1. <RatingBar  
  2.     android:id="@+id/rbar"  
  3.     android:layout_width="wrap_content"  
  4.     android:layout_height="wrap_content"  
  5.     android:numStars="5"  
  6.     android:stepSize="1.0" />  


2.定义一个监听器

[java] view plaincopy
  1. private class RatingBarListener implements RatingBar.OnRatingBarChangeListener{  
  2.     @Override  
  3.     public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser){  
  4.         System.out.println("rating : " + rating);  
  5.     }  
  6. }  


3.绑定

[java] view plaincopy
  1. ratingBar.setOnRatingBarChangeListener(new RatingBarListener());