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

发布于 2024-08-07 02:38:11 字数 66 浏览 5 评论 0原文

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吹泡泡o 2024-08-14 02:38:11

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

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

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

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

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

<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

农村范ル 2024-08-14 02:38:11

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

警告:由于您的 onKeyPreIme() 方法返回 true,因此您无法使用后退键退出应用。

示例:

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 开发者

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

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

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

如痴如狂 2024-08-14 02:38:11

我找到了一种方法,可以在编辑 EditText 类的 myEditText 字段后保持软键盘可见。诀窍是重写 onEditorAction 方法,使其返回 true

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

,否则只有在 onEditorAction 之后才返回 true “完成”键单击 (IME_ACTION_DONE< /a>) 否则 false

  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;
    }       
  });

(另请参阅 onEditorAction 上的此答案方法)

android:windowSoftInputMode="stateAlwaysVisible 添加到 Manifest 文件有助于在 Activity 启动时显示软键盘,但并不能阻止软键盘在每次单击“Done”键后再次消失。编辑。

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

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

or else have onEditorAction return true only after the "Done" key-click (IME_ACTION_DONE) otherwise false

  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 answer on the onEditorAction method)

Adding android:windowSoftInputMode="stateAlwaysVisible to 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文