天天看點

Android 禁止軟鍵盤自動彈出

Android系統對EditText這個控件有監聽功能,如果某個Activity中含有該控件,就會自動彈出軟鍵盤讓你輸入,這個看似人性化的方案有時候并不被使用者喜歡的,是以在有些情況下要禁用該功能。這幾天做的應用也有這個問題,是以就查了,網上大部分都是如下方法: [html] view plain copy

  1. <activity android:name=".MainActivity"  
  2.                android:screenOrientation="landscape"  
  3.                <span style="color:#ff0000;">android:windowSoftInputMode="adjustPan|stateHidden"  
  4. </span>                <span style="color:#ff0000;">android:configChanges="orientation|keyboardHidden</span>">           
  5.                <intent-filter>  
  6.                    <action android:name="android.intent.action.MAIN"/>  
  7.                    <category android:name="android.intent.category.LAUNCHER"/>  
  8.                </intent-filter>         
  9.      </activity>   

該方法确實有用,但隻是在剛進入此Activity時能起到左右,如果該Activity中有Tab功能的切換,軟鍵盤又會彈出來,是以有了下面這個解決辦法:

在xml檔案中加入一個隐藏的TextView:

[html] view plain copy

  1. <TextView  
  2.         android:id="@+id/config_hidden"  
  3.         android:layout_width="wrap_content"  
  4.         android:layout_height="wrap_content"  
  5.         android:focusable="true"  
  6.         android:focusableInTouchMode="true"  
  7.         />  

然後再在Activity中加入:

[java] view plain copy

  1. TextView config_hidden = (TextView) this.findViewById(R.id.config_hidden);  

[java] view plain copy

  1. config_hidden.requestFocus();  

這樣軟鍵盤就不會彈出了。