uiControl整体界面如下图所示,按照视频教程,学习控件由于是初学,都是最基础知识。还有ImageSwitcher、Gallery未更新,o(╯□╰)o
1 package com.example.uiconrrol; 2 3 import android.content.Context; 4 import android.content.Intent; 5 6 public class ListCellData { 7 8 public ListCellData(Context context, String controlName, 9 Intent relatedIntent) { 10 this.controlName = controlName; 11 this.context = context; 12 this.relatedIntent = relatedIntent; 13 } 14 15 private String controlName = ""; 16 17 public String getControlName() { 18 return controlName; 19 } 20 21 private Context context = null; 22 23 public Context getContext() { 24 return context; 25 } 26 27 private Intent relatedIntent = null; 28 29 public Intent getRelatedIntent() { 30 return relatedIntent; 31 } 32 33 public void startActivity() { 34 getContext().startActivity(getRelatedIntent()); 35 } 36 37 // 重写toString方法,只显示Name 38 @Override 39 public String toString() { 40 return getControlName(); 41 } 42 43 }
1 package com.example.uiconrrol; 2 3 import android.app.ListActivity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.widget.ArrayAdapter; 8 import android.widget.ListView; 9 10 public class MainActivity extends ListActivity { 11 12 private ArrayAdapter<ListCellData> adapter; 13 14 @Override 15 protected void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 18 adapter = new ArrayAdapter<ListCellData>(this, 19 android.R.layout.simple_list_item_1); 20 setListAdapter(adapter); 21 22 adapter.add(new ListCellData(this, "RadioGroup", new Intent(this, 23 AtyUsingRadioGroup.class))); 24 adapter.add(new ListCellData(this, "CheckBox", new Intent(this, 25 AtyUsingCheckBox.class))); 26 adapter.add(new ListCellData(this, "DatePicker", new Intent(this, 27 AtyUsingDatePicker.class))); 28 adapter.add(new ListCellData(this, "TimePicker", new Intent(this, 29 AtyUsingTimePicker.class))); 30 adapter.add(new ListCellData(this, "Spinner", new Intent(this, 31 AtyUsingSpinner.class))); 32 adapter.add(new ListCellData(this, "ProgressBar", new Intent(this, 33 AtyUsingProgressBar.class))); 34 adapter.add(new ListCellData(this, "SeekBar", new Intent(this, 35 AtyUsingSeekBar.class))); 36 adapter.add(new ListCellData(this, "GridView", new Intent(this, 37 AtyUsingGridView.class))); 38 adapter.add(new ListCellData(this, "ProgressDialog", new Intent(this, 39 AtyUsingProgressDialog.class))); 40 adapter.add(new ListCellData(this, "Notification", new Intent(this, 41 AtyUsingNotification.class))); 42 adapter.add(new ListCellData(this, "SrcollView", new Intent(this, 43 AtyUsingSrcollView.class))); 44 adapter.add(new ListCellData(this, "RatingBar", new Intent(this, 45 AtyUsingRatingBar.class))); 46 } 47 48 @Override 49 protected void onListItemClick(ListView l, View v, int position, long id) { 50 51 ListCellData data = adapter.getItem(position); 52 data.startActivity(); 53 54 super.onListItemClick(l, v, position, id); 55 } 56 57 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.example.uiconrrol" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 7 <uses-sdk 8 android:minSdkVersion="8" 9 android:targetSdkVersion="21" /> 10 11 <application 12 android:allowBackup="true" 13 android:icon="@drawable/ic_launcher" 14 android:label="@string/app_name" 15 android:theme="@style/AppTheme" > 16 <activity 17 android:name=".MainActivity" 18 android:label="@string/app_name" > 19 <intent-filter> 20 <action android:name="android.intent.action.MAIN" /> 21 22 <category android:name="android.intent.category.LAUNCHER" /> 23 </intent-filter> 24 </activity> 25 <activity android:name="AtyUsingRadioGroup" > 26 </activity> 27 <activity android:name="AtyUsingCheckBox" > 28 </activity> 29 <activity android:name="AtyUsingDatePicker" > 30 </activity> 31 <activity android:name="AtyUsingTimePicker" > 32 </activity> 33 <activity android:name="AtyUsingSpinner" > 34 </activity> 35 <activity android:name="AtyUsingProgressBar" > 36 </activity> 37 <activity android:name="AtyUsingSeekBar" > 38 </activity> 39 <activity android:name="AtyUsingGridView" > 40 </activity> 41 <activity android:name="AtyUsingProgressDialog" > 42 </activity> 43 <activity android:name="AtyUsingNotification" > 44 </activity> 45 <activity android:name="AtyUsingSrcollView" > 46 </activity> 47 <activity android:name="AtyUsingRatingBar" > 48 </activity> 49 </application> 50 51 </manifest>
1. RadioGroup
介绍Android中如何使用RadioGroup和RadioButton,实现自定义的RadioGroup中被选中RadioButton的变更监听事件。
1 package com.example.uiconrrol; 2 3 import android.app.Activity; 4 import android.app.AlertDialog; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.widget.RadioButton; 8 9 public class AtyUsingRadioGroup extends Activity { 10 11 private RadioButton radioRight; 12 13 @Override 14 protected void onCreate(Bundle savedInstanceState) { 15 // TODO Auto-generated method stub 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.aty_using_radiogroup); 18 19 radioRight = (RadioButton) findViewById(R.id.radioRight); 20 21 findViewById(R.id.btnSubmit).setOnClickListener( 22 new View.OnClickListener() { 23 24 @Override 25 public void onClick(View v) { 26 if (radioRight.isChecked()) { 27 new AlertDialog.Builder(AtyUsingRadioGroup.this) 28 .setTitle("判断").setMessage("回答正确") 29 .setPositiveButton("Cancel", null).show(); 30 } else { 31 new AlertDialog.Builder(AtyUsingRadioGroup.this) 32 .setTitle("判断").setMessage("回答错误") 33 .setPositiveButton("Cancel", null).show(); 34 } 35 } 36 }); 37 } 38 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <TextView 8 android:id="@+id/textView1" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:text="老师是个帅哥吗?" /> 12 13 <RadioGroup 14 android:id="@+id/radioGroup1" 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content" > 17 18 <RadioButton 19 android:id="@+id/radioRight" 20 android:layout_width="wrap_content" 21 android:layout_height="wrap_content" 22 android:text="是" /> 23 24 <RadioButton 25 android:id="@+id/radioNo" 26 android:layout_width="wrap_content" 27 android:layout_height="wrap_content" 28 android:text="不是" /> 29 30 <RadioButton 31 android:id="@+id/radio2" 32 android:layout_width="wrap_content" 33 android:layout_height="wrap_content" 34 android:text="不知道" /> 35 </RadioGroup> 36 37 <Button 38 android:id="@+id/btnSubmit" 39 android:layout_width="wrap_content" 40 android:layout_height="wrap_content" 41 android:text="提交" /> 42 43 </LinearLayout>
2. CheckBox
CheckBox是多项选择,允许用户用列表中选择一个或多个选项,它在android源代码中是继承CompoundButton类的,而CompoundButton类是继承于Button类的。
1 package com.example.uiconrrol; 2 3 import android.app.Activity; 4 import android.app.AlertDialog; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.widget.Button; 8 import android.widget.CheckBox; 9 10 public class AtyUsingCheckBox extends Activity { 11 12 private CheckBox cbMifan, cbDoufu, cbXihongshi, cbTudou; 13 private Button btnSubmit; 14 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.aty_using_checkbox); 19 20 cbMifan = (CheckBox) findViewById(R.id.cbMifan); 21 cbDoufu = (CheckBox) findViewById(R.id.cbDoufu); 22 cbXihongshi = (CheckBox) findViewById(R.id.cbXihongshi); 23 cbTudou = (CheckBox) findViewById(R.id.cbTudou); 24 25 btnSubmit = (Button) findViewById(R.id.btnSubmit); 26 27 btnSubmit.setOnClickListener(new View.OnClickListener() { 28 29 @Override 30 public void onClick(View v) { 31 32 String str = "中午要吃的是: \n"; 33 34 if (cbMifan.isChecked()) { 35 str += "米饭 \n"; 36 } 37 if (cbDoufu.isChecked()) { 38 str += "豆腐 \n"; 39 } 40 if (cbXihongshi.isChecked()) { 41 str += "西红柿 \n"; 42 } 43 if (cbTudou.isChecked()) { 44 str += "土豆"; 45 } 46 47 new AlertDialog.Builder(AtyUsingCheckBox.this).setTitle("结果") 48 .setMessage(str).setPositiveButton("关闭", null).show(); 49 } 50 }); 51 } 52 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <TextView 8 android:id="@+id/textView1" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:text="中午你打算吃什么?" 12 android:textAppearance="?android:attr/textAppearanceLarge" /> 13 14 <CheckBox 15 android:id="@+id/cbTudou" 16 android:layout_width="wrap_content" 17 android:layout_height="wrap_content" 18 android:text="土豆" /> 19 20 <CheckBox 21 android:id="@+id/cbXihongshi" 22 android:layout_width="wrap_content" 23 android:layout_height="wrap_content" 24 android:text="西红柿" /> 25 26 <CheckBox 27 android:id="@+id/cbMifan" 28 android:layout_width="wrap_content" 29 android:layout_height="wrap_content" 30 android:text="米饭" /> 31 32 <CheckBox 33 android:id="@+id/cbDoufu" 34 android:layout_width="wrap_content" 35 android:layout_height="wrap_content" 36 android:text="豆腐" /> 37 38 <Button 39 android:id="@+id/btnSubmit" 40 android:layout_width="wrap_content" 41 android:layout_height="wrap_content" 42 android:text="提交" /> 43 44 </LinearLayout>
3. DatePicker
DatePicker是一个选择年月日的日历控件。
1 package com.example.uiconrrol; 2 3 import android.app.Activity; 4 import android.app.DatePickerDialog; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.widget.Button; 8 import android.widget.DatePicker; 9 10 public class AtyUsingDatePicker extends Activity { 11 12 private Button btnDatePicer; 13 14 @Override 15 protected void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.aty_using_datepicker); 18 19 btnDatePicer = (Button) findViewById(R.id.btnSelectDate); 20 21 btnDatePicer.setOnClickListener(new View.OnClickListener() { 22 23 @Override 24 public void onClick(View v) { 25 new DatePickerDialog(AtyUsingDatePicker.this, 26 new DatePickerDialog.OnDateSetListener() { 27 28 @Override 29 public void onDateSet(DatePicker view, int year, 30 int monthOfYear, int dayOfMonth) { 31 btnDatePicer.setText(String.format("%d:%d:%d", 32 year, monthOfYear, dayOfMonth)); 33 } 34 }, 2014, 10, 22).show(); 35 } 36 }); 37 } 38 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <Button 8 android:id="@+id/btnSelectDate" 9 android:layout_width="match_parent" 10 android:layout_height="wrap_content" 11 android:text="0000:00:00" /> 12 13 </LinearLayout>
4. TimePicker
TimePicker也继承自FrameLayout类,时间选择控件向用户显示一天中的时间(可以为24小时,也可以为AM/PM制),并允许用户进行选择,如果要捕获用户修改时间数据的事件,便需要为TimePicker添加 OnTimeChangedListener监听器。
1 package com.example.uiconrrol; 2 3 import android.app.Activity; 4 import android.app.TimePickerDialog; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.widget.Button; 8 import android.widget.TimePicker; 9 10 public class AtyUsingTimePicker extends Activity { 11 12 private Button btnSelectTime; 13 14 @Override 15 protected void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.aty_using_timepicker); 18 19 btnSelectTime = (Button) findViewById(R.id.btnSelectTime); 20 21 btnSelectTime.setOnClickListener(new View.OnClickListener() { 22 23 @Override 24 public void onClick(View v) { 25 26 new TimePickerDialog(AtyUsingTimePicker.this, 27 new TimePickerDialog.OnTimeSetListener() { 28 29 @Override 30 public void onTimeSet(TimePicker view, 31 int hourOfDay, int minute) { 32 btnSelectTime.setText(String.format("%s:%s", 33 timeFormat(hourOfDay), 34 timeFormat(minute))); 35 } 36 }, 10, 25, true).show(); 37 } 38 }); 39 } 40 41 public String timeFormat(int value) { 42 return (value >= 10) ? ("" + value) : ("0" + value); 43 } 44 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <Button 8 android:id="@+id/btnSelectTime" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:text="00:00" /> 12 13 </LinearLayout>
5. Spinner
Spinner位于android.widget包下,每次只显示用户选中的元素,当用户再次点击时,会弹出选择列表供用户选择,而选择列表中的元素同样来自适配器,Spinner是View类的一个子类。
1 package com.example.uiconrrol; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.widget.ArrayAdapter; 6 import android.widget.Spinner; 7 8 public class AtyUsingSpinner extends Activity { 9 10 private Spinner spinner; 11 private ArrayAdapter<String> adapter; 12 13 @Override 14 protected void onCreate(Bundle savedInstanceState) { 15 super.onCreate(savedInstanceState); 16 setContentView(R.layout.aty_using_spinner); 17 18 adapter = new ArrayAdapter<String>(this, 19 android.R.layout.simple_spinner_item); 20 adapter.add("1"); 21 adapter.add("2"); 22 adapter.add("3"); 23 adapter.add("4"); 24 25 spinner = (Spinner) findViewById(R.id.spinner); 26 spinner.setAdapter(adapter); 27 } 28 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <Spinner 8 android:id="@+id/spinner" 9 android:layout_width="match_parent" 10 android:layout_height="wrap_content" /> 11 12 </LinearLayout>
6. ProgressBar
ProgressBar是一个进度条控件,当用户在后台执行一些任务时,在前台展示的信息,用于表示任务正在处理,请用户等待或者给用户展示处理的进度消息等,它在android源代码中是直接继承View类的。
1 package com.example.uiconrrol; 2 3 import java.util.Timer; 4 import java.util.TimerTask; 5 6 import android.app.Activity; 7 import android.os.Bundle; 8 import android.widget.ProgressBar; 9 10 public class AtyUsingProgressBar extends Activity { 11 12 private ProgressBar pb; 13 14 @Override 15 protected void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.aty_using_progressbar); 18 19 pb = (ProgressBar) findViewById(R.id.progressBar); 20 pb.setMax(100); 21 22 } 23 24 @Override 25 protected void onResume() { 26 super.onResume(); 27 28 startTimer(); 29 } 30 31 @Override 32 protected void onPause() { 33 super.onPause(); 34 35 stopTimer(); 36 } 37 38 private int progress = 0; 39 40 public void startTimer() { 41 if (timer == null) { 42 timer = new Timer(); 43 task = new TimerTask() { 44 45 @Override 46 public void run() { 47 48 progress++; 49 pb.setProgress(progress); 50 51 } 52 }; 53 timer.scheduleAtFixedRate(task, 1000, 500); 54 } 55 } 56 57 public void stopTimer() { 58 if (timer != null) { 59 task.cancel(); 60 timer.cancel(); 61 62 task = null; 63 timer = null; 64 } 65 } 66 67 private Timer timer = null; 68 private TimerTask task = null; 69 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <ProgressBar 8 android:id="@+id/progressBar2" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" /> 11 12 <ProgressBar 13 android:id="@+id/progressBar3" 14 style="?android:attr/progressBarStyleLarge" 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content" /> 17 18 <ProgressBar 19 android:id="@+id/progressBar" 20 style="?android:attr/progressBarStyleHorizontal" 21 android:layout_width="fill_parent" 22 android:layout_height="wrap_content" /> 23 24 </LinearLayout>
7. SeekBar
SeekBar控件其实是相对高级的进度条,是可以拖动的,可以改变进度的一个进度条控件。
1 package com.example.uiconrrol; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.widget.SeekBar; 6 import android.widget.TextView; 7 8 public class AtyUsingSeekBar extends Activity { 9 10 private SeekBar seekBar; 11 private TextView txOut; 12 13 @Override 14 protected void onCreate(Bundle savedInstanceState) { 15 super.onCreate(savedInstanceState); 16 setContentView(R.layout.aty_using_seekbar); 17 18 txOut = (TextView) findViewById(R.id.txOut); 19 20 seekBar = (SeekBar) findViewById(R.id.seekBar); 21 seekBar.setMax(100); 22 seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 23 24 @Override 25 public void onStopTrackingTouch(SeekBar seekBar) { 26 27 } 28 29 @Override 30 public void onStartTrackingTouch(SeekBar seekBar) { 31 32 } 33 34 @Override 35 public void onProgressChanged(SeekBar seekBar, int progress, 36 boolean fromUser) { 37 txOut.setText(String.format("当前进度为:%d", progress) + "%"); 38 } 39 }); 40 41 } 42 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <SeekBar 8 android:id="@+id/seekBar" 9 android:layout_width="match_parent" 10 android:layout_height="wrap_content" /> 11 12 <TextView 13 android:id="@+id/txOut" 14 android:layout_width="fill_parent" 15 android:layout_height="wrap_content" 16 android:text="TextView" /> 17 18 </LinearLayout>
8. GridView
GridView(网格视图)是按照行列的方式来显示内容的,一般用于显示图片等内容,比如实现九宫格图,用GridView是首选,也是最简单的。
1 package com.example.uiconrrol; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.widget.ArrayAdapter; 6 import android.widget.GridView; 7 8 public class AtyUsingGridView extends Activity { 9 10 private ArrayAdapter<String> adapter; 11 private GridView gridView; 12 13 @Override 14 protected void onCreate(Bundle savedInstanceState) { 15 super.onCreate(savedInstanceState); 16 setContentView(R.layout.aty_using_gridview); 17 18 adapter = new ArrayAdapter<String>(this, 19 android.R.layout.simple_list_item_1); 20 21 gridView = (GridView) findViewById(R.id.gridView); 22 gridView.setAdapter(adapter); 23 24 for (int i = 0; i < 60; i++) { 25 adapter.add("Nankai" + i); 26 } 27 } 28 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <GridView 8 android:id="@+id/gridView" 9 android:layout_width="match_parent" 10 android:layout_height="fill_parent" 11 android:numColumns="3" > 12 </GridView> 13 14 </LinearLayout>
9. ProgressDialog
ProgressDialog,顾名思义,就是一个进度对话框,常用于显示载入进度、下载进度等,合理使用ProgressDialog能增加用户体验,让用户知道现在程序所处的状态。
1 package com.example.uiconrrol; 2 3 import android.app.Activity; 4 import android.app.ProgressDialog; 5 import android.os.Bundle; 6 import android.view.View; 7 8 public class AtyUsingProgressDialog extends Activity { 9 10 private ProgressDialog progressDialog; 11 12 @Override 13 protected void onCreate(Bundle savedInstanceState) { 14 super.onCreate(savedInstanceState); 15 setContentView(R.layout.aty_using_progressdialog); 16 17 findViewById(R.id.btnShowProgressDialog).setOnClickListener( 18 new View.OnClickListener() { 19 20 @Override 21 public void onClick(View v) { 22 progressDialog = ProgressDialog.show( 23 AtyUsingProgressDialog.this, "加载中", "正在加载,请稍后"); 24 25 new Thread() { 26 public void run() { 27 try { 28 Thread.sleep(3000); 29 30 progressDialog.dismiss(); 31 } catch (InterruptedException e) { 32 e.printStackTrace(); 33 } 34 }; 35 }.start(); 36 } 37 }); 38 } 39 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <Button 8 android:id="@+id/btnShowProgressDialog" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:text="Show Progress Dialog" /> 12 13 </LinearLayout>
10. Notification
Notification是Android中常用的一种通知方式,当有未读短信或未接电话的时候,屏幕状态栏就会有提示图标,这时可以下拉状态栏来读取通知。在使用微信的时(微信在后台运行),如果有新消息时便会发出声音提示,状态栏也有相应的微信提示。
1 package com.example.uiconrrol; 2 3 import android.app.Activity; 4 import android.app.Notification; 5 import android.app.NotificationManager; 6 import android.app.PendingIntent; 7 import android.content.Context; 8 import android.os.Bundle; 9 import android.view.View; 10 11 public class AtyUsingNotification extends Activity { 12 13 private NotificationManager nm; 14 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.aty_using_notification); 19 20 nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 21 nm.cancel(R.layout.aty_using_notification); 22 23 findViewById(R.id.btnShowNotification).setOnClickListener(new View.OnClickListener() { 24 25 @Override 26 public void onClick(View v) { 27 Notification n = new Notification(R.drawable.ic_launcher, "Ticker Text", System.currentTimeMillis()); 28 n.setLatestEventInfo(AtyUsingNotification.this, "Title", "content", PendingIntent.getActivity(AtyUsingNotification.this, 1, getIntent(), 0)); 29 nm.notify(R.layout.aty_using_notification, n); 30 } 31 }); 32 } 33 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <Button 8 android:id="@+id/btnShowNotification" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:text="Button" /> 12 13 </LinearLayout>
11. ScrollView
ScrollView滚动视图是指当拥有很多内容,屏幕显示不完时,需要通过滚动跳来显示的视图。ScrollView只支持垂直滚动。
1 package com.example.uiconrrol; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 6 public class AtyUsingSrcollView extends Activity { 7 8 @Override 9 protected void onCreate(Bundle savedInstanceState) { 10 super.onCreate(savedInstanceState); 11 setContentView(R.layout.aty_using_scrollview); 12 } 13 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" > 5 6 <LinearLayout 7 android:layout_width="fill_parent" 8 android:layout_height="wrap_content" 9 android:orientation="vertical" > 10 11 <TextView 12 android:id="@+id/textView1" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:text="TextView" /> 16 17 <EditText 18 android:id="@+id/editText1" 19 android:layout_width="match_parent" 20 android:layout_height="wrap_content" 21 android:ems="10" 22 android:inputType="textMultiLine" > 23 24 <requestFocus /> 25 </EditText> 26 27 <EditText 28 android:id="@+id/editText2" 29 android:layout_width="match_parent" 30 android:layout_height="wrap_content" 31 android:ems="10" 32 android:inputType="textPersonName" /> 33 34 <Spinner 35 android:id="@+id/spinner1" 36 android:layout_width="match_parent" 37 android:layout_height="wrap_content" /> 38 39 <Button 40 android:id="@+id/button1" 41 style="?android:attr/buttonStyleSmall" 42 android:layout_width="wrap_content" 43 android:layout_height="wrap_content" 44 android:text="Button" /> 45 46 <EditText 47 android:id="@+id/editText3" 48 android:layout_width="match_parent" 49 android:layout_height="wrap_content" 50 android:ems="10" 51 android:inputType="textPostalAddress" /> 52 53 <ProgressBar 54 android:id="@+id/progressBar1" 55 style="?android:attr/progressBarStyleLarge" 56 android:layout_width="wrap_content" 57 android:layout_height="wrap_content" /> 58 59 <Spinner 60 android:id="@+id/spinner2" 61 android:layout_width="match_parent" 62 android:layout_height="wrap_content" /> 63 64 <RatingBar 65 android:id="@+id/ratingBar1" 66 android:layout_width="wrap_content" 67 android:layout_height="wrap_content" /> 68 69 <RadioGroup 70 android:id="@+id/radioGroup2" 71 android:layout_width="wrap_content" 72 android:layout_height="wrap_content" > 73 74 <RadioButton 75 android:id="@+id/radio0" 76 android:layout_width="wrap_content" 77 android:layout_height="wrap_content" 78 android:checked="true" 79 android:text="RadioButton" /> 80 81 <RadioButton 82 android:id="@+id/radio1" 83 android:layout_width="wrap_content" 84 android:layout_height="wrap_content" 85 android:text="RadioButton" /> 86 87 <RadioButton 88 android:id="@+id/radio2" 89 android:layout_width="wrap_content" 90 android:layout_height="wrap_content" 91 android:text="RadioButton" /> 92 </RadioGroup> 93 94 <RadioGroup 95 android:id="@+id/radioGroup1" 96 android:layout_width="wrap_content" 97 android:layout_height="wrap_content" > 98 99 <RadioButton 100 android:id="@+id/radio3" 101 android:layout_width="wrap_content" 102 android:layout_height="wrap_content" 103 android:checked="true" 104 android:text="RadioButton" /> 105 106 <RadioButton 107 android:id="@+id/radio4" 108 android:layout_width="wrap_content" 109 android:layout_height="wrap_content" 110 android:text="RadioButton" /> 111 112 <RadioButton 113 android:id="@+id/radio5" 114 android:layout_width="wrap_content" 115 android:layout_height="wrap_content" 116 android:text="RadioButton" /> 117 </RadioGroup> 118 </LinearLayout> 119 120 </ScrollView>
12. RatingBar
RatingBar是SeekBar和ProgressBar的扩展,用星星来评级,使用的默认大小RatingBar时,用户可以触摸/拖动或使用键来设置评分,它有大小两种样式,其中大的只适合指示,不适合于用户交互。
1 package com.example.uiconrrol; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.widget.RatingBar; 6 import android.widget.Toast; 7 8 public class AtyUsingRatingBar extends Activity { 9 10 private RatingBar ratingBar; 11 12 @Override 13 protected void onCreate(Bundle savedInstanceState) { 14 super.onCreate(savedInstanceState); 15 setContentView(R.layout.aty_using_ratingbar); 16 17 ratingBar = (RatingBar) findViewById(R.id.ratingBar); 18 ratingBar 19 .setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() { 20 21 @Override 22 public void onRatingChanged(RatingBar ratingBar, 23 float rating, boolean fromUser) { 24 Toast.makeText(AtyUsingRatingBar.this, 25 "评价为: " + rating, Toast.LENGTH_SHORT).show(); 26 } 27 }); 28 } 29 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <RatingBar 8 android:id="@+id/ratingBar" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" /> 11 12 </LinearLayout>