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:菜单,点击菜单键后弹出的选项菜单
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- menu.add(0, 1, 1, R.string.exit); //组ID,项ID,排序ID,文字
- menu.add(0, 2, 2, R.string.about);
- return super.onCreateOptionsMenu(menu);
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- if(item.getItemId() == 1){
- finish(); //表示结束应用程序
- }
- return super.onOptionsItemSelected(item);
- }
6.单选框:RadioGroup / RadioButton
一个RadioGroup包括多个RadioButton,用于分组,与HTML一样
- rgroupSex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
- public void onCheckedChanged(RadioGroup group, int checkedId) {
- if (rbtnMale.getId() == checkedId) {
- Toast.makeText(Control1Activity.this, "亲,你是男的喔!", Toast.LENGTH_SHORT).show();
- }
- }
- });
7.复选框:CheckBox
- chkRead.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
- if(isChecked){
- Toast.makeText(Control1Activity.this, "亲,我也喜欢读书哈~", Toast.LENGTH_SHORT).show();
- }
- }
- });
8.提示信息:Toast
一种消息提示的方式,会在屏幕下方中间显示出一段小提示文本
- 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" //不可见
- progeressBar.setMax(123); //进度条最大值
- progeressBar.setVisibility(View.VISIBLE); //将进度条设置为可见
- progeressBar.setVisibility(View.GONE); //将进度条设置为不可见
- progeressBar.SetProgress(int); //进度条进度(颜色较深)
- progeressBar.SetSecondaryProgress(int); //进度条第二进度(颜色较浅)
10.列表:ListView
//布局文件
- <?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="fill_parent"
- android:drawSelectorOnTop="false"
- android:scrollbars="vertical"
- android:padding="5px" />
- </LinearLayout>
//列表需要继承 ListActivity
- public class Control2Activity extends ListActivity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.control2);
- // 组织一下数据源
- ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
- HashMap<String, String> hm1 = new HashMap<String, String>();
- hm1.put("userName", "张三");
- hm1.put("userAccount", "ZhengYongQiang");
- list.add(hm1);
- HashMap<String, String> hm2 = new HashMap<String, String>();
- hm2.put("userName", "李四");
- hm2.put("userAccount", "WengJiaXiong");
- list.add(hm2);
- HashMap<String, String> hm3 = new HashMap<String, String>();
- hm3.put("userName", "王五");
- hm3.put("userAccount", "LiLiXiang");
- list.add(hm3);
- // 绑定数据源
- SimpleAdapter adapter = new SimpleAdapter(this, list,
- R.layout.control2list,
- new String[] { "userName", "userAccount" },
- new int[] {R.id.lblUserName, R.id.lblUserAccount });
- setListAdapter(adapter);
- ListView lv = getListView();
- lv.setOnItemClickListener(new OnItemClickListener() {
- public void onItemClick(AdapterView<?> av, View v, int pos, long id) {
- Toast.makeText(Control2Activity.this, "亲,你点击了:" + id, Toast.LENGTH_SHORT).show();
- }
- });
- }
- }
11.Spinner的基本使用方法
1.布局文件
- <?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" >
- <Spinner
- android:id="@+id/ddlCity"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" />
- </LinearLayout>
2.在string.xml创建一个数组
- <string-array name="array_count">
- <item>1</item>
- <item>2</item>
- </string-array>
3.代码绑定
- public class SpinnerActivity extends Activity {
- Spinner ddlCity = null;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.spinner);
- ddlCity = (Spinner) findViewById(R.id.ddlCity);
- ddlCity.setOnItemSelectedListener(new OnItemSelectedListener() {
- public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
- Toast.makeText(SpinnerActivity.this, "亲,你选择了" + adapterView.getItemAtPosition(position).toString(), 3000).show();
- }
- public void onNothingSelected(AdapterView<?> adapterView) {
- Toast.makeText(SpinnerActivity.this, "亲,你先一个吧!", 3000).show();
- }
- });
- // 第一种方法:通过string.xml中的数组创建
- // ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(SpinnerActivity.this,
- // R.array.array_city, android.R.layout.simple_spinner_item);
- // 第二种方法:通过一个List动态加载数据
- List<String> list = new ArrayList<String>();
- list.add("福州");
- list.add("漳州");
- list.add("厦门");
- ArrayAdapter<String> adapter = new ArrayAdapter<String>(SpinnerActivity.this, android.R.layout.simple_spinner_item, list);
- // 设置下拉列表样式
- adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
- ddlCity.setAdapter(adapter);
- ddlCity.setPrompt("请选择城市:");
- }
- }
12.DatePicker和DatePickerDialog的基本使用
//布局文件
- <?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" >
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="10dp"
- android:orientation="horizontal" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/remindDate" />
- <EditText
- android:id="@+id/txtDate"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:inputType="date" />
- </LinearLayout>
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="10dp"
- android:orientation="horizontal" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/remindTime" />
- <EditText
- android:id="@+id/txtTime"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:inputType="time" />
- </LinearLayout>
- </LinearLayout>
//编码
- public class PickerActivity extends Activity {
- EditText txtDate = null;
- EditText txtTime = null;
- private static final int DIALOG_DATE_ID = 1;
- private static final int DIALOG_TIME_ID = 2;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.picker);
- txtDate = (EditText) findViewById(R.id.txtDate);
- txtTime = (EditText) findViewById(R.id.txtTime);
- txtDate.setOnFocusChangeListener(new OnFocusChangeListener() {
- public void onFocusChange(View v, boolean hasFocus) {
- if (hasFocus)
- showDialog(DIALOG_DATE_ID);
- }
- });
- txtTime.setOnFocusChangeListener(new OnFocusChangeListener() {
- public void onFocusChange(View v, boolean hasFocus) {
- if (hasFocus)
- showDialog(DIALOG_TIME_ID);
- }
- });
- ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.activity_list_item, REMIND_LIST);
- txtRemind.setAdapter(adapter);
- }
- DatePickerDialog.OnDateSetListener onDateSetListener = new OnDateSetListener() {
- public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
- txtDate.setText(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth);
- }
- };
- TimePickerDialog.OnTimeSetListener onTimeSetListener = new OnTimeSetListener() {
- public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
- txtTime.setText(hourOfDay + ":" + minute);
- }
- };
- @Override
- protected Dialog onCreateDialog(int id) {
- switch (id) {
- case DIALOG_DATE_ID:
- new DatePickerDialog(this, onDateSetListener, 2012, 1, 14).show();
- break;
- case DIALOG_TIME_ID:
- new TimePickerDialog(this, onTimeSetListener, 13, 14, true).show();
- break;
- }
- return super.onCreateDialog(id);
- }
- }
13.AutoCompleteTextView的基本使用
//布局文件
- <?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" >
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="10dp"
- android:orientation="horizontal" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/remindText" />
- <AutoCompleteTextView
- android:id="@+id/txtRemind"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" />
- </LinearLayout>
- </LinearLayout>
//编码
- public class PickerActivity extends Activity {
- AutoCompleteTextView txtRemind = null;
- private static final String[] REMIND_LIST = new String[] {
- "闹钟模式", "节日提醒", "会议提醒", "特殊提醒",
- "ClockRemind", "DayRemind", "MeetingRemind", "OtherRemind"
- };
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.picker);
- txtDate = (EditText) findViewById(R.id.txtDate);
- txtTime = (EditText) findViewById(R.id.txtTime);
- txtRemind = (AutoCompleteTextView) findViewById(R.id.txtRemind);
- ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.activity_list_item, REMIND_LIST);
- txtRemind.setAdapter(adapter);
- }
- }
14.ExpandableListActivity的基本使用方法
1.在布局文件中声明对象
- <?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" >
- <ExpandableListView
- android:id="@id/android:list"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:drawSelectorOnTop="false" />
- <TextView
- android:id="@id/android:empty"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:text="No Data" />
- </LinearLayout>
2.在而已文件中声明group的样式group.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" >
- <TextView
- android:id="@+id/lblExpListGroup"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_marginLeft="40dp"
- android:layout_marginTop="10dp"
- android:layout_marginBottom="10dp"
- android:textSize="20dp" />
- </LinearLayout>
3.在而已文件当中声明子项的样式child.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" >
- <TextView
- android:id="@+id/lblExpListItem"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_marginLeft="50dp"
- android:layout_marginTop="10dp"
- android:layout_marginBottom="10dp"
- android:textSize="16dp" />
- </LinearLayout>
4.创建一个Activity,继承ExpandableListActivity
5.为group创建数据
6.为child创建数据
7.绑定数据
- public class ExpListActivity extends ExpandableListActivity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.explist);
- //列表组数据
- List<Map<String, String>> groups = new ArrayList<Map<String, String>>();
- Map<String, String> group1 = new HashMap<String, String>();
- group1.put("name", "列表组1");
- groups.add(group1);
- Map<String, String> group2 = new HashMap<String, String>();
- group2.put("name", "列表组2");
- groups.add(group2);
- //列表组1数据
- List<Map<String, String>> item1 = new ArrayList<Map<String, String>>();
- Map<String, String> item11 = new HashMap<String, String>();
- item11.put("name", "列表组1子项1");
- item1.add(item11);
- Map<String, String> item12 = new HashMap<String, String>();
- item12.put("name", "列表组1子项2");
- item1.add(item12);
- //列表组2数据
- List<Map<String, String>> item2 = new ArrayList<Map<String, String>>();
- Map<String, String> item21 = new HashMap<String, String>();
- item21.put("name", "列表组2子项1");
- item2.add(item21);
- Map<String, String> item22 = new HashMap<String, String>();
- item22.put("name", "列表组2子项2");
- item2.add(item22);
- //列表项数据
- List<List<Map<String,String>>> items = new ArrayList<List<Map<String,String>>>();
- items.add(item1);
- items.add(item2);
- //创建适配器
- SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(ExpListActivity.this,
- groups, R.layout.explistgroup, new String[] { "name" }, new int[] { R.id.lblExpListGroup },
- items, R.layout.explistitem, new String[] { "name" }, new int[] { R.id.lblExpListItem });
- //绑定
- setListAdapter(adapter);
- }
- }
15.SeekBar的基本使用方法
进度条,可滑动,设置音量、音乐进度等
1.在布局文件中声明
- <SeekBar
- android:id="@+id/sbar"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" />
2.定义一个监听器
- private class SeekBarListener implements SeekBar.OnSeekBarChangeListener{
- @Override //滑块改变触发
- public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
- System.out.println("Changed : " + progress);
- }
- @Override //滑块开始改变时触发
- public void onStartTrackingTouch(SeekBar seekBar){
- System.out.println("Start : " + seekBar.getProgress());
- }
- @Override //滑块停止改变时触发
- public void onStopTrackingTouch(SeekBar seekBar){
- System.out.println("Stop : " + seekBar.getProgress());
- }
- }
3.实例
- seekBar = (SeekBar)findViewById(R.id.sbar);
- seekBar.setMax(100); //设置最大进度
- seekBar.setOnSeekBarChangeListener(new SeekBarListener()); //绑定事件
16.RatingBar的基本使用方法
打分条,几个五角星,可以供用户打分
1.在布局文件中声明
- <RatingBar
- android:id="@+id/rbar"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:numStars="5"
- android:stepSize="1.0" />
2.定义一个监听器
- private class RatingBarListener implements RatingBar.OnRatingBarChangeListener{
- @Override
- public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser){
- System.out.println("rating : " + rating);
- }
- }
3.绑定
- ratingBar.setOnRatingBarChangeListener(new RatingBarListener());