Android: How to make the keypad always visible?

2020-01-23 17:08发布

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.

3条回答
小情绪 Triste *
2楼-- · 2020-01-23 17:50

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

查看更多
等我变得足够好
3楼-- · 2020-01-23 18:00

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.

查看更多
beautiful°
4楼-- · 2020-01-23 18:06

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

查看更多
登录 后发表回答