編輯:關於 Android 程式設計
概述:上一篇已從原理層面講解了 Android 觸摸機制的運作模式,這次我們透過實際程式碼來驗證這個結論——是驢子是馬,拉出來遛一遛便知。另外筆者學識有限,若有任何問題,歡迎交流指正。
先補充一個重要觀念:上一篇曾提到三個與觸摸機制息息相關的方法(onTouchEvent、onInterceptTouchEvent、dispatchTouchEvent),但更精確地說,只有 ViewGroup 才完整擁有這三個方法;而 View 並沒有 onInterceptTouchEvent。原因很簡單——View 已是最小單位,底下沒有其他子元件可以攔截了。
先拋出一個問題供思考:Activity 和 View 究竟是什麼關係?兩者看似都是介面,但背後另有玄机。以下透過幾個自訂元件觀察事件的分發流程。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
System.out.println("MainActivity---dispatchTouchEvent---ACTION_DOWN");
break;
case MotionEvent.ACTION_UP:
System.out.println("MainActivity---dispatchTouchEvent---ACTION_UP");
break;
}
boolean result = super.dispatchTouchEvent(event);
System.out.println("MainActivity---dispatchTouchEvent---result:" + result);
return result;
}
}
作為最小 View 單位,Button 沒有 onInterceptTouchEvent 方法可以覆寫,只能實作另外兩個方法:
public class MyButton extends Button {
public MyButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) System.out.println("MyButton---onTouchEvent---ACTION_DOWN");
if (action == MotionEvent.ACTION_MOVE) System.out.println("MyButton---onTouchEvent---ACTION_MOVE");
if (action == MotionEvent.ACTION_UP) System.out.println("MyButton---onTouchEvent---ACTION_UP");
return true;
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
int action = event.getAction();
boolean result = super.dispatchTouchEvent(event);
System.out.println("MyButton---dispatchTouchEvent---result:" + result);
return result;
}
}
作為 LinearLayout 的子類,它擁有 onInterceptTouchEvent 方法,可以選擇是否攔截子 View 的事件。以下範例中刻意將 onInterceptTouchEvent 的結果設為 true,模擬攔截行為:
public class MyLinearLayout extends LinearLayout {
public MyLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
boolean result = super.onTouchEvent(event);
System.out.println("MyLinearLayout---onTouchEvent---result:" + result);
return result;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
boolean result = super.onInterceptTouchEvent(event);
result = true; // 強制攔截
System.out.println("MyLinearLayout---onInterceptTouchEvent---result:" + result);
return result;
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
boolean result = super.dispatchTouchEvent(event);
System.out.println("MyLinearLayout---dispatchTouchEvent---result:" + result);
return result;
}
}
和自訂 Button 相同,ImageView 作為最小 View 單位,同樣不存在 onInterceptTouchEvent 方法,只能實作 dispatchTouchEvent 與 onTouchEvent。
最後來看布局,結構相當簡單,將上述自訂元件依序排列即可:
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:textStyle="bold"
android:text="@string/touch" />
<com.example.touchevent.MyButton
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/title"
android:layout_marginTop="10dp"
android:text="@string/mybutton1" />
<com.example.touchevent.MyLinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/button1"
android:layout_marginTop="50dp"
android:gravity="center" >
<com.example.touchevent.MyImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="fitCenter"
android:src="@drawable/jacky" />
</com.example.touchevent.MyLinearLayout>
</RelativeLayout>
透過觀察 LogCat 的輸出順序,你便能清楚掌握事件從 Activity 到 ViewGroup、再到子 View 的分發與回傳路徑,真正理解 Android 觸摸機制的運作精髓。
讓 select2 搜尋框支援拼音輸入:改寫 matcher 並掛上拼音對照表
select2 的下拉搜尋預設只比對選項文字本身,中文選項 therefore 只能打中文字才有反應。對使用者來說很不友善——誰會記得每個選項的完整用字?只要加一層拼音映
Android OpenGLES 2.0(六):用三角形拼出圓錐、圓柱與球體
前面的筆記裡,我們已經畫過三角形、正方形、圓形與立方體。這一集要挑戰圓錐、圓柱和球體。把這幾個基本幾何體拿下來之後,其餘常見的形體基本就攔不住我們了。 先把最重要的心法再
Android SQLite 實戰:增刪改查全彙總,含無 SQL 語句的 CRUD 封裝
SQLite 是 Android 自帶的輕量數據庫,幾乎每個稍具規模的應用都會用到。本文把常用操作系統性地梳理一遍:既包括原生 SQL 寫法,也包括完全不寫 SQL 的
微信公眾號如何設定分組?後台用戶管理圖文教學
公眾號用戶變多後,可透過分組管理,讓群發與名單整理更有效率。 設定步驟 1. 登入微信公眾平台,進入官網後台。 2. 在左側選單找到「管理」,點擊「用戶管理」。 3.