简体   繁体   English

软键盘不会隐藏

[英]Soft keyboard doesn't hide

My app has one activity with three fragments.我的应用程序有一个包含三个片段的活动。 There is listview in first (Frg1) and third(Frg3) fragments.第一个 (Frg1) 和第三个 (Frg3) 片段中有列表视图。 Second fragment(Frg2) has one editText.第二个片段(Frg2) 有一个editText。 Soft keyboard auto shown when i start Frg2.启动 Frg2 时自动显示软键盘。 It's right.这是正确的。 I try Frg2.onPause this code我尝试 Frg2.onPause 这段代码

@Override
public void onPause() {
    super.onPause();

    editText.post(new Runnable() {
        @Override
        public void run() {
            editText.clearFocus();
            imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
        }
    });
}

When i start Frg1 or Frg2 after Frg3 soft keyboard hides but after render Frg1/Frg2 keyboard shows again.当我在 Frg3 软键盘隐藏后启动 Frg1 或 Frg2 但渲染后 Frg1/Frg2 键盘再次显示时。

Code sample(in Frg3):代码示例(在 Frg3 中):

@Override
public void onResume() {
    super.onResume();
    editText.requestFocus();
}

and

        editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            editText.post(new Runnable() {
                @Override
                public void run() {
                    imm.showSoftInput(editText, 0);
                }
            });
        }
    });

In Manifest.xml在 Manifest.xml 中

<activity android:name=".ContentActivity"
          android:configChanges="keyboardHidden|orientation|screenSize"
          android:screenOrientation="portrait"
          android:windowSoftInputMode="adjustResize" >

What could be the problem?可能是什么问题呢?

UPDATE更新

I found the solution我找到了解决方案

1. Delete 1. 删除

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            editText.post(new Runnable() {
                @Override
                public void run() {
                    imm.showSoftInput(editText, 0);
                }
            });
        }
    });

2. Add 2. 添加

@Override
public void onResume() {
    super.onResume();
    editText.requestFocus();
    imm.showSoftInput(editText, 0);
}

and

@Override
public void onPause() {
    super.onPause();
    View v = getActivity().getCurrentFocus();
    if(v != null) {
        v.clearFocus();
        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
    }
    editText.clearFocus();
}

Thanks all!谢谢大家!

I found the solution我找到了解决方案

1. Delete 1. 删除

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        editText.post(new Runnable() {
            @Override
            public void run() {
                imm.showSoftInput(editText, 0);
            }
        });
    }
});

2. Add 2. 添加

 @Override
 public void onResume() {
    super.onResume();
    editText.requestFocus();
    imm.showSoftInput(editText, 0);
 }

3. Add 3. 添加

@Override
public void onPause() {
    super.onPause();
    View v = getActivity().getCurrentFocus();
    if(v != null) {
        v.clearFocus();
        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
    }
    editText.clearFocus();
}

Thanks all!谢谢大家!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM