有软键盘的麻烦

时间:2013-01-16 11:55:59

标签: android android-widget android-manifest android-softkeyboard

大家好我刚接触Android并在我的项目中陷入了非常愚蠢的问题我有一个EditText,它会在用户触摸EditText软键盘时在列表视图的页眉视图中定义。我有一个叫清除按钮的东西清除EditText清除软键盘后没有在2.3设备中显示。每当我按下锁定/电源按钮锁定设备并解锁然后软键盘显示

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

这是Android中的Android活动声明:windowSoftInputMode:&#39; adjustResize&#39;选项适用于Android 2.3设备但在4.0设备中失败,其中软键盘重叠在编辑文本上。选项&#39; adjustPan&#39;在4.0设备上工作得很好但在2.3设备中失败

请帮助我摆脱这个问题。

先谢谢

4 个答案:

答案 0 :(得分:5)

使用编辑文本在活动中尝试此操作:

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

同时删除,

android:windowSoftInputMode="stateVisible|adjustResize|adjustPan"

来自清单文件中的活动。

Build.Version

答案 1 :(得分:2)

我认为问题在于,当您单击清除按钮时,editText会失去焦点,因此键盘会隐藏自己。

我会尝试两件事来解决它;

  1. 清理文本后,执行:

    yourEditTextField.requestFocus()

  2. 手动控制键盘可见性:

    public void showKeyboard(){     View view = getWindow()。getCurrentFocus();     if(view == null)         返回;

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

    }

    public void hideKeyboard(){     View view = getWindow()。getCurrentFocus();     if(view == null)         返回;

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

    }

答案 2 :(得分:1)

我的AndroidManifest.xml文件中有以下行,效果很好。请试试这段代码。不要添加任何额外的代码。如果你没有取得任何成功,请告诉我。我想帮助你。

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

尝试使用上述代码进行活动。

答案 3 :(得分:1)

尝试类似的东西

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

如果它不起作用,请尝试添加

        editext1.setOnFocusChangeListener(new OnFocusChangeListener()
        {

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

也可以从布局中删除请求焦点。并且可能在活动清单中设置 android:windowSoftInputMode =“stateHidden | stateAlwaysHidden”

相关问题