防止旋转后EditText聚焦 [英] Prevent EditText from focussing after rotation
问题描述
我已经实现了一个简单的EditText,它提供了一个Done按钮来隐藏键盘,并且在旋转到横向时不会为EditText提供全屏对话框.但是有一个问题.当我点击完成"以关闭键盘,然后将设备旋转至横向时,将出现键盘.如果我再次关闭它,然后旋转至纵向,键盘将再次出现.
I've implemented a simple EditText that provides a Done button to hide the keyboard and upon rotating to landscape it does not present a full screen dialog for the EditText. But there's a problem. When I tap Done to dismiss the keyboard then I rotate the device to landscape, the keyboard appears. If I dismiss it again then rotate to portrait the keyboard appears yet again.
如何在旋转时保留键盘可见性状态-如果在旋转前隐藏了键盘可见性状态,则在旋转后不呈现它,但是如果在旋转前可见,则在旋转后呈现它?
How can I preserve the keyboard visibility state upon rotation - if it was hidden before rotation then don't present it after rotation, but if it was visible before rotation then present it after rotation?
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/editText1"
android:inputType="text"
android:imeOptions="flagNoFullscreen|actionDone" />
我尝试在父容器(RelativeLayout)中设置android:descendantFocusability="beforeDescendants"
和android:focusableInTouchMode="true"
,但这并没有影响此行为.
I tried setting android:descendantFocusability="beforeDescendants"
and android:focusableInTouchMode="true"
in the parent container (RelativeLayout), but that didn't affect this behavior.
推荐答案
有两个选项.
在您的onCreate()
方法中,尝试以下选项
There are two options..
In your onCreate()
method try these options
选项1.
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
选项2
edittext.clearFocus();
此选项将焦点设置回活动中的第一个可聚焦视图.
This option sets the focus back to the first focusable view in the activity.
如果您自己的edittext本身是活动中的第一个可聚焦视图,则选项2将不起作用,因为clearFocus
将焦点重新设置为活动中的第一个可聚焦视图.
Option 2 will not work if your edittext it self is the first focusable view in your activity as clearFocus
sets the focus back to first focusable view in your activity.
选项1的使用:
public class MyActivity extends Activity {
EditText editText1;
boolean isKeyBoardOpen=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
editText1 = (EditText) findViewById(R.id.editText1);
if(savedInstanceState!=null &&(savedInstanceState.getBoolean("isKeyBoardOpen",false)))
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
else this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
final View activityRootView = findViewById(R.id.root_layout);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect r = new Rect();
//r will be populated with the coordinates of your view that area still visible.
activityRootView.getWindowVisibleDisplayFrame(r);
int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
isKeyBoardOpen=true;
}else isKeyBoardOpen=false;
}
});
}
protected void onSaveInstanceState(final Bundle bundle) {
super.onSaveInstanceState(bundle);
bundle.putBoolean("isKeyBoardOpen",isKeyBoardOpen);
}
}
这篇关于防止旋转后EditText聚焦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!