Having Trouble with Soft Keyboard

你离开我真会死。 提交于 2020-01-02 04:57:08

问题


Hi Everyone iam new to Android and stuck in really silly problem in my project i have one EditText which is defined in Header View of a List View whenever users touches the EditText soft keyboard will be displayed. i have something called clear button which clears the EditText After clearing soft keyboard is not displaying in 2.3 devices.Whenever i press lock/power button lock the device and unlock then soft keyboard is displaying

<activity
        android:name=".pl.Mobile.AddJobNew"
        android:configChanges="orientation|keyboardHidden"
        android:screenOrientation="landscape"
        android:windowSoftInputMode="stateVisible|adjustResize|adjustPan"
         >

Here is My Activity Declaration in Android for android:windowSoftInputMode : 'adjustResize' option working perfectly for Android 2.3 devices but failed in 4.0 devices where soft keyboard overlapping over edit text . option 'adjustPan' is workin good in 4.0 devices but failed in 2.3 devices

Please help me get out of this problem.

Thanks in Advance


回答1:


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




回答2:


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

    }




回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/14357889/having-trouble-with-soft-keyboard

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