Android 如何让EditText不自动获取焦点&隐藏软键盘
感谢大佬:https://blog.csdn.net/a18615971648/article/details/72869345
有时候的项目当中进入某个页面edittext会自动获取焦点弹出软键盘,用户体验非常不好,那么如何避免这种情况呢?在网上查了一下大概有三种方法。
第一种:设置一个默认的View,在页面加载的时候调用requFocus()方法,前提是该View的setFocusable()要设置为true
第二种:直接调用edittext的clearFocus()方法,不过该方法有时候会不生效
第三种:在布局文件中给edittext的父控件增加两个属性
android:focusable="true"
android:focusableInTouchMode="true"
例如:
android:layout_height="50dp"
android:background="@drawable/bg_input"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:gravity="center_vertical" android:focusable="true"
android:focusableInTouchMode="true" >
android:layout_width="0dp"
android:layout_weight="5"
android:layout_height="wrap_content"
android:hint="手机号码"
android:layout_toRightOf="@id/iv_login"
android:paddingLeft="5dp"
android:inputType="number"
android:background="@null"
另外还有一种方法是强制隐藏输入法,但并不会使edittext失去焦点
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
补充:
参考:https://ask.csdn.net/questions/39
个人学习笔记!仅以学习为目的,感谢各位前辈!

浙公网安备 33010602011771号