編輯:關於 Android 程式設計
做瀏覽器時遇到這樣一個需求:點擊網址列時要全選文字並彈出鍵盤,再點一次則取消全選、顯示游標;點擊畫面其他區域時,鍵盤與游標都要收起來。
這其實是大多數瀏覽器的通用邏輯,因為它減少了使用者的操作步驟——不用長按選取,一下就能整段覆寫。實作起來並不難,下面把邏輯簡化成「畫面只有一個 EditText」來說明。
有兩個屬性是關鍵:android:focusable="true" 與 android:selectAllOnFocus="true"。
<?xml version="1.0" encoding="utf-8"?> <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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.edittexttest.MainActivity"> <EditText android:id="@+id/edit" android:layout_width="match_parent" android:layout_height="wrap_content" android:focusable="true" android:selectAllOnFocus="true" /> </RelativeLayout>
做法是攔截 Activity 層級的觸碰事件:先判斷手指按下的位置是否落在 EditText 之外,如果是就收起鍵盤並清除焦點;而 EditText 自身的觸碰監聽則負責「取得焦點 + 全選」。
package com.example.edittexttest; import android.content.Context; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.inputmethod.InputMethodManager; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private EditText editText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = (EditText) findViewById(R.id.edit); editText.setText("click to select all"); editText.clearFocus(); editText.setFocusableInTouchMode(false); editText.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { if (motionEvent.getAction() == MotionEvent.ACTION_UP) { editText.setFocusableInTouchMode(true); editText.requestFocus(); editText.setText("click to select all"); editText.selectAll(); } return false; } }); } @Override public boolean dispatchTouchEvent(MotionEvent ev) { if (ev.getAction() == MotionEvent.ACTION_DOWN) { View v = getCurrentFocus(); if (isShouldHideInput(v, ev)) { InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); if (imm.isActive()) { imm.hideSoftInputFromWindow(v.getWindowToken(), 0); } } return super.dispatchTouchEvent(ev); } // Necessary if (getWindow().superDispatchTouchEvent(ev)) { return true; } editText.clearFocus(); editText.setFocusableInTouchMode(false); return onTouchEvent(ev); } public boolean isShouldHideInput(View v, MotionEvent event) { if (v != null && (v instanceof EditText)) { int[] leftTop = { 0, 0 }; //get location of TextView v.getLocationInWindow(leftTop); int left = leftTop[0]; int top = leftTop[1]; int bottom = top + v.getHeight(); int right = left + v.getWidth(); if (event.getX() > left && event.getX() < right && event.getY() > top && event.getY() < bottom) { return false; } else { return true; } } return false; } }
真正讓「點擊即全選」生效的是這兩行:
editText.setFocusableInTouchMode(true); editText.requestFocus();
少了它們,即使設了 selectAllOnFocus,全選也不會發生。另外 isShouldHideInput() 的邊界判斷是用 View 在視窗中的實際座標去比對觸碰點,實作時記得把狀態列與導覽列的高度差算進去。
Android 資料儲存:SharedPreferences 用法詳解
本文將介紹如何使用 SharedPreferences 儲存與讀取資料,並說明三種取得物件的方式與檔案的存放位置。 簡介 SharedPreferences 是資料儲存中
Android IPC 機制(一):IPC 概念與 Binder 運作原理
IPC 是什麼 IPC 是 Inter-Process Communication 的縮寫,指進程間通訊。在 Android 中,不同進程可能擁有自己的虛擬機與記憶體空間
深入解析 Android 6.0 的 LowMemoryKiller 機制
近來處理了一些與 LowMemoryKiller 相關的問題,於是對這個機制作了一輪梳理,在此整理成筆記。 背景知識 眾所周知,Android 的本質是一個 Linux
Android 遊戲開發:按鍵事件的響應處理
在遊戲開發中,按鍵事件的響應處理是一項基礎而重要的技能。本文將詳細說明 onKeyDown 與 onKeyUp 兩個方法的原理與用法,並透過範例演示如何在自定義 View