美文网首页
EditText焦点问题

EditText焦点问题

作者: _戏_梦 | 来源:发表于2018-05-31 10:24 被阅读15次
  1. 禁止EditText自动获取焦点

在EditText的父布局中加入下面属性即可

 android:focusable="true"//是否可聚焦
 android:focusableInTouchMode="true"//是否是触摸方式获取焦点
  1. 解决有EditText的界面,软键盘和EditText焦点切换的问题
/**
 * 处理EditText焦点问题
 */
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        View v = getCurrentFocus();
        if (v instanceof EditText) {
            Rect outRect = new Rect();
            v.getGlobalVisibleRect(outRect);
            if (!outRect.contains((int) event.getRawX(), (int) event.getRawY())) {
                v.setFocusable(false);
                v.setFocusableInTouchMode(true);
                InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
            }
        }
    }
    return super.dispatchTouchEvent(event);
}
  1. 避免有两个EditText时,两个EditText互相争夺焦点的问题

解决方法:方法1和方法2共同使用

相关文章

网友评论

      本文标题:EditText焦点问题

      本文链接:https://www.haomeiwen.com/subject/yqzfsftx.html