【问题标题】:Prevent EditText from focussing after rotation防止 EditText 在旋转后聚焦
【发布时间】:2015-01-07 15:31:56
【问题描述】:

我已经实现了一个简单的 EditText,它提供了一个完成按钮来隐藏键盘,并且在旋转到横向时它不会为 EditText 显示全屏对话框。但是有一个问题。当我点击完成关闭键盘然后我将设备旋转到横向时,键盘会出现。如果我再次关闭它然后旋转到纵向键盘再次出现。

如何在旋转时保持键盘可见性状态 - 如果它在旋转之前被隐藏,则在旋转后不显示它,但如果它在旋转之前可见,则在旋转后显示它?

<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",但这并不影响此行为。

【问题讨论】:

  • 您是否在代码中实现了 onConfigurationChange,那么我可以给您一个答案。因为如果您现在实现它,您还有其他事情需要处理。
  • @piyush 我还没有在onConfigurationChange做任何事情。
  • 试过我给出的答案

标签: android rotation keyboard android-edittext


【解决方案1】:

有两种选择.. 在您的 onCreate() 方法中尝试这些选项

选项 1。

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

选项 2

edittext.clearFocus();

此选项将焦点设置回活动中的第一个可聚焦视图。

编辑:

如果您的 edittext 本身是您的活动中的第一个可聚焦视图,则选项 2 将不起作用,因为 clearFocus 将焦点设置回您的活动中的第一个可聚焦视图。

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

}
}

【讨论】:

  • 第二个选项不起作用。第一个选项可以很好地防止键盘在之前不在屏幕上的情况下在旋转后出现,但是如果我调出键盘,那么旋转键盘就会被取消。理想情况下,它会在旋转之前出现在屏幕上。
  • @Joey 编辑了我的答案
【解决方案2】:

有很多方法可以做,但我给你2个

1 在配置更改时覆盖其中的 onConfigurationChange 方法,查看键盘是否打开,您可以通过将其值保存为布尔值并在 onConfigurationChange 更改后使用该布尔值来采取行动显示或隐藏键盘的值。

2 如果您还没有实现 onConfigurationChange 方法,另一种方法是在 onSaveInstanceState 中保存状态并在 onRestoreInstanceState 中检索它,如下所示。

 @Override
 protected void onSaveInstanceState(Bundle outState) {
  // TODO Auto-generated method stub
  super.onSaveInstanceState(outState);

  //get the status of keyboard 
  boolean status = isKeyboardVisible(){this is your method or your logic};
  outState.putBoolean(key, status)

 }
@Override
 protected void onRestoreInstanceState(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onRestoreInstanceState(savedInstanceState);

  boolean status = savedInstanceState.getBoolean(key);

    //based on status show or hide keyboard.
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-24
    • 2012-06-01
    • 2020-10-23
    • 1970-01-01
    相关资源
    最近更新 更多