简体   繁体   中英

radiobutton and edittext can't close soft keyboard

I have some RadioButton and an EditText in my layout. When EditText get focus will show soft keyboard and lose focus hide it. But now I can't hide soft keyboard when EditText lose focus.
Here is layout code:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">
        <android.support.constraint.ConstraintLayout
            android:id="@+id/constraintLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="8dp">
            <RadioGroup
                android:id="@+id/feedback_radiogroup"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">
                <android.support.v7.widget.AppCompatRadioButton
                    android:id="@+id/feedback_radiobutton_0"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="0" />
                <android.support.v7.widget.AppCompatRadioButton
                    android:id="@+id/feedback_radiobutton_1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="1"/>
                <android.support.v7.widget.AppCompatRadioButton
                    android:id="@+id/feedback_radiobutton_2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="2" />
                <android.support.v7.widget.AppCompatRadioButton
                    android:id="@+id/feedback_etc_radiobutton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="etc"
                    />
            </RadioGroup>
            <android.support.v7.widget.AppCompatEditText
                android:id="@+id/feedback_edittext"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:focusable="false"
                android:imeOptions="actionSend"
                android:inputType="text"
                app:layout_constraintStart_toEndOf="parent"
             app:layout_constraintTop_toBottomOf="@id/feedback_radiogroup"/>
        </android.support.constraint.ConstraintLayout>
    </android.support.design.widget.CoordinatorLayout>

And this is code for activity:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_feedback);

    mRadioGroup = (RadioGroup) findViewById(R.id.feedback_radiogroup);
    mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
    mEditText.setFocusable(checkedId == R.id.feedback_etc_radiobutton);
    mEditText.setFocusableInTouchMode(checkedId == R.id.feedback_etc_radiobutton);
if (checkedId != R.id.feedback_etc_radiobutton) {
    mEditText.setText("");
} else {
    mEditText.performClick();
}
}
});
    mRadioButton0 = (AppCompatRadioButton) findViewById(R.id.feedback_radiobutton_0);
    mRadioButton1 = (AppCompatRadioButton) findViewById(R.id.feedback_radiobutton_1);
    mRadioButton2 = (AppCompatRadioButton) findViewById(R.id.feedback_radiobutton_2);
    mEtcRadioButton = (AppCompatRadioButton) findViewById(R.id.feedback_etc_radiobutton);
mEditText = (AppCompatEditText) findViewById(R.id.feedback_edittext);
mEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
    mRadioGroup.check(mEtcRadioButton.getId());
    showSoftInputKeyboard();
} else {
    hideSoftInputKeyboard();
}
}
});
mEditText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
    mEditText.setFocusable(true);
    mEditText.setFocusableInTouchMode(true);
    mEditText.requestFocus();
    }
});
mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    if (actionId == EditorInfo.IME_ACTION_SEND) {
        //do something
        return true;
    }
        return false;
    }
});
}
void showSoftInputKeyboard() {
    ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE))
.showSoftInput(getCurrentFocus(), InputMethodManager.SHOW_FORCED);
}
void hideSoftInputKeyboard() {
    if (getCurrentFocus() != null) {
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}

I want show soft keyboard when user check etcRadioButton or EditText ,and user checked another RadioButton the soft keyboard will be hidden and text of EditText will be clear.But now showSoftInputKeyboard seems work and hideSoftInputKeyboard not work. UPDATE: I want etcRadioButton was checked when user click on EditText . Here is my manifast :

    <activity
        android:name=".FeedbackActivity"
        android:label="@string/title_activity_feedback"
        android:theme="@style/AppTheme.FeedbackActivity"
        android:windowSoftInputMode="adjustResize" />
    call this method from your oncreate() and pass your view and 
    activity




     public  void hideKeyboardOnOutSideTouch(View view, final 
     Activity activity) {
    if (!(view instanceof EditText)) {
        view.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                hideKeyboard(activity);
                return false;
            }
        });
    }
    if (view instanceof ViewGroup) {
        for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
            View innerView = ((ViewGroup) view).getChildAt(i);
            hideKeyboardOnOutSideTouch(innerView, activity);
        }
    }
}

I don't think you need to add mRadioGroup.check(mEtcRadioButton.getId()); as the button that's clicked, gets also checked without this line of code.

Also, I have encountered before onFocusChange(View v, boolean hasFocus) getting called twice, once for receiving focus and other time for losing focus. Instead of using the boolean hasFocus , can you use mRadioButton.isChecked() in the below code like:

public void onFocusChange(View v, boolean hasFocus) {
if ((RadioButton)v).isChecked()) {
    showSoftInputKeyboard();
} else {
    hideSoftInputKeyboard();
}

I've solved it,but not perfect. If anyone has a better idea,told me plz. I just control EditText endable or disable. Following is the code :

mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            mEditText.setEnabled(checkedId == R.id.feedback_etc_radiobutton);
            mEditText.setFocusable(checkedId == R.id.feedback_etc_radiobutton);
            mEditText.setFocusableInTouchMode(checkedId == R.id.feedback_etc_radiobutton);
            if (checkedId != R.id.feedback_etc_radiobutton) {
                mEditText.setText("");
            } else {
                mEditText.requestFocus();
            }
            mEditText.setEnabled(true);
        }
    });

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