Android取消Edittext默认获取焦点
16lz
2021-01-23
Android取消Edittext默认获取焦点
Android控件EditText自动默认会获取屏幕焦点,并显示输入键盘,有时候我们需要取消这个行为,
只需要两行代码,如下:
android:focusable="true" android:focusableInTouchMode="true"
我们只需要把这两行代码添加至EditText控件的父级控件中,把屏幕焦点设置到父级控件上,EditText就会暂时失去焦点。
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:focusable="true" android:focusableInTouchMode="true" android:orientation="vertical" > <EditText android:id="@+id/et_user_name" android:layout_width="wrap_content" android:layout_height="wrap_content" /> LinearLayout>
万一我们的页面中有多个Edittext呢?
其实可以放在多级父控件中,甚至放在根目录的布局中都是可以实现:取消获取子控件(或多级子控件)默认获取焦点。
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:focusable="true" android:focusableInTouchMode="true" android:orientation="vertical">....//各种子控件LinearLayout>
这里不用担心以后不会重新获得焦点,上面两句修改只是让默认显示的情况不获取焦点,当你点击Edittext的时候,还是会有焦点,并显示虚拟键盘的。
共勉:没有伞的孩子更应该学会在雨中奔跑
更多相关文章
- Android XML�O置屏幕方向(android:screenOrientation)详解
- Android基础 | 控件基础 | ImageView用法
- 高级控件之网格视图(GridView)
- Android圆环控件
- Android控件阴影效果的几种实现方法
- Android UI基础——ImageView&ImageButton控件
- android 各种控件颜色值
- android控件的对齐方式转讲
- Android设置任何控件透明度