简体   繁体   中英

Android lollipop can`t get keyboard input

I have user keyboard input working on all android versions except on Android Lollipop (5.0).

I have used this to open software keyboard:

 public static void OpenKeyBoard(){     
MainActivity._Instance.runOnUiThread(new Runnable() {
    public void run() {         
        InputMethodManager imm = (InputMethodManager)    MainActivity._Instance.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(MainActivity._Instance.getWindow().getDecorView(), 0);        
    }
});                     

}

and i get user input by standard event :

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{   

I have this code working for all pre Lollipop versions of Android. When I use it on Lollipop, the software keyboard appears, but when I try to click on any letter/number, the keyboard disappears and the method "onKeyDown" doesn`t receive any keycode.

Did anyone had this problem? Any opinion how to solve this?

Thank you.

尝试将您的google SDK更新到最新版本,我这样做了,这解决了键盘关闭时出现的所有问题。

I has this problem too. I managed to solve this by overriding the onLayout, onFocusChanged & onCheckIsTextEditor methods inside WebView. Here's the code that made it work (I generate a webview programmatically):

        this.webView = new WebView(context)
        {
            boolean layoutChangedOnce = false;

            @Override
            protected void onLayout(boolean changed, int l, int t, int r, int b)
            {
                if (!layoutChangedOnce)
                {
                    super.onLayout(changed, l, t, r, b);
                    layoutChangedOnce = true;
                }
            }

            @Override
            protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect)
            {
                super.onFocusChanged(true, direction, previouslyFocusedRect);
            }

            @Override
            public boolean onCheckIsTextEditor()
            {
                return true;
            }
        };

        this.webView.setFocusable(true);
        this.webView.setFocusableInTouchMode(true);
        this.webView.requestFocus(View.FOCUS_DOWN);

        addView(this.webView);

this issue is not limited to just android applications but also occurs when any keyboard related event is accessed using javascript or jquery within a phone browser.

eg. I have my website which asks a user to enter zip code(numeric characters) on a certain page. Now the problem is when up tap on the input box, the numeric keyboard appears but it allows you to paste alpha-numeric characters in it as well.

Reason searched so far, Try accessing this through your desktop and also a mobile device running android OS 5.0+ and try different possible keystrokes.

Note:

  1. When you enter any alpha-characters using mobile device with above mentioned configuration it shows up '229' as the keycode.

  2. When you enter any alpha-characters using a desktop it shows up the appropriate keycode.

This works for me I have a SurfaceView implements SurfaceHolder.Callback with a thread to do drawing

@Override
public boolean onTouchEvent(MotionEvent event)
{
    synchronized (this)
    {
        mouse.useEvent(event);
    }
    return true;
}


@Override
public InputConnection onCreateInputConnection(EditorInfo editorinfo) {

    BaseInputConnection bic = new BaseInputConnection(this, false);
    editorinfo.actionLabel = null;
    editorinfo.inputType = InputType.TYPE_NULL;
    editorinfo.imeOptions = EditorInfo.IME_FLAG_NO_FULLSCREEN;
    bic.finishComposingText();
    return bic;
}

@Override
public boolean dispatchKeyEvent(KeyEvent event)
{

    input.useEvent(event);
    return super.dispatchKeyEvent(event);
}

I have solved this using AlertDialog editText programmatically. Eg:

public void KeyboardTextField(int id){              
    if (id == 0){
        final String title = getStringResourceByName("profile_title");       
        final String createP = getStringResourceByName("profile_confirm");
        final String cancel = getStringResourceByName("profile_cancel");    

        MainActivity._Instance.ActivityWillBeShown = true;
        MainActivity._Instance.runOnUiThread(new Runnable() {
              public void run() {                   
                AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity._Instance);
                alert.setCancelable(false);

                alert.setTitle(title);
               // alert.setMessage(""); // message

                // Set an EditText view to get user input 
                final EditText input = new EditText(MainActivity._Instance);
                final int maxLength = 12;                       

                input.setFilters(new InputFilter[] {new InputFilter() {                     
                    @Override
                    public CharSequence filter(CharSequence source, int start, int end,
                            Spanned dest, int dstart, int dend) {
                        if (source != null && blockCharacterSet.contains(("" + source))) {
                         return "";
                        }                           
                        // TODO Auto-generated method stub
                        return null;
                    }
                } , new InputFilter.LengthFilter(maxLength)});

                //input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_CLASS_NUMBER);
                input.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS); //turn off txt auto complete
                input.setInputType(InputType.TYPE_TEXT_VARIATION_POSTAL_ADDRESS); // type only text,numbers and some special char              

                alert.setView(input);
                //input.setText("Player");

                alert.setPositiveButton(createP, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                  String value = input.getText().toString();
                  // Do something with value!   

                  if (value.isEmpty()){
                      value = "Player";
                  }

                  nativeName(value);                     

                  InputMethodManager imm = (InputMethodManager)getSystemService(
                          Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(input.getWindowToken(), 0);

                }

                });

                alert.setNegativeButton(cancel, new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int whichButton) {
                    // Canceled.
                      InputMethodManager imm = (InputMethodManager)getSystemService(
                              Context.INPUT_METHOD_SERVICE);
                        imm.hideSoftInputFromWindow(input.getWindowToken(), 0);
                  }
                });

                alert.show();                  
              };
        });
    }

And Whenever you need keyboard you just call this function. It works on all Android versions (4.x, 5.x, 6.0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM