編輯:關於 Android 開發
本篇延續上一回的內容,繼續盤點 Android 開發中最常打交道的一批控件,以及它們各自對應的事件監聽方式。掌握這些監聽器,你就能應對絕大多數的日常互動需求。
以下先列出本篇將要涵蓋的六類控件與其監聽介面:
① EditText(編輯框)——OnKeyListener
② RadioGroup、RadioButton(單選按鈕)——OnCheckedChangeListener
③ CheckBox(多選按鈕)——OnCheckedChangeListener
④ Spinner(下拉清單)——OnItemSelectedListener
⑤ Menu(選單)——onMenuItemSelected
⑥ Dialog(對話方塊)——DialogInterface.OnClickListener()
這個範例示範了如何即時擷取編輯框中的輸入內容,並同步顯示到 TextView 上。同時也示範了用 setHint 設定提示文字的效果(在 main.xml 中也可用 android:hint 達成同樣目的)。
1 package org.hualang.eventtest2; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.KeyEvent; 6 import android.view.View; 7 import android.widget.EditText; 8 import android.widget.TextView; 9 10 public class EventTest2 extends Activity { 11 /** Called when the activity is first created. */ 12 private TextView mytext; 13 private EditText edittext; 14 @Override 15 public void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.main); 18 mytext = (TextView)findViewById(R.id.mytext); 19 edittext = (EditText)findViewById(R.id.edittext); 20 /** 21 * 設置當EditText為空,則提示"請輸入賬號" 22 * 在配置文件main.xml中可以用android:hint="請輸入賬號"來實現 23 */ 24 edittext.setHint("請輸入賬號"); 25 //下面為EditText事件監聽 26 edittext.setOnKeyListener(new EditText.OnKeyListener() 27 { 28 29 @Override 30 public boolean onKey(View arg0, int arg1, KeyEvent arg2) { 31 //得到文字,顯示在TextView中 32 mytext.setText("內容:"+edittext.getText().toString()); 33 return false; 34 } 35 36 }); 37 } 38 }
main.xml:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" 6 > 7 <TextView 8 android:layout_width="fill_parent" 9 android:layout_height="wrap_content" 10 android:id="@+id/mytext" 11 /> 12 <EditText 13 android:id="@+id/edittext" 14 android:layout_width="fill_parent" 15 android:layout_height="wrap_content" 16 android:textSize="10pt" 17 /> 18 </LinearLayout>
執行結果如下:


