当前位置: 移动技术网 > IT编程>移动开发>Android > Android 中使用EditText 点击全选再次点击取消全选功能

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

2019年07月24日  | 移动技术网IT编程  | 我要评论

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

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

代码很简单,这里我简化了逻辑,页面只有一个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 点击全选再次点击取消全选功能,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网