Why my EditText view cannot bring up soft keyboard in Android even I force it to show?

岁酱吖の 提交于 2019-12-11 05:08:17

问题


I have an EditText view in a fragment and when showing that fragment, I need to let it get focus and the soft keyboard pops up to allow user type in directly, however, the EditText view seems can only get focus but not show the keyboard even though I manually force it to show. The user has to touch it to bring up the soft keyboard. I tried to get a minimum app to reproduce this, it's simply an activity with an EditText. Any idea?

Activity

package com.example.litaos.testeditview;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EditText editText = findViewById(R.id.edit_text);
        editText.requestFocus();
        final InputMethodManager imm = (InputMethodManager) getApplicationContext().getSystemService(
                Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
    }
}

Layout

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.litaos.testeditview.MainActivity">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/edit_text"
        android:hint="Hello World!"
        android:focusable="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

回答1:


I think it's not enough to just request focus. Please try adding following code to your onCreate method:

if(editText.requestFocus()) {
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}

I hope this helps you!




回答2:


Have you tried adding requestFocus ?

                <EditText
                    android:id="@+id/et_email_address"
                    style="@style/baseEditTextStyle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp" >
                    <requestFocus/>
                </EditText>


来源:https://stackoverflow.com/questions/49364025/why-my-edittext-view-cannot-bring-up-soft-keyboard-in-android-even-i-force-it-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!