Android 中使用EditText 点击全选再次点击取消全选功能

Android 中使用EditText 点击全选再次点击取消全选功能,第1张

概述最近在开发浏览器碰到这么一个需求:点击地址栏的时候,需要全选并调出键盘,再次点击就取消全选显示光标。点击屏幕除地址栏其他位置时,键盘隐藏,隐藏光标。

最近在开发浏览器碰到这么一个需求:点击地址栏的时候,需要全选并调出键盘,再次点击就取消全选显示光标。点击屏幕除地址栏其他位置时,键盘隐藏,隐藏光标。

大部分浏览器都是这样的逻辑,这样可以提高用户体验,减少 *** 作。

代码很简单,这里我简化了逻辑,页面只有一个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 点击全选再次点击取消全选功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!

总结

以上是内存溢出为你收集整理的Android 中使用EditText 点击全选再次点击取消全选功能全部内容,希望文章能够帮你解决Android 中使用EditText 点击全选再次点击取消全选功能所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1147512.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存