简体   繁体   English

Android:键盘并不总是出现在edittext焦点上

[英]Android: Keyboard does not always appear on edittext focus

I have a Login view. 我有一个登录视图。 When the user starts the app, if he already synced with the server before, the app opens on the login view and the user is prompted to enter his login. 当用户启动应用程序时,如果他之前已经与服务器同步,则应用程序将在登录视图中打开,并提示用户输入其登录信息。 In this situation, the keyboard opens automatically when the app starts. 在这种情况下,应用程序启动时键盘会自动打开。 If, however, the user never synced before, the user is redirected to the configuration view and will sync from there. 但是,如果用户之前从未同步过,则会将用户重定向到配置视图并从那里进行同步。 After the sync is over, the configuration activity finishes and the user then sees the login view for the first time. 同步结束后,配置活动完成,然后用户第一次看到登录视图。 At this moment, the focus is on the PIN edit text, as it should, but the keyboard does not open. 此时,焦点在于PIN编辑文本,因为键盘不会打开。

I tried several solutions found online, none of which worked. 我尝试了几种在线发现的解决方案,但都没有。 This is my code in its current state: 这是我当前状态下的代码:

View: 视图:

@Override
    public void onResume() {
        super.onResume();

        EditText pinEditText = (EditText)findViewById(R.id.passwordEditText);
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        boolean dbContainsUsers = controller.Dispatcher.Count(new controller.User(), false) > 0;

        if (!dbContainsUsers) {
            Intent i = new Intent(getApplicationContext(), Configuration.class);

            startActivity(i);
        }

        pinEditText.clearFocus();
        pinEditText.requestFocus();
        imm.showSoftInput(pinEditText, InputMethodManager.SHOW_IMPLICIT);
    }

Layout: 布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="view.Login">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:orientation="vertical">

        <include layout="@layout/app_bar" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:orientation="vertical">

        <EditText
            android:id="@+id/passwordEditText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="@dimen/lgnEditTextMarginLeft"
            android:textSize="@dimen/textSizeL"
            android:ems="8"
            android:inputType="numberPassword"
            android:selectAllOnFocus="true" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:orientation="horizontal">

            <Button
                android:id="@+id/btnSynchroniser"
                style="@style/RexforetTheme.Button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="@dimen/btn_marge_top"
                android:drawableLeft="@drawable/icon_sync"
                android:textSize="@dimen/textSizeM"
                android:text="@string/btn_synchroniser" />

            <Button
                android:id="@+id/btnConfiguration"
                style="@style/RexforetTheme.Button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="@dimen/btn_marge_top"
                android:drawableLeft="@drawable/icon_configuration"
                android:textSize="@dimen/textSizeM"
                android:text="@string/btn_configuration" />
        </LinearLayout>
    </LinearLayout>



</RelativeLayout>

Manifest: 表现:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="ca.aaa.bbb">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/RexforetTheme">
        <activity android:name="view.Login">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"
                        android:windowSoftInputMode="stateVisible"/>

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="view.MenuPrincipal" />
        <activity android:name="view.Configuration" />

        <service
            android:name="service.WebDBService"
            android:exported="false"/>
    </application>

</manifest>

you have android:windowSoftInputMode="stateVisible" in this place replace 你有android:windowSoftInputMode =“stateVisible”在这个地方替换

android:windowSoftInputMode="stateHidden|adjustPan"

in your menifest file 在你的清单文件中

you have android:windowSoftInputMode="stateVisible" in this place replace 你有android:windowSoftInputMode =“stateVisible”在这个地方替换

android:windowSoftInputMode="adjustResize" 

in your manifest file. 在您的清单文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM