您的位置:首页 > 移动开发 > Android开发

Android 如何让EditText不自动获取焦点,默认未编辑状态

2014-07-21 13:50 423 查看
在项目中,一进入一个页面, EditText默认就会自动获取焦点。

那么如何取消这个默认行为呢?

 

在网上找了好久,有点 监听软键盘事件,有点 调用 clearFouse()方法,但是测试了都没有! xml中也找不到相应的属性可以关闭这个默认行为

 

解决之道:在EditText的父级控件中找一个,设置成

   android:focusable="true"  
   android:focusableInTouchMode="true"

这样,就把EditText默认的行为截断了!

 

 

<LinearLayout 

        style="@style/FillWrapWidgetStyle"

        android:orientation="vertical"

        android:background="@color/black"

        android:gravity="center_horizontal"

        
        android:focusable="true"  
        android:focusableInTouchMode="true"

        >

        <ImageView

            android:id="@+id/logo"

            style="@style/WrapContentWidgetStyle"

            android:background="@drawable/dream_dictionary_logo"

          />

        <RelativeLayout 

            style="@style/FillWrapWidgetStyle"

            android:background="@drawable/searchbar_bg"

            android:gravity="center_vertical"

            >
            <EditText
                android:id="@+id/searchEditText"
               style="@style/WrapContentWidgetStyle"
               android:background="@null"
               android:hint="Search"
               android:layout_marginLeft="40dp"
               android:singleLine="true"
             />

            

        </RelativeLayout>

        

    </LinearLayout>

不可编辑状态

<EditText

                    android:id="@+id/ed_newPwd"

                    android:layout_width="80dp"

                    android:layout_height="50dp"

                    android:focusable="false"/>

 

可编辑状态

ed_adress.setFocusable(true);

ed_adress.requestFocus();

ed_adress.setFocusableInTouchMode(true);

完美解决

别忘了关闭键盘进入不可编辑状态的时候

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);  

//得到InputMethodManager的实例

if (imm.isActive()) {

//如果开启

imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS); 

//关闭软键盘,开启方法相同,这个方法是切换开启与关闭状态的

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Android textview 焦点