Android提供了強大的事件處理機制,主要包括兩大類:
1,基于監聽的事件處理機制:主要做法是為Android界面組件綁定特定的事件監聽器
2,基于回調的事件處理機制:主要做法是重寫Android組件特定的回調方法,或重寫Activity的回調方法。也就是說Android的絕大多數的界面組件都提供了事件響應的回調方法,開發者只要重寫它們即可
基于監聽的事件處理是一種更加面向對象的事件處理,這種事件處理方式與Java的Swing處理方式幾乎相同。
**監聽處理模型:**
**事件源(Event Source)**:事件發生的場所,就是各個組件,比如按鈕,窗口,菜單等
**事件(Event)**:事件封裝了界面組件上發生的特定事情(就是用戶操作)。如果程序需要獲得界面組件上所發生事件的相關信息,一般通過Event對象來獲取
**事件監聽器(Event Listener)**:負責監聽事件源所發生的時間,并且對各個事件作出響應。
**委派式事件處理方法:**
普通組件(事件源)將整個事件處理委派給特定的對象(事件監聽器);當該事件源發生指定的時間是,就通知所委派的事件監聽器來處理這個事件。
每個組件都可以針對特定的時間指定一個事件監聽器,每個事件監聽器也可以監聽一個或多個事件源。因為同一個事件源上可能發生多個事件,委派式事件處理方式可以吧事件源上所有可能發生的時間分別授權給不同的事件處理器來處理;同時也可以讓一類事件都使用同一個事件監聽器來處理。

