在Android开发中如何移除EditText上的输入焦点

  当我们创建一个带EditText 或 AutoCompleteTextView的视图时,在加载视图时总是会把输入的焦点自动移动到第一个输入框。如下图所示:

带EditText 或 AutoCompleteTextView的视图

  下面是mail.xml布局文件:

 
  
1 . <!--? xml version = " 1.0 " encoding = " utf-8 " ?-->
2 . < linearlayout android:layout_height = " fill_parent " android:layout_width = " fill_parent " android:orientation = " vertical " xmlns:android = " http://schemas.android.com/apk/res/android " >
3 . < button android:id = " @+id/Button01 " android:layout_height = " wrap_content " android:layout_width = " wrap_content " android:text = " @string/button_text " >
4 . < / button>
5 . < edittext android:hint = " @string/hint " android:id = " @+id/EditText01 " android:layout_height = " wrap_content " android:layout_width = " wrap_content " android:text = "" >
6 . < / edittext>
7 . < button android:id = " @+id/Button02 " android:layout_height = " wrap_content " android:layout_width = " wrap_content " android:text = " @string/button_text " >
8 . < / button>
9 . < / linearlayout>
10 .

  我们如何才能把焦点从EditText上移除呢?最简单的方法是创建一个不可见的(invisible)LinearLayout,LinearLayout将会把焦点从EditText上移走。

  我们修改mail.xml布局文件,在EditText之前增加一个LinearLayout ,如下所示:

 
  
1 . <!--? xml version = " 1.0 " encoding = " utf-8 " ?-->
2 . < linearlayout android:layout_height = " fill_parent " android:layout_width = " fill_parent " android:orientation = " vertical " xmlns:android = " http://schemas.android.com/apk/res/android " >
3 . < button android:id = " @+id/Button01 " android:layout_height = " wrap_content " android:layout_width = " wrap_content " android:text = " @string/button_text " >
4 . < / button>
5 . < linearlayout android:focusable = " true " android:focusableintouchmode = " true " android:layout_height = " 0px " android:layout_width = " 0px " >
6 . < edittext android:hint = " @string/hint " android:id = " @+id/EditText01 " android:layout_height = " wrap_content " android:layout_width = " wrap_content " android:text = "" >
7 . < / edittext>
8 . < button android:id = " @+id/Button02 " android:layout_height = " wrap_content " android:layout_width = " wrap_content " android:text = " @string/button_text " >
9 . < / button>
10 . < / linearlayout>
11 . < / linearlayout>

  最终效果如下所示:

最终效果

  不需要任何代码就把焦点移除了,是不是最简单的解决方案?

  希望本文对您有所帮助!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值