OnScreen keyboard opens automatically when Activity starts(OnScreen 键盘在 Activity 启动时自动打开)
问题描述
当我的带有 ScrollView 布局和 EditTexts 的 Activity 启动时,EditTexts 获得焦点并且 Android OnScreen 键盘打开.
When my Activity with a ScrollView layout and EditTexts starts, the EditTexts get focus and the Android OnScreen keyboard opens.
我怎样才能避免这种情况?
How I can avoid that?
当我在没有 ScrollView 的情况下使用 LinearLayout 和 RelativeLayout 时,它不会发生.
When I was using LinearLayout and RelativeLayout without the ScrollView it doesn't happen.
我已经尝试过这种方式,它可以工作,但这不是一个好方法:
I've tried it this way, and it works, but it's not a good way to do it:
TextView TextFocus = (TextView) findViewById(R.id.MovileLabel);
TextFocus.setFocusableInTouchMode(true);
TextFocus.requestFocus();
接下来你有一个我的一些布局的例子,当这个 Activity 启动时,焦点转到第一个 EditText,Description 并且 Android 键盘自动打开,这很烦人.
Next you have an example of some of my layouts with this problem, when this Activity starts, focus goes to the first EditText, Description and the Android keyboard opens automatically, this is very annoying.
<?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>
推荐答案
如果你在Activity启动时有一个EditText焦点,Android会自动打开OnScreenKeyboard.
Android opens the OnScreenKeyboard automatically if you have an EditText focussed when the Activity starts.
您可以通过在 Activity 的 onCreate 方法中添加以下内容来防止这种情况发生.
You can prevent that by adding following into your Activity's onCreate method.
 getWindow().setSoftInputMode(
    WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
                        这篇关于OnScreen 键盘在 Activity 启动时自动打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:OnScreen 键盘在 Activity 启动时自动打开
				
        
 
            
        - Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
 - Android viewpager检测滑动超出范围 2022-01-01
 - 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
 - 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
 - 用 Swift 实现 UITextFieldDelegate 2022-01-01
 - MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
 - 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
 - Android - 拆分 Drawable 2022-01-01
 - android 4中的android RadioButton问题 2022-01-01
 - 想使用ViewPager,无法识别android.support.*? 2022-01-01
 
				
				
				
				