Android:如何使键盘始终可见?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1509719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:09:55  来源:igfitidea点击:

Android: How to make the keypad always visible?

androidkeypad

提问by Prabhu R

In android, how do we make the device keypad always visible in the application? The top portion displays the content the application wants to render and bottom portion displays the keypad always.

在android中,我们如何使设备键盘在应用程序中始终可见?顶部显示应用程序想要呈现的内容,底部始终显示小键盘。

回答by Intrications

Add android:windowSoftInputMode="stateAlwaysVisible" to your activity in the AndroidManifest.xml file:

在 AndroidManifest.xml 文件中将 android:windowSoftInputMode="stateAlwaysVisible" 添加到您的活动中:

<activity android:name=".MainActivity"
android:label="@string/app_name"
android:windowSoftInputMode="stateAlwaysVisible" />

In my test app this shows the keyboard on starting of the application although it isn't fixed there but can be dismissed by pressing the back button.

在我的测试应用程序中,这显示了应用程序启动时的键盘,尽管它没有固定在那里,但可以通过按后退按钮来关闭。

To make sure the keyboard is always visible you might have to create your own keyboard as part of the UI of your application. Here is a tutorial to show you how to do this with KeyboardView: http://www.fampennings.nl/maarten/android/09keyboard/index.htm

为了确保键盘始终可见,您可能需要创建自己的键盘作为应用程序 UI 的一部分。这是一个教程,向您展示如何使用 KeyboardView 执行此操作:http: //www.fampennings.nl/maarten/android/09keyboard/index.htm

回答by Favas Kv

You must have an EditTextin your layout and that need to extent EditTextbase class. then Override onKeyPreIme()method, and return True. Now your keyboard will be always visible and can't be dismissed by Back key.

EditText的布局中必须有一个并且需要扩展EditText基类。然后覆盖onKeyPreIme()方法,并返回True。现在您的键盘将始终可见,并且无法通过后退键解除。

Caution: Because of your onKeyPreIme()method returns trueyou can't exit your app using back key.

注意:由于您的onKeyPreIme()方法返回,true您无法使用后退键退出您的应用程序。

Example:

例子:

public class CustomEdit extends EditText {

    public CustomEdit(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }
    @Override
    public boolean onKeyPreIme(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        Log.e("Log", "onKeyPreIme");
        return true;
        //return super.onKeyPreIme(keyCode, event);
    }
}

onKeyPreIme() - Android developer

onKeyPreIme() - Android 开发者

回答by user2314737

I found a way that works for me to keep the soft keyboard visible after an edit in my myEditTextfield of class EditText. The trick is to override the onEditorActionmethod so that it returns true

我找到了一种对我有用的方法,可以在我myEditText的课程领域进行编辑后保持软键盘可见EditText。诀窍是覆盖该onEditorAction方法,使其返回true

  myEditText.setOnEditorActionListener(new OnEditorActionListener() {                     
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
      return true;
    }       
  });

or else have onEditorActionreturn trueonly after the "Done" key-click (IME_ACTION_DONE) otherwise false

否则只有在“完成”键单击 ( )后才onEditorAction返回,否则trueIME_ACTION_DONEfalse

  myEditText.setOnEditorActionListener(new OnEditorActionListener() {                     
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
      if(actionId==EditorInfo.IME_ACTION_DONE){
        Log.i(LOG_TAG, "IME_ACTION_DONE");
        return true;    
      }
      return false;
    }       
  });

(see also this answeron the onEditorActionmethod)

(另见这个答案onEditorAction方法)

Adding android:windowSoftInputMode="stateAlwaysVisibleto the Manifest file helped to have the soft keyboard shown at activity start but it didn't prevent it from disappearing again whenever the "Done" key was clicked after an edit.

添加android:windowSoftInputMode="stateAlwaysVisible到清单文件有助于在活动开始时显示软键盘,但它并没有阻止它在编辑后单击“完成”键时再次消失。