android keyboard does not appear
So I've an edittext that I try to get the keyboard for if it's in focus when tilted.
I'm working with a device from motorola, MC40. Android version 2.3.4.
I check if it's in focus, I've debugged and seen that it's in focus. I've tried the following:
txtQuantity.selectAll();
txtQuantity.requestFocus();
Thou while that works in other parts of my program, it does not work in this activity.
The edittext is focused on, but the text is not selected and the keyboard is not there. The edittext is a bit down on the screen, on other activities it's a bit higher up. I believe that's why the keyboard does not show, correct or wrong?
If I force the keyboard with
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
Then the keyboard it shown. However, for some strange reason, my edittext box now shrinks to 1/3d of it's former size and you can't see what's written in it!
This issue is starting to get to me.
Edit :
This event seems to help, as a work around. However, I get a popup asking me to chose between 3 choises, words/all/inmethod. If I choose the middle one, it works, but I need to do that programmatically some how.
edittext.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 0, 0, 0));
The edittextbox
< EditText android:id="@+id/..." android:layout_width="60dp" android:layout_height="wrap_content" android:layout_alignLeft="@+id/..." android:layout_alignRight="@+id/..." android:layout_below="@+id/..." android:layout_marginTop="10dp" android:layout_toRightOf="@+id/..." android:ems="10" android:hint="@string/..." android:imeOptions="actionDone" android:inputType="number" android:maxLength="10" android:nextFocusUp="@+id/..." android:selectAllOnFocus="true" />
You could try using:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
and then in your manifest file you can put the following in the <activity>
tag:
android:windowSoftInputMode="adjustPan"
Here is some code that worked for me. I had a similar issue, where I would requestFocus on my EditText but the keyboard would not show.
public static void showKeyboardForEditText(Context context, EditText editText) {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
Here is the code if you need to close the keyboard:
public static void hideKeyboard(Context context, EditText editText) {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}
链接地址: http://www.djcxy.com/p/16664.html
下一篇: Android键盘不会出现