当用户点击 EditText 时,如何禁用软键盘显示?
How to disable the soft keyboard to show up when the user clicks on EditText?
我使用 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"/>
编辑:
抱歉,焦点更改似乎过早了,因为此方法是关于隐藏软键盘而不是阻止它显示。
使用 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);
}
}
我找到了一个工作正常且非常简单的解决方案:
只需在inCreateView 方法中使用mEditTextQuestion1.setEnabled(false);
即可使EditText 不可点击。在我的例子中,EditText 的颜色变成了更亮的暗色,所以我将 android:textColor="#000"
添加到我的 xml 文件中。
我有一个非常相似的问题,并且能够按如下方式解决。
在 Android 清单中,您应该将这些行添加到 activity
<activity
android:name=".activities.YourActivity"
android:windowSoftInputMode="adjustResize|stateAlwaysHidden" />
在 Activity Class (Java) 中,将其添加到 onResume
方法中。这将防止在 activity 恢复时弹出键盘
@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);
}
}
我使用 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"/>
编辑: 抱歉,焦点更改似乎过早了,因为此方法是关于隐藏软键盘而不是阻止它显示。
使用 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);
}
}
我找到了一个工作正常且非常简单的解决方案:
只需在inCreateView 方法中使用mEditTextQuestion1.setEnabled(false);
即可使EditText 不可点击。在我的例子中,EditText 的颜色变成了更亮的暗色,所以我将 android:textColor="#000"
添加到我的 xml 文件中。
我有一个非常相似的问题,并且能够按如下方式解决。
在 Android 清单中,您应该将这些行添加到 activity
<activity
android:name=".activities.YourActivity"
android:windowSoftInputMode="adjustResize|stateAlwaysHidden" />
在 Activity Class (Java) 中,将其添加到 onResume
方法中。这将防止在 activity 恢复时弹出键盘
@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);
}
}