radiobutton和edittext无法关闭软键盘

时间:2018-03-03 16:13:07

标签: android

我的布局中有一些RadioButton和一个EditText。 当EditText获得焦点时,将显示软键盘并失去焦点隐藏它。 但是,当EditText失去焦点时,我无法隐藏软键盘 这是布局代码:

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

这是活动代码:

    @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);
}
}

我希望在用户选中etcRadioButtonEditText时显示软键盘,并且用户选中了另一个RadioButton软键盘将被隐藏,EditText的文字将被清除。但现在showSoftInputKeyboard似乎有效,hideSoftInputKeyboard无效。 更新:我想在用户点击etcRadioButton时检查EditText。 这是我的表现:

    <activity
        android:name=".FeedbackActivity"
        android:label="@string/title_activity_feedback"
        android:theme="@style/AppTheme.FeedbackActivity"
        android:windowSoftInputMode="adjustResize" />

2 个答案:

答案 0 :(得分:0)

    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);
        }
    }
}

答案 1 :(得分:-1)

我已经解决了,但并不完美。 如果有人有更好的主意,请告诉我PLZ。 我只是控制EditText可以使用或禁用。 以下是代码:

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);
        }
    });