Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

EditText 點擊全選、再點取消全選的實作

編輯:關於 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>

MainActivity 實作

做法是攔截 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 在視窗中的實際座標去比對觸碰點,實作時記得把狀態列與導覽列的高度差算進去。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved