简体   繁体   中英

Soft keyboard doesn't hide

My app has one activity with three fragments. There is listview in first (Frg1) and third(Frg3) fragments. Second fragment(Frg2) has one editText. Soft keyboard auto shown when i start Frg2. It's right. I try Frg2.onPause this code

@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.

Code sample(in 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

<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

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

@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

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

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

3. Add

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

Thanks all!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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