【问题标题】:OnScreen keyboard opens automatically when Activity startsOnScreen 键盘在 Activity 启动时自动打开
【发布时间】:2010-11-10 21:46:25
【问题描述】:

当我的带有ScrollView 布局和EditTexts 的Activity 启动时,EditTexts 获得焦点并且Android OnScreen 键盘打开。

我怎样才能避免这种情况?

当我使用 LinearLayoutRelativeLayout 而没有 ScrollView 时,它不会发生。

我已经尝试过这种方式,它可以工作,但这不是一个好方法:

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

接下来你有一个我的一些布局有这个问题的例子,当这个Activity启动时,焦点转到第一个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>

【问题讨论】:

  • 我对其进行了测试,是的,如果我从布局中删除滚动视图,Android 键盘不会自动打开!!!这是滚动视图的问题,但我需要它:有人可以帮忙吗?

标签: android keyboard focus scrollview


【解决方案1】:

如果您在 Activity 启动时聚焦 EditText,Android 会自动打开 OnScreenKeyboard。

您可以通过在 Activity 的 onCreate 方法中添加以下内容来防止这种情况发生。

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

【讨论】:

  • 总是隐藏的部分是否意味着如果我点击 EditText 它不会显示?
  • @ToothlessRebel:是的..确实如此!
  • 我正在使用这个 android:windowSoftInputMode="stateHidden" 这对我不起作用
【解决方案2】:

如果您想编辑 AndroidManifest:

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

android:windowSoftInputMode="stateHidden" 行是阻止键盘焦点的行。

【讨论】:

    【解决方案3】:

    另一种方法是在 LinearLayout 上添加:

    android:focusable="true"
    
    android:focusableInTouchMode="true"
    

    让我指出

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

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

    【讨论】:

      【解决方案4】:

      这是不恰当的行为

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

      但它会顺利完成(在 manifest 中进行该活动)

       android:windowSoftInputMode="stateHidden"
      

      【讨论】:

        【解决方案5】:

        你也可以使用下面的语句,

        (EditTextName).setInputType(0)

        它将永久隐藏特定 Edittext 的默认键盘,即使在 EditText Touch 或 Click 上也是如此。

        【讨论】:

        • 以及如何再次打开键盘?
        猜你喜欢
        • 2011-06-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-06
        • 1970-01-01
        • 1970-01-01
        • 2020-11-11
        • 2011-01-17
        相关资源
        最近更新 更多