Rom*_*rin 5 xml keyboard layout android android-edittext
好吧,花了几个小时寻找解决方案,我来到这里.
我认为这可能是Android的一个问题.
尝试创建这个简单的布局.打开键盘,隐藏它,然后再次打开它,隐藏EditText.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="300dp"
android:gravity="center"
android:inputType="phone"
android:maxLength="14"
android:text="some text"
android:textColor="#666666"
android:textSize="22sp" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
现在,删除android:gravity="center"
,一切正常!在你问我之前,是的,我已经添加了android:windowSoftInputMode="adjustPan"
任何解决方案将不胜感激!谢谢
我最近在 Nexus 7 手机上处理了这个问题。在我们的任何其他测试手机上都没有发生这种情况。诀窍似乎是在关闭软键盘之前改变焦点。键盘在 3 个点处关闭 - 单击完成按钮、单击编辑文本框外部以及单击后退按钮。
首先,创建一个隐藏元素来吸引你的注意力
<MyEditText
android:id="@+id/editHidden"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_below="@id/imageLogin"
/>
Run Code Online (Sandbox Code Playgroud)
我保存了这个,但是有点丑……
@Override
protected void onResume() {
super.onResume();
Utils.focusable = findViewById(R.id.editHidden);
Run Code Online (Sandbox Code Playgroud)
现在,当键盘关闭时,将焦点更改为隐藏元素。
public static void clearFocus() {
try {
if (focusable!=null)
focusable.requestFocus();
} catch (Exception e) {}
}
public static void hideSoftKeyboard(View view) {
clearFocus();
if (view!=null) {
try {
InputMethodManager inputMethodManager = (InputMethodManager) view.getContext().getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
} catch (Exception e) {}
}
}
Run Code Online (Sandbox Code Playgroud)
然后在键盘关闭的地方运行 hideKeyboard 函数:按下 EditText 后退按钮:
@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_BACK)
{
try {
Utils.clearFocus();
} catch (Exception e) {}
}
return super.onKeyPreIme(keyCode, event);
}
Run Code Online (Sandbox Code Playgroud)
按下“完成”按钮,将其附加到出现问题的任何 EditText 框:
public static OnEditorActionListener getOnEditorActionListener() {
return new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId==EditorInfo.IME_ACTION_DONE){
hideSoftKeyboard(v);
}
return false;
}
};
}
Run Code Online (Sandbox Code Playgroud)
单击文本框外部 - 在 onCreate() 中附加到页面上的所有元素;
公共静态无效setupUI(查看视图){
try {
//Set up touch listener for non-text box views to hide keyboard.
if(!(view instanceof EditText)) {
view.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
hideSoftKeyboard(v);
return false;
}
});
}
//If a layout container, iterate over children and seed recursion.
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
View innerView = ((ViewGroup) view).getChildAt(i);
setupUI(innerView);
}
}
} catch (Exception e) {}
Run Code Online (Sandbox Code Playgroud)
}
虽然很乱,但确实解决了问题,希望有更好的方法。
归档时间: |
|
查看次数: |
1206 次 |
最近记录: |