android启动activity文本框不获得焦点

在开发中,常常会碰到这种情况,打开一个activity后,第一个文本框自动获得焦点,同时会弹出软键盘输入框,这样很影响用户体验,现在来看解决方法。

我们先来看看为什么会出现上述情况,原因很简单,文本框默认是会获得焦点的,获得焦点之后当然会继续弹出输入框,等待输入,针对此原因,我们可以有以下两种方案:

1、不让文本框获得焦点;
2、获得焦点不弹出输入框;

来看第一种方法,我们可以抢占文本框的焦点,如在其父窗体中加入:

 1     <LinearLayout
 2         xmlns:android="http://schemas.android.com/apk/res/android"
 3         xmlns:tools="http://schemas.android.com/tools"
 4         android:layout_width="match_parent"
 5         android:layout_height="match_parent"
 6         android:focusable="true"
 7         android:focusableInTouchMode="true"
 8         android:orientation="vertical"
 9         tools:context=".MainActivity" >
10 
11         <EditText
12             android:id="@+id/etMsg"
13             android:layout_width="wrap_content"
14             android:layout_height="wrap_content" />
15     </LinearLayout>

 来看第二种方法,在activity中加入:

1 android:windowSoftInputMode = "stateHidden"

 

reference:

http://my.oschina.net/helu/blog/142020

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。