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

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

代码很简单,这里我简化了逻辑,页面只有一个EditText。

布局文件如下:里面有两个属性需要注意

android:focusable="true"android:selectAllOnFocus="true"

完整布局文件

<?xml version="1.0" encoding="utf-8"?>  

**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 点击全选再次点击取消全选功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

更多相关文章

  1. Android(安卓)ViewPager+Fragment滑动选项卡,tab点击选项卡
  2. Android开发问题之布局(layout)文件图形界面不能显示
  3. android 的Java代码中的布局相关方法LayoutParams
  4. 安卓布局中layout_gravity和gravity的区别
  5. Android扩大控件的点击区域
  6. Android中的4.0新布局控件:Space和GridLayout
  7. Android:自定义输入框光标颜色
  8. Android使用recycleView组件
  9. Android的merge标签用法

随机推荐

  1. Android(安卓)lint 删除无用图片文件和配
  2. android抓取网络通讯包
  3. arcgis for android 环境配置
  4. 极光推送服务集成指南(一)
  5. select在ios和android中样式不兼容
  6. 关于android.os.NetworkOnMainThreadExce
  7. Bitmap的回收
  8. [Xamarin.Android]使用SqliteNET (转帖)
  9. Android通过Intent发送电子邮件含附件
  10. 读书目录