为什么我的EditText视图无法在Android中显示软键盘,即使我强制显示它?
我在片段中有一个EditText视图,当显示该片段时,我需要让它获得焦点,并弹出软键盘以允许用户直接输入,然而,EditText视图似乎只能获得焦点,但无法显示键盘,即使我手动强制它显示。用户必须触摸它才能打开软键盘。我试图得到一个最小的应用程序来复制这个,它只是一个带有编辑文本的活动。有什么想法吗 活动为什么我的EditText视图无法在Android中显示软键盘,即使我强制显示它?,android,android-edittext,android-textinputedittext,Android,Android Edittext,Android Textinputedittext,我在片段中有一个EditText视图,当显示该片段时,我需要让它获得焦点,并弹出软键盘以允许用户直接输入,然而,EditText视图似乎只能获得焦点,但无法显示键盘,即使我手动强制它显示。用户必须触摸它才能打开软键盘。我试图得到一个最小的应用程序来复制这个,它只是一个带有编辑文本的活动。有什么想法吗 活动 package com.example.litaos.testeditview; import android.content.Context; import android.support
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);
}
}
布局
<?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>
您尝试过添加requestFocus吗
您尝试过添加requestFocus吗
我认为仅仅要求专注是不够的。请尝试将以下代码添加到onCreate方法:
if(editText.requestFocus()) {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
我希望这对你有帮助 我认为仅仅要求焦点是不够的。请尝试将以下代码添加到onCreate方法:
if(editText.requestFocus()) {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
我希望这对你有帮助 哈,真管用!但我有一个片段可能需要隐藏键盘。我没有在那里尝试过,因为在这种情况下需要更多的时间来测试。将更新。谢谢很高兴我能帮上忙。哈,真管用!但我有一个片段可能需要隐藏键盘。我没有在那里尝试过,因为在这种情况下需要更多的时间来测试。将更新。谢谢很高兴我能帮忙。