Having Trouble with Soft Keyboard

☆樱花仙子☆ 提交于 2019-12-05 10:37:28

Try this in the activity with the edit text:

int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH){
// Do something for 4.0 and above versions
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
} 
else{
// do something for phones running an SDK before 4.0
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
}

Also remove,

android:windowSoftInputMode="stateVisible|adjustResize|adjustPan"

From your activity in the manifest file.

Build.Version

I think that the problem is that when you click the clear button, the editText looses its focus so the keyboard hides itself.

I would try 2 things to solve it;

  1. after you clean the text, do:

    yourEditTextField.requestFocus()

  2. manually control the keyboard visibility:

    public void showKeyboard(){ View view = getWindow().getCurrentFocus(); if (view==null) return;

    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);
    

    }

    public void hideKeyboard(){ View view = getWindow().getCurrentFocus(); if (view==null) return;

    IBinder binder = view.getWindowToken();
    if (binder == null)
        return;
    
     // prevent a bug in some keyboards that makes the text hang on the screen
    if (view instanceof EditText) 
        ((EditText)view).setText(((EditText)view).getText().toString());
    
    
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(binder, InputMethodManager.HIDE_NOT_ALWAYS);
    

    }

I have below line in My AndroidManifest.xml file and it works great. Please try this code. Dont put any extra code. And still if you not get any success then let me know. I will like to help you out.

<activity android:name=".pl.Mobile.AddJobNew" android:screenOrientation="portrait" android:configChanges="keyboard|orientation" android:windowSoftInputMode="adjustPan"/>

Try with above code for your activity.

Try something like that

    editext1.setOnTouchListener(new OnTouchListener()
            {

                public boolean onTouch(View v, MotionEvent event)
                {
                    if (v instanceof EditText)
                    {
                        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                        v.requestFocusFromTouch();
                        return true;
                    }
                    return false;
                }
            });

If it not work for you try to add

        editext1.setOnFocusChangeListener(new OnFocusChangeListener()
        {

            public void onFocusChange(View v, boolean hasFocus)
            {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
            }
        });

Also is good to remove request focus from layout. and maybe set this android:windowSoftInputMode="stateHidden|stateAlwaysHidden" in manifest on activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!