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
0
投稿
猜你喜欢
- 首先下载newtonsoft.json,增加引用using newtonsoft.json.linq;把jobject的内容提取出来,job
- 一个简单的拼图小游戏,供大家参考,具体内容如下1.首先设计视图面板。2.添加所需要的图片按钮。3.最主要的是设计监听事件,添加图片的监听按钮
- 本文实例讲述了Android开发之imageView图片按比例缩放的实现方法。分享给大家供大家参考,具体如下:android:scaleTy
- 在作应用系统开发时,管理配置是必不可少的。例如数据库服务器的配置、安装和更新配置等等。由于Xml的兴起,现在的配置文件大都是以xml文档来存
- Android RollPagerView实现轮播图android图片轮播效果,RollViewPager的简单使用 <c
- using System; using System.Collections.Generic; using
- 尝试了各种防止中文乱码的方式,但是还是乱码;最后还是细节问题导致;解决方式:以及俩种方式是百度的,我的问题不是这俩块1.在requestMa
- 数据库里面表的字段中带有“”_“下划线,我们知道插件默认的是将这些带有下划线的字段默认的变成“优美的驼峰式”的。表是肯定不能动的,实体类的字
- 一. 项目需求我们做项目的时候,数据量比较大,单表千万级别的,需要分库分表,于是在网上搜索这方面的开源框架,最常见的就是mycat,shar
- 俗话说,工欲善其事,必先利其器。要做微信公众号开发,两样东西不可少,那就是要有一个用来测试的公众号,还有一个用来调式代码的开发环境。测试公众
- 目录1、Stream API2、ParallelStreams执行原理3、ParallelStreams注意事项前言:并行编程势不可挡,Ja
- 前言Dijkstra算法是最短路径算法中为人熟知的一种,是单起点全路径算法。该算法被称为是“贪心算法”的成功典范。本文接下来将尝试以最通俗的
- 描述说明:public class TryCatchStu { /*try catch:自己处理异常 *t
- 详解java 中Spring jsonp 跨域请求的实例jsonp介绍  
- 1、为什么要进行启动优化网上流行一种说法,就是8秒定律,意思是说,如果用户在打开一个页面,在8秒的时间内还没有打开,那么用户大概的会放弃掉,
- 实现跨服务的远程调用(RestTemplate)业务场景:在返回订单信息数据中显示用户信息实现思路:基于RestTemplate发起的htt
- 1、通过注解ModelAttribute直接映射表单中的参数到POJO。在from中的action写提交的路径,在input的name写参数
- 当我们开发spring web应用程序时,对于如 IOException , ClassNotFoundException 之类的检查异常,
- 目录@ConfigurationProperties使用@ConfigurationProperties特点宽松绑定支持复杂属性类型激活@C
- 本文实例为大家分享了Android实现图片点击放大的具体代码,供大家参考,具体内容如下在我的项目中,有点击图片banner后放大浏览的功能。