[英]How to disable the soft keyboard to show up when the user clicks on EditText?
I created a custom keyboard using CardView, so I don't need the software keyboard when I have an EditText.我使用 CardView 创建了一个自定义键盘,所以当我有一个 EditText 时我不需要软件键盘。 The thing now is that when I click on EditText the virtual keyboard pops up, is there a way to disable the virtual keyboard?
现在的问题是,当我单击 EditText 时会弹出虚拟键盘,有没有办法禁用虚拟键盘?
Here is my fragment code:这是我的片段代码:
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;
}
Here is the way I used the EditText in my xml file:这是我在 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"/>
Edit: Sorry it seems on Focus change is to early as this method is about hiding a softKeyboard not preventing it from showing.编辑:抱歉,焦点更改似乎很早,因为此方法是关于隐藏软键盘而不阻止它显示。
Use an onClickListener works better but you will probably see the keyboard briefly.使用 onClickListener 效果更好,但您可能会短暂看到键盘。
And then hide the keyboard with InputMethodManager然后用 InputMethodManager 隐藏键盘
See Close/hide the Android Soft Keyboard请参阅关闭/隐藏 Android 软键盘
Even onClick Seem to be unreliable due to timing issues.甚至 onClick 由于时序问题,似乎也不可靠。
But some example code that works most of the time.但是一些示例代码在大多数情况下都有效。
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);
}
}
I found a solution that works fine and is pretty easy:我找到了一个工作正常且非常简单的解决方案:
Just use mEditTextQuestion1.setEnabled(false);
只需使用
mEditTextQuestion1.setEnabled(false);
in the inCreateView method to make the EditText not clickable.在 inCreateView 方法中使 EditText 不可点击。 In my case then the color of the EditText changed to a brighter dark so I added
android:textColor="#000"
to my xml file.在我的情况下,EditText 的颜色变为更亮的暗色,因此我将
android:textColor="#000"
添加到我的 xml 文件中。
I had a very similar issue and was able to solve it as follows.我有一个非常相似的问题,并且能够按如下方式解决它。
In the Android Manifest, you should add these lines to the activity在 Android 清单中,您应该将这些行添加到活动中
<activity
android:name=".activities.YourActivity"
android:windowSoftInputMode="adjustResize|stateAlwaysHidden" />
Within the Activity Class (Java), add this to the onResume
method.在活动 Class (Java) 中,将其添加到
onResume
方法中。 This will prevent the keyboard from popping up when the activity is resumed这将防止在活动恢复时弹出键盘
@Override
protected void onResume() {
super.onResume();
this.getWindow().setSoftInputMode(WindowManager.
LayoutParams.SOFT_INPUT_STATE_HIDDEN); //hide keyboard when your activity is in use
}
And I added this method to modify the EditText
, and called it from onCreate, and whenever I modified my custom keyboard我添加了这个方法来修改
EditText
,并从 onCreate 调用它,每当我修改我的自定义键盘时
private void setupSpinItemsCustomKeyboard() {
Helper.hideAllKeyboard(YourActivity.this);
fromEditText.setShowSoftInputOnFocus(false); //no response on keyboard touch
fromEditText.requestFocus();
}
and this to actually hide the keyboard这实际上隐藏了键盘
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.