我使用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"/>
当您使用焦点侦听器将EditText对准焦点时,您可以隐藏软键盘
请参见Event for Handling the Focus of the EditText
然后使用InputMethodManager隐藏键盘