当用户单击EditText时如何禁用软键盘显示?

时间:2019-10-03 16:02:29

标签: android android-fragments android-softkeyboard show-hide

我使用CardView创建了一个自定义键盘,因此拥有EditText时不需要软件键盘。现在的问题是,当我单击EditText时,虚拟键盘会弹出,是否可以禁用虚拟键盘?

这是我的片段代码:

public class FragmentQuestion1 extends Fragment {

    private EditText mEditTextQuestion1;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_question_1, container, false);

        mEditTextQuestion1 = view.findViewById(R.id.edit_text_question_1);
        MyKeyboard keyboard = view.findViewById(R.id.keyboard);
        mEditTextQuestion1.setRawInputType(InputType.TYPE_CLASS_TEXT);
        mEditTextQuestion1.setTextIsSelectable(true);

        InputConnection ic = mEditTextQuestion1.onCreateInputConnection(new EditorInfo());
        keyboard.setInputConnection(ic);

        mEditTextQuestion1.addTextChangedListener(new NumberTextWatcher(mEditTextQuestion1));

mEditTextQuestion1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                if (hasFocus) {
                    getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
                } else {

                }
            }
        });

        return view;
    }

这是我在xml文件中使用EditText的方式:

<EditText
            android:id="@+id/edit_text_question_1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:backgroundTint="#FFFFFF"
            android:inputType="number"
            android:gravity="center"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:textIsSelectable="true"/>

3 个答案:

答案 0 :(得分:1)

编辑: 抱歉,Focus更改似乎为时过早,因为此方法是隐藏softKeyboard而不阻止其显示。

使用onClickListener效果更好,但您可能会短暂看到键盘。

然后使用InputMethodManager隐藏键盘

请参见Close/hide the Android Soft Keyboard

由于时间问题,即使onClick似乎也不可靠。

但是大多数情况下,一些示例代码仍然有效。


public class MainActivity extends AppCompatActivity {


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

        final EditText message = (EditText) findViewById(R.id.message);

        message.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hideSoftKeyboard(v);
            }
        });

    }

    public void hideSoftKeyboard(View view) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
}

答案 1 :(得分:0)

我找到了一个运行良好且非常简单的解决方案:

只需在inCreateView方法中使用mEditTextQuestion1.setEnabled(false);即可使EditText不可单击。在我的情况下,EditText的颜色变成了较亮的深色,因此我在XML文件中添加了android:textColor="#000"

答案 2 :(得分:0)

我遇到了一个非常类似的问题,并能够按照以下方法解决。

在Android清单中,您应将这些行添加到活动中

   <activity
        android:name=".activities.YourActivity"
        android:windowSoftInputMode="adjustResize|stateAlwaysHidden" />

在活动类(Java)中,将其添加到onResume方法中。恢复活动后,这将防止键盘弹出

@Override
    protected void onResume() {
        super.onResume();
        this.getWindow().setSoftInputMode(WindowManager.
                LayoutParams.SOFT_INPUT_STATE_HIDDEN); //hide keyboard when your activity is in use
    }

然后我添加了此方法来修改EditText,并在onCreate以及每次修改自定义键盘时都调用它

private void setupSpinItemsCustomKeyboard() {
        Helper.hideAllKeyboard(YourActivity.this);
        fromEditText.setShowSoftInputOnFocus(false); //no response on keyboard touch
        fromEditText.requestFocus();
    }

这实际上是隐藏键盘

public static void hideAllKeyboard(Activity activity) {
        View view = activity.getCurrentFocus();
        if (view != null) {
            InputMethodManager imm = (InputMethodManager) activity.getSystemService(
                    Context.INPUT_METHOD_SERVICE);
            assert imm != null;
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }
相关问题