Android 中使用EditText 点击全选再次点击取消全选功能
作者:尹小贱 发布时间:2023-09-08 00:08:44
标签:android,edittext,全选
最近在开发浏览器碰到这么一个需求:点击地址栏的时候,需要全选并调出键盘,再次点击就取消全选显示光标。点击屏幕除地址栏其他位置时,键盘隐藏,隐藏光标。
大部分浏览器都是这样的逻辑,这样可以提高用户体验,减少操作。
代码很简单,这里我简化了逻辑,页面只有一个EditText。
布局文件如下:里面有两个属性需要注意
android:focusable="true"
android:selectAllOnFocus="true"
完整布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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">
<EditText
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:selectAllOnFocus="true"
/>
</RelativeLayout>
**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 点击全选再次点击取消全选功能网站的支持!
来源:http://blog.csdn.net/qq_30835655/article/details/53538198


猜你喜欢
- 一、原文翻译WorkManager API 可以很容易的指定可延迟的异步任务。允许你创建任务,并把它交给WorkManager来立即运行或在
- 前言:现在的手机品牌和型号越来越多,导致我们平时写布局的时候会在个不同的移动设备上显示的效果不同,比如我们的设计稿一个View的大小是300
- using System;using System.Collections.Generic;using System.Text;namesp
- 最近研究了一下如何在Android上实现CoverFlow效果的控件,其实早在2010年,就有Neil Davies开发并开源出了这个控件,
- Android的主线程中执行长时间操作,导致界面无响应,会引起ANR。如果需要执行较长时间的操作,一般会在另一个线程处理,然后将数据转交给主
- Android N 中推出了多窗口支持,项目要求适配多窗口模式,记录一下。1.生命周期:对于完全没有适配多窗口的APP来说,当启用多窗口模式
- Mybatis动态排序 #{} ${}问题在写Mybatis动态排序是遇到一个问题,开始,我是这样写的<if test="o
- 一.Unsafe类的源码分析JDK的rt.jar包中的Unsafe类提供了硬件级别的原子操作,Unsafe里面的方法都是native方法,通
- Quartz是一款开源的定时任务调度框架,Quartz的官网是:http://www.quartz-s
- Criteria的and和or进行联合查询DemoExample example=new DemoExample ();DemoExampl
- 前言如果你想玩转C# 里面多线程,工厂模式,生产者/消费者,队列等高级操作,就可以和我一起探索这个强大的线程安全提供阻塞和限制功能的C#神器
- FileStream,顾名思义,文件流。流,是字节流。我的理解是,硬盘上存在一个字节流,内存里也有一个字节流,它们是对应的。程序运行时,我们
- 本文实例讲述了Spring使用@Resource配置依赖操作。分享给大家供大家参考,具体如下:一 配置<?xml version=&q
- 本文实例为大家分享了Android Studio实现帧动画的具体代码,供大家参考,具体内容如下按一定的顺序播放静态的图片1、几张联系的图片2
- 描述说明:public class TryCatchStu { /*try catch:自己处理异常 *t
- 参考资料:alibaba-easyexcel.github.io简介EasyExcel是一个基于Java的简单、省内存的读写Excel的开源
- 普通校验导入依赖:默认的报错:没有提示具体的属性设置自己的错误信息提示:创建 ValidationMessages.properties内容
- 1 前言为什么我们在使用SpringBoot框架开发Java Web应用需要引入大量的starter?例如,我们引入Redis就在Maven
- 前言在《C# wpf Canvas中实现控件动态调整大小》中我们实现了Canvas中的控件动态调整大小,由于Grid也是可层叠布局,在Gri
- Struts提供了一个更简单的方式来处理未捕获的异常,并将用户重定向到一个专门的错误页面。您可以轻松地Struts配置到不同的异常有不同的错