2010-11-10 97 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); 

还是做同样的事情,但光标仍然存在。

相关问题