Android取消Edittext默认获取焦点

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呢?
其实可以放在多级父控件中,甚至放在根目录的布局中都是可以实现:取消获取子控件(或多级子控件)默认获取焦点。


<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)