2010-11-10 140 views
31

當我的ScrollView佈局和EditText s活動開始時,EditText會變得焦點並打開Android屏幕上鍵盤。當活動開始時,屏幕鍵盤會自動打開

我怎樣才能避免這種情況?

當我使用LinearLayoutRelativeLayout沒有ScrollView它不會發生。

我已經嘗試過這種方式,和它的作品,但它不是一個好辦法做到這一點:

TextView TextFocus = (TextView) findViewById(R.id.MovileLabel); 
TextFocus.setFocusableInTouchMode(true); 
TextFocus.requestFocus(); 

接下來你有我的一些佈局這個問題的一個例子,當這種活動開始,焦點將轉到第一個EditText說明和Android鍵盤會自動打開,這是很煩人的。

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView android:id="@+id/ScrollView01" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:padding="10px"> 

     <RelativeLayout 
      android:gravity="center_vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"> 
      <TextView 
       android:id="@+id/UserLabel" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="13px" 
       android:text="@string/userlabel"/> 
      <TextView 
       android:id="@+id/User" 
       android:layout_alignBaseline="@id/UserLabel" 
       android:layout_alignParentRight="true" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="test"/> 
     </RelativeLayout> 

     <View 
      android:layout_gravity="center_horizontal" 
      android:layout_width="fill_parent" 
      android:layout_height="1dip" 
      android:background="#808080" 
      android:layout_marginTop="5px" 
      android:layout_marginBottom="12px"/> 

     <RelativeLayout 
      android:gravity="center_vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"> 
      <TextView 
      android:id="@+id/DescriptionLabel" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/desclabel" 
      android:layout_marginTop="13px"/> 
      <EditText 
      android:id="@+id/Description" 
      android:layout_alignBaseline="@id/DescriptionLabel" 
      android:layout_alignParentRight="true" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:width="180px"/> 
     </RelativeLayout> 

     <RelativeLayout 
      android:gravity="center_vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"> 
      <TextView 
       android:id="@+id/EmailLabel" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/emaillabel" 
       android:layout_marginTop="13px"/> 
      <EditText 
       android:id="@+id/Email" 
       android:layout_alignBaseline="@+id/EmailLabel" 
       android:layout_alignParentRight="true" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:width="180px"/> 
     </RelativeLayout> 

     <RelativeLayout 
      android:gravity="center_vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"> 
      <TextView 
       android:id="@+id/MovilePhoneLabel" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/movilephonelabel" 
       android:layout_marginTop="13px"/> 
      <EditText 
       android:id="@+id/MovilePhone" 
       android:layout_alignBaseline="@+id/MovilePhoneLabel" 
       android:layout_alignParentRight="true" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:width="180px"/> 
     </RelativeLayout> 

     <View 
      android:layout_gravity="center_horizontal" 
      android:layout_width="fill_parent" 
      android:layout_height="1dip" 
      android:background="#808080" 
      android:layout_marginTop="5px" 
      android:layout_marginBottom="10px"/> 


     <RelativeLayout 
      android:gravity="center_vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"> 
      <Button 
       android:id="@+id/applybutton" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/apply" 
       android:width="100px" 
       android:layout_marginLeft="40dip"/> 
      <Button 
       android:id="@+id/cancelbutton" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/cancel" 
       android:width="100px" 
       android:layout_alignBaseline="@+id/applybutton" 
       android:layout_alignParentRight="true" 
       android:layout_marginRight="40dip"/> 
     </RelativeLayout> 
    </LinearLayout> 
</ScrollView> 
+0

我測試了,是的,如果我從我的佈局中刪除滾動視圖,Android鍵盤doesn'ts自動打開!問題是爲scrollview,但我需要它:S可以有人幫助? – NullPointerException 2010-11-10 22:16:17

回答

89

的Android會自動打開OnScreenKeyboard如果你有一個EditText活動開始時集中。

您可以防止通過添加以下到您的活動的onCreate方法。

getWindow().setSoftInputMode(
    WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
+0

它的作品!謝謝!!! – NullPointerException 2010-11-11 10:13:50

+1

是否總是隱藏部分的意思是顯示不出來,如果我挖掘的EditText? – ToothlessRebel 2011-08-02 14:50:49

+0

@ToothlessRebel:是的! – nithinreddy 2011-10-01 05:14:25

17

如果你想這樣做編輯AndroidManifest:

 <activity 
     android:name=".Dades" 
     android:label="@string/app_name" 
     android:windowSoftInputMode="stateHidden"> 

android:windowSoftInputMode="stateHidden"是防止鍵盤焦點之一。

+0

因此未爲我工作 – Killer 2015-12-22 10:24:19

0

您可以使用下面的語句也

(EditTextName).setInputType(0)

它將永久隱藏perticular EDITTEXT的默認鍵盤,即使在EditText上輕觸或點擊。

+0

以及如何再次打開鍵盤? – AndroidGeeks 2015-07-27 09:11:37

11

另一種方式是通過添加上的LinearLayout:

android:focusable="true" 

android:focusableInTouchMode="true" 

讓我表明

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 

還是做同樣的事情,但光標仍然存在。