日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

点击编辑框全选内容java_Android 中使用EditText 点击全选再次点击取消全选功能

發布時間:2025/3/12 Android 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 点击编辑框全选内容java_Android 中使用EditText 点击全选再次点击取消全选功能 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近在開發瀏覽器碰到這么一個需求:點擊地址欄的時候,需要全選并調出鍵盤,再次點擊就取消全選顯示光標。點擊屏幕除地址欄其他位置時,鍵盤隱藏,隱藏光標。

大部分瀏覽器都是這樣的邏輯,這樣可以提高用戶體驗,減少操作。

代碼很簡單,這里我簡化了邏輯,頁面只有一個EditText。

布局文件如下:里面有兩個屬性需要注意

android:focusable="true"

android:selectAllOnFocus="true"

完整布局文件

xmlns:tools="http://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">

android:id="@+id/edit"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:focusable="true"

android:selectAllOnFocus="true"

/>

**mainactivity.java

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();

以上所述是小編給大家介紹的Android 中使用EditText 點擊全選再次點擊取消全選功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!

總結

以上是生活随笔為你收集整理的点击编辑框全选内容java_Android 中使用EditText 点击全选再次点击取消全选功能的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。