手记

EditText 如何不让其自动获取焦点

在项目中,一进入一个页面, EditText默认就会自动获取焦点。

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

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

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

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

这样,就把EditText默认的行为截断了!
复制代码

1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:background="#CFD4DA"
6 android:orientation="vertical" >
7
8 <!-- 查询 增加 -->
9
10 <LinearLayout
11 android:layout_width="fill_parent"
12 android:layout_height="40dp"
13 android:background="#A1AEBF"
14 android:focusable="true"
15 android:focusableInTouchMode="true"
16 android:orientation="horizontal" >
17
18 <EditText
19 android:padding="2dp"
20 android:id="@+id/et_query"
21 android:layout_width="wrap_content"
22 android:layout_height="wrap_content"
23 android:layout_weight="4"
24 android:maxLength="13" />
25
26 <Button
27 android:id="@+id/bt_query"
28 android:layout_width="wrap_content"
29 android:layout_height="wrap_content"
30 android:layout_weight="0.5"
31 android:text="查询" />
32
33 <Button
34 android:id="@+id/bt_add"
35 android:layout_width="wrap_content"
36 android:layout_height="wrap_content"
37 android:layout_weight="0.5"
38 android:text="增加" />
39 </LinearLayout>
40
41 <ListView
42 android:id="@+id/lv_info_query_send"
43 android:layout_width="match_parent"
44 android:layout_height="match_parent"
45 android:layout_marginTop="20dp" />
46
47 </LinearLayout>

3人推荐
随时随地看视频
慕课网APP