這裡用一個「哪個城市美女最多」的趣味小測驗,示範如何透過 RadioGroup 的 OnCheckedChangeListener 判斷使用者選中了哪一項。
1 package org.hualang.eventtest; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.Gravity; 6 import android.widget.RadioButton; 7 import android.widget.RadioGroup; 8 import android.widget.Toast; 9 10 public class EventTest3 extends Activity { 11 /** Called when the activity is first created. */ 12 private RadioGroup group; 13 private RadioButton radio1,radio2,radio3,radio4; 14 @Override 15 public void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.main); 18 19 group = (RadioGroup)findViewById(R.id.radiogroup1); 20 radio1 = (RadioButton)findViewById(R.id.button1); 21 radio2 = (RadioButton)findViewById(R.id.button2); 22 radio3 = (RadioButton)findViewById(R.id.button3); 23 radio4 = (RadioButton)findViewById(R.id.button4); 24 25 group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 26 27 @Override 28 public void onCheckedChanged(RadioGroup group, int checkedId) { 29 // TODO Auto-generated method stub 30 if (checkedId == radio2.getId()) 31 { 32 showMessage("正確答案:" + radio2.getText()+",恭喜你,答對了"); 33 } 34 else 35 { 36 showMessage("對不起,雖然很多,但不是公認的最多"); 37 } 38 } 39 }); 40 } 41 public void showMessage(String str) 42 { 43 Toast toast = Toast.makeText(this, str, Toast.LENGTH_SHORT); 44 toast.setGravity(Gravity.TOP, 0, 220); 45 toast.show(); 46 } 47 }
main.xml:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" 6 > 7 <TextView 8 android:id="@+id/mytextview" 9 android:layout_width="fill_parent" 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 android:orientation="vertical" 18 > 19 <RadioButton 20 android:id="@+id/button1" 21 android:layout_width="wrap_content" 22 android:layout_height="wrap_content" 23 android:text="杭州" 24 /> 25 <RadioButton 26 android:id="@+id/button2" 27 android:layout_width="wrap_content" 28 android:layout_height="wrap_content" 29 android:text="重慶" 30 /> 31 <RadioButton 32 android:id="@+id/button3" 33 android:layout_width="wrap_content" 34 android:layout_height="wrap_content" 35 android:text="成都" 36 /> 37 <RadioButton 38 android:id="@+id/button4" 39 android:layout_width="wrap_content" 40 android:layout_height="wrap_content" 41 android:text="香港" 42 /> 43 </RadioGroup> 44 </LinearLayout>
執行結果如下:

這個範例用五個 CheckBox 讓使用者勾選喜歡的行動系統,並在按下「確定」時統計勾選數量。每個 CheckBox 都透過 OnCheckedChangeListener 即時回饋選擇狀態。
1 package org.hualang.eventtest4; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.Gravity; 6 import android.view.View; 7 import android.widget.Button; 8 import android.widget.CheckBox; 9 import android.widget.CompoundButton; 10 import android.widget.CompoundButton.OnCheckedChangeListener; 11 import android.widget.Toast; 12 13 public class EventTest4 extends Activity { 14 /** Called when the activity is first created. */ 15 private CheckBox ch1,ch2,ch3,ch4,ch5; 16 private Button mybutton; 17 @Override 18 public void onCreate(Bundle savedInstanceState) { 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.main); 21 22 mybutton = (Button)findViewById(R.id.mybutton); 23 ch1 = (CheckBox)findViewById(R.id.check1); 24 ch2 = (CheckBox)findViewById(R.id.check2); 25 ch3 = (CheckBox)findViewById(R.id.check3); 26 ch4 = (CheckBox)findViewById(R.id.check4); 27 ch5 = (CheckBox)findViewById(R.id.check5); 28 29 ch1.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() 30 { 31 32 @Override 33 public void onCheckedChanged(CompoundButton arg0, boolean arg1) { 34 // TODO Auto-generated method stub 35 if(ch1.isChecked()) 36 { 37 showMessage("你選擇了"+ch1.getText()); 38 } 39 40 }); 41 ch2.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() 42 { 43 44 @Override 45 public void onCheckedChanged(CompoundButton arg0, boolean arg1) { 46 // TODO Auto-generated method stub 47 if(ch3.isChecked()) 48 { 49 showMessage("你選擇了"+ch2.getText()); 50 } 51 } 52 53 }); 54 ch3.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() 55 { 56 57 @Override 58 public void onCheckedChanged(CompoundButton arg0, boolean arg1) { 59 // TODO Auto-generated method stub 60 if(ch3.isChecked()) 61 { 62 showMessage("你選擇了"+ch3.getText()); 63 } 64 } 65 }); 66 ch4.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() 67 { 68 69 @Override 70 public void onCheckedChanged(CompoundButton arg0, boolean arg1) { 71 // TODO Auto-generated method stub 72 if(ch4.isChecked()) 73 { 74 showMessage("你選擇了"+ch4.getText()); 75 } 76 } 77 }); 78 ch5.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() 79 { 80 81 @Override 82 public void onCheckedChanged(CompoundButton arg0, boolean arg1) { 83 // TODO Auto-generated method stub 84 if(ch5.isChecked()) 85 { 86 showMessage("你選擇了"+ch5.getText()); 87 } 88 } 89 }); 90 91 mybutton.setOnClickListener(new Button.OnClickListener() 92 { 93 94 @Override 95 public void onClick(View arg0) { 96 // TODO Auto-generated method stub 97 int num = 0; 98 if(ch1.isChecked()) 99 { 100 num++; 101 } 102 if(ch2.isChecked()) 103 { 104 num++; 105 } 106 if(ch3.isChecked()) 107 { 108 num++; 109 } 110 if(ch4.isChecked()) 111 { 112 num++; 113 } 114 if(ch5.isChecked()) 115 { 116 num++; 117 } 118 119 showMessage("謝謝參與,您一共選擇了"+num+"項"); 120 121 } 122 123 }); 124 } 125 126 127 128 public void showMessage(String str) 129 { 130 Toast toast = Toast.makeText(this, str, Toast.LENGTH_SHORT); 131 toast.setGravity(Gravity.TOP, 0, 220); 132 toast.show(); 133 } 134 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" 6 > 7 <TextView 8 android:layout_width="fill_parent" 9 android:layout_height="wrap_content" 10 android:text="你喜歡哪些智能手機系統" 11 /> 12 <CheckBox 13 android:id="@+id/check1" 14 android:layout_width="fill_parent" 15 android:layout_height="wrap_content" 16 android:text="蘋果 ios" 17 /> 18 <CheckBox 19 android:id="@+id/check2" 20 android:layout_width="fill_parent" 21 android:layout_height="wrap_content" 22 android:text="谷歌 Android" 23 /> 24 <CheckBox 25 android:id="@+id/check3" 26 android:layout_width="fill_parent" 27 android:layout_height="wrap_content" 28 android:text="RIM BlackBerry" 29 /> 30 <CheckBox 31 android:id="@+id/check4" 32 android:layout_width="fill_parent" 33 android:layout_height="wrap_content" 34 android:text="微軟 Windows phone 7" 35 /> 36 <CheckBox 37 android:id="@+id/check5" 38 android:layout_width="fill_parent" 39 android:layout_height="wrap_content" 40 android:text="諾基亞 symbian" 41 /> 42 <Button 43 android:id="@+id/mybutton" 44 android:layout_width="fill_parent" 45 android:layout_height="wrap_content" 46 android:text="確定" 47 /> 48 49 </LinearLayout>
執行結果:

Spinner 透過 ArrayAdapter 綁定一組城市資料,並以 OnItemSelectedListener 回應使用者的選擇。
1 package org.hualang.eventtest5; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.View; 6 import android.widget.AdapterView; 7 import android.widget.ArrayAdapter; 8 import android.widget.Spinner; 9 import android.widget.TextView; 10 11 public class EventTest5 extends Activity { 12 /** Called when the activity is first created. */ 13 private static final String[] citys={"杭州","北京","成都","大連","深圳","南京"}; 14 private TextView text; 15 private Spinner spinner; 16 private ArrayAdapter<String> adapter; 17 @Override 18 public void onCreate(Bundle savedInstanceState) { 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.main); 21 text=(TextView)findViewById(R.id.text); 22 spinner=(Spinner)findViewById(R.id.spinner); 23 24 //將可選內容與ArrayAdapter連接 25 adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,citys); 26 //設置下拉列表風格 27 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 28 //將adapter添加到spinner中 29 spinner.setAdapter(adapter); 30 //添加Spinner事件監聽 31 spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() 32 { 33 34 @Override 35 public void onItemSelected(AdapterView<?> arg0, View arg1, 36 int arg2, long arg3) { 37 // TODO Auto-generated method stub 38 text.setText("你所在的城市是:"+citys[arg2]); 39 //設置顯示當前選擇的項 40 arg0.setVisibility(View.VISIBLE); 41 } 42 43 @Override 44 public void onNothingSelected(AdapterView<?> arg0) { 45 // TODO Auto-generated method stub 46 47 } 48 49 }); 50 } 51 }
main.xml:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" 6 > 7 <TextView 8 android:id="@+id/text" 9 android:layout_width="fill_parent" 10 android:layout_height="wrap_content" 11 android:text="您所在的城市" 12 /> 13 <Spinner 14 android:id="@+id/spinner" 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content" 17 android:layout_centerHorizontal="true" 18 /> 19 </LinearLayout>
執行結果如下:



透過 onCreateOptionsMenu 載入選單資源,並在 onMenuItemSelected 中依據被點選項的 ID 進行分派處理。
1 package org.hualang.eventtest6; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.Menu; 6 import android.view.MenuInflater; 7 import android.view.MenuItem; 8 import android.widget.Toast; 9 10 public class EventTest6 extends Activity { 11 /** Called when the activity is first created. */ 12 @Override 13 public void onCreate(Bundle savedInstanceState) { 14 super.onCreate(savedInstanceState); 15 setContentView(R.layout.main); 16 } 17 18 @Override 19 public boolean onCreateOptionsMenu(Menu menu) { 20 // TODO Auto-generated method stub 21 MenuInflater inflater = getMenuInflater(); 22 //設置menu界面為res/menu/menu.xml 23 inflater.inflate(R.menu.menu, menu); 24 return true; 25 } 26 27 @Override 28 public boolean onMenuItemSelected(int featureId, MenuItem item) { 29 //得到當前選中的MenuItem的ID 30 int itemId = item.getItemId(); 31 switch(itemId) 32 { 33 case R.id.apple: 34 Toast toast = Toast.makeText(this, "這是蘋果", Toast.LENGTH_SHORT); 35 toast.show(); 36 break; 37 case R.id.banana: 38 Toast toast2 = Toast.makeText(this, "這是香蕉", Toast.LENGTH_SHORT); 39 toast2.show(); 40 break; 41 case R.id.exit: 42 EventTest6.this.finish(); 43 break; 44 } 45 return true; 46 } 47 48 49 }
res/menu/menu.xml:
1 <?xml version="1.0" encoding="utf-8"?> 2 <menu xmlns:android="https://schemas.android.com/apk/res/android"> 3 <item android:id="@+id/apple" 4 android:title="蘋果" 5 /> 6 <item android:id="@+id/banana" 7 android:title="香蕉" 8 /> 9 <item android:id="@+id/exit" 10 android:title="退出" 11 /> 12 </menu>
執行結果如下:


這個範例示範了 AlertDialog 的多層次互動:先彈出一個登入提示,再展開自訂版面配置的登入框,最後以 ProgressDialog 模擬一段耗時的登入過程。
1 package org.hualang.dialog; 2 3 import android.app.Activity; 4 import android.app.AlertDialog; 5 import android.app.Dialog; 6 import android.app.ProgressDialog; 7 import android.content.DialogInterface; 8 import android.content.DialogInterface.OnClickListener; 9 import android.os.Bundle; 10 import android.view.LayoutInflater; 11 import android.view.View; 12 13 public class MainActivity extends Activity { 14 /** Called when the activity is first created. */ 15 ProgressDialog myDialog; 16 @Override 17 public void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.main); 20 21 Dialog dialog = new AlertDialog.Builder(MainActivity.this) 22 .setTitle("登錄提示") 23 .setMessage("這裡需要登錄") 24 .setPositiveButton("確定", new DialogInterface.OnClickListener() 25 { 26 27 @Override 28 public void onClick(DialogInterface dialog, int which) { 29 // TODO Auto-generated method stub 30 LayoutInflater factory = LayoutInflater.from(MainActivity.this); 31 final View DialogView = factory.inflate(R.layout.dialog, null); 32 AlertDialog dlg = new AlertDialog.Builder(MainActivity.this) 33 .setTitle("登錄框") 34 .setView(DialogView) 35 .setPositiveButton("確定", new DialogInterface.OnClickListener() 36 { 37 38 @Override 39 public void onClick(DialogInterface dialog, int whichButton) { 40 // TODO Auto-generated method stub 41 myDialog = ProgressDialog.show(MainActivity.this, "請等待...", "正在為你登錄", true); 42 new Thread() 43 { 44 public void run() 45 { 46 try 47 { 48 sleep(3000); 49 }catch(Exception e) 50 { 51 e.printStackTrace(); 52 }finally 53 { 54 myDialog.dismiss(); 55 } 56 } 57 }.start(); 58 } 59 }).setNegativeButton("取消", 60 new DialogInterface.OnClickListener() 61 { 62 63 @Override 64 public void onClick(DialogInterface dialog, int which) { 65 // TODO Auto-generated method stub 66 MainActivity.this.finish(); 67 } 68 }).create(); 69 dlg.show(); 70 } 71 }).setNeutralButton("退出", new DialogInterface.OnClickListener() 72 { 73 74 @Override 75 public void onClick(DialogInterface dialog, int which) { 76 // TODO Auto-generated method stub 77 MainActivity.this.finish(); 78 } 79 }).create(); 80 dialog.show(); 81 82 } 83 }
res/layout/dialog.xml:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" 6 > 7 <TextView 8 android:id="@+id/username" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:layout_marginLeft="20dip" 12 android:layout_marginRight="20dip" 13 android:text="賬號" 14 android:gravity="left" 15 android:textAppearance="?android:attr/textAppearanceMedium" 16 /> 17 <EditText 18 android:id="@+id/myusername" 19 android:layout_height="wrap_content" 20 android:layout_width="fill_parent" 21 android:layout_marginLeft="20dip" 22 android:layout_marginRight="20dip" 23 android:scrollHorizontally="true" 24 android:autoText="false" 25 android:capitalize="none" 26 android:gravity="fill_horizontal" 27 android:textAppearance="?android:attr/textAppearanceMedium" 28 /> 29 <TextView 30 android:id="@+id/password" 31 android:layout_width="fill_parent" 32 android:layout_height="wrap_content" 33 android:layout_marginLeft="20dip" 34 android:layout_marginRight="20dip" 35 android:text="密碼" 36 android:gravity="left" 37 android:textAppearance="?android:attr/textAppearanceMedium" 38 /> 39 <EditText 40 android:id="@+id/mypassword" 41 android:layout_width="fill_parent" 42 android:layout_height="wrap_content" 43 android:layout_marginLeft="20dip" 44 android:layout_marginRight="20dip" 45 android:scrollHorizontally="true" 46 android:autoText="false" 47 android:capitalize="none" 48 android:gravity="fill_horizontal" 49 android:password="true" 50 /> 51 </LinearLayout>
執行結果:



轉自:https://www.iteye.com/topic/1060815
使用 Source Insight 閱讀 Android 源碼的完整流程
使用 Source Insight 閱讀 Android 源碼Source Insight 適合建立大型 C/C++ 專案索引,便於閱讀 Android 源碼。安裝後,下
安卓 UI 適配完全指南:常用資源限定符一次搞懂
前言 同一套 App 要跑在尺寸、解析度、語言、版本都不同的裝置上,UI 適配一直是個額外負擔。限定符(qualifier)的出現,讓這件事變得單純許多:只要在 res
Android 原始碼在線搜尋技巧:快速定位類別與方法
Android 原始碼在線搜尋技巧:快速定位類別與方法 在搜尋引擎普及之前,工程師遇到問題時常常只能直接閱讀原始碼。即使現在可以透過網路找到答案,若想理解 Android
Android 效能優化:如何縮短應用啟動時間
啟動速度是最直接影響第一印象的指標。使用者不會看你的架構圖,但他一定會感覺到「這個 App 點下去要等多久」。 一、兩種啟動方式 冷啟動(Cold Start):啟動