activity_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"
android:gravity="center_horizontal"
>
<EditText
android:id="@+id/txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:editable="false"
android:cursorVisible="false"
android:textSize="12pt"
/>
<!-- 定義一個按鈕,該按鈕將作為事件源 -->
<Button
android:id="@+id/bn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="單擊我"
/>
</LinearLayout>
~~~
MainActivity.java
~~~
public class <span style="font-size:24px;">MainActivity </span>extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 獲取應用程序中的bn按鈕
Button bn = (Button) findViewById(R.id.bn);
// 為按鈕綁定事件監聽器。
bn.setOnClickListener(new MyClickListener()); // ①
}
// 定義一個單擊事件的監聽器
class MyClickListener implements View.OnClickListener
{
// 實現監聽器類必須實現的方法,該方法將會作為事件處理器
@Override
public void onClick(View v)
{
EditText txt = (EditText) findViewById(R.id.txt);
txt.setText("bn按鈕被單擊了!");
}
}
}
~~~
基于監聽器的事件處理模型的編程步驟如下:
1,獲取普通界面組件(事件源),也就是被監聽的對象
2,實現時間監聽類,該監聽器類是一個特殊的Java類,必須實現一個XxxListener接口
3,調用事件源的setXXXListener方法,將事件監聽器對象注冊給普通組件(事件源)
事件監聽器必須實現事件監聽接口,Android的不同界面組件提供了不同的監聽器接口,這些接口通常以內部類的形式存在。以View類為例,包含如下監聽接口:
View.OnClickListener單擊事件的事件監聽器必須時間的接口
View.OnCreateContextMenuListener:創建上下文菜單時間的事件監聽器必須實現的接口
View.onFocusChangeListener:焦點改變事件的事件監聽器必須實現的接口
View.OnKeyListener:按鍵事件的時間監聽器必須實現的接口
**所謂事件監聽器,其實就是實現了特定接口的java類的實例。在程序上一般有幾種形式:**
1,內部類形式:將事件監聽器定義成當前類的內部類
~~~
public class EventQs extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 獲取應用程序中的bn按鈕
Button bn = (Button) findViewById(R.id.bn);
// 為按鈕綁定事件監聽器。
bn.setOnClickListener(new MyClickListener()); //
}
// 定義一個單擊事件的監聽器,內部類形式
class MyClickListener implements View.OnClickListener
{
// 實現監聽器類必須實現的方法,該方法將會作為事件處理器
@Override
public void onClick(View v)
{
EditText txt = (EditText) findViewById(R.id.txt);
txt.setText("bn按鈕被單擊了!");
}
}
}
~~~
**因為監聽器是內部類,所以可以自由訪問外部類的所有界面組件,這也是內部類的優勢。**
2,外部類形式:將事件監聽器類定義成一個外部類
外部類形式的事件監聽器不能自由訪問創建GUI界面類的組件,使用時需要向監聽器類傳遞GUI界面類的組件的引用 ,所以用的比較少
MainActivity.java
~~~
public class MainActivity extends Activity
{
EditText address;
EditText content;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 獲取頁面中收件人地址、短信內容
address = (EditText)findViewById(R.id.address);
content = (EditText)findViewById(R.id.content);
Button bn = (Button)findViewById(R.id.send);
bn.setOnLongClickListener(new SendSmsListener(
this , address, content));//外部類形式,需要傳遞本類組件
}
}
~~~
SendSmsListener.java
~~~
public class SendSmsListener implements OnLongClickListener
{
private Activity act;
private EditText address;
private EditText content;
public SendSmsListener(Activity act, EditText address
, EditText content)
{
this.act = act;
this.address = address;
this.content = content;
}
@Override
public boolean onLongClick(View source)
{
String addressStr = address.getText().toString();
String contentStr = content.getText().toString();
// 獲取短信管理器
SmsManager smsManager = SmsManager.getDefault();
// 創建發送短信的PendingIntent
PendingIntent sentIntent = PendingIntent.getBroadcast(act
, 0, new Intent(), 0);
// 發送文本短信
smsManager.sendTextMessage(addressStr, null, contentStr
, sentIntent, null);
Toast.makeText(act, "短信發送完成", Toast.LENGTH_LONG).show();
return false;
}
}
~~~
3,Activity本身作為事件監聽器類:讓Activity本身實現監聽接口,并實現事件處理器方法
~~~
// <span style="font-size:24px;">Activity</span>實現事件監聽器接口
public class ActivityListener extends Activity
implements OnClickListener
{
EditText show;
Button bn;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
show = (EditText) findViewById(R.id.show);
bn = (Button) findViewById(R.id.bn);
// 直接使用Activity作為事件監聽器
bn.setOnClickListener(this);
}
// 實現事件處理方法
@Override
public void onClick(View v)
{
show.setText("bn按鈕被單擊了!");
}
}
~~~
4,匿名內部類形式:使用匿名內部類創建事件監聽器對象。代碼簡單的情況,只能用一次
~~~
public class MainActivity extends Activity
{
EditText show;
Button bn;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
show = (EditText) findViewById(R.id.show);
bn = (Button) findViewById(R.id.bn);
// 直接使用Activity作為事件監聽器
bn.setOnClickListener(new OnClickListener()
{
// 實現事件處理方法
@Override
public void onClick(View v)
{
show.setText("bn按鈕被單擊了!");
}
});
}
~~~
- 前言
- Eclipse搭建android環境及Genymotion模擬器安裝問題解決方法
- 表格布局(TableLayout)及重要屬性
- 幀布局(FrameLayout)及屬性
- layout_width和width,layout_height和height
- UI組件之TextView及其子類
- UI組件之TextView及其子類(一)TextView和EditText
- UI組件之TextView及其子類(二)RadioButton和CheckBox
- UI組件之TextView及其子類(三)ToggleButton和Switch
- UI組件之TextView及其子類(四)AnalogClock,DigitalClock
- UI組件之TextView及其子類(五)計時器Chronometer
- UI組件之ImageView及其子類(一)ImageView顯示圖片
- UI組件之ImageView及其子類(二)ImageButton ,ZoomButton
- UI組件之AdapterView及其子類關系,Adapter接口及其實現類關系
- UI組件之AdapterView及其子類(一)三種Adapter適配器填充ListView
- UI組件之AdapterView及其子類(二)GridView網格視圖的使用
- UI組件之AdapterView及其子類(三)Spinner控件詳解
- UI組件之AdapterView及其子類(四)Gallery畫廊控件使用
- UI組件之AdapterView及其子類(五)ListView組件和ListActivity
- UI組件之AdapterView及其子類(六)ExpandableListView組件和ExpandableListActivity的使用
- UI組件之 ProgressBar及其子類(一)ProgressBar進度條的使用
- UI組件之ProgressBar及其子類(二)SeekBar拖動條和RatingBar星級評分條的使用
- ViewFlipper的功能和用法
- Toast的功能和用法
- TabHost選項卡的 功能和用法
- AlertDialog創建6種對話框的用法
- Android基于監聽的事件處理機制
- Android基于回調的事件處理
- Handler消息傳遞機制(一)
- Handler消息傳遞機制(二)Handler,Loop,Message,MessageQueue的工作原理
- 啟動Activity的兩種方式startActivity和startActivityForResult(一)
- 啟動Activity的兩種方式startActivity和startActivityForResult(二)
- Activity的生命周期理解
- Bundle在Activity之間交換數據
- 通過 Intent 傳遞類對象
- Intent對象詳解(一)
- Intent對象詳解(二)
- 使用指定的Action,Category調用系統Activity
- 使用Action,Data屬性啟動系統Activity
- Android數據存儲的三種方式-SharedPrefrences,File,SQLite