Android 在 EditText 上禁用键盘
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10636635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disable keyboard on EditText
提问by Ferox
I'm doing a calculator.
So I made my own Buttons
with numbers and functions.
The expression that has to be calculated, is in an EditText
, because I want users can add numbers or functions also in the middle of the expression, so with the EditText
I have the cursor
. But I want to disable the Keyboard
when users click on the EditText
.
I found this example that it's ok for Android 2.3
, but with ICS
disable the Keyboard
and also the cursor.
我在做计算器。所以我Buttons
用数字和函数制作了我自己的。必须计算EditText
的表达式EditText
在cursor
. 但是我想Keyboard
在用户单击EditText
. 我发现这个例子可以用于Android 2.3
,但ICS
禁用Keyboard
和光标。
public class NoImeEditText extends EditText {
public NoImeEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onCheckIsTextEditor() {
return false;
}
}
And then I use this NoImeEditText
in my XML
file
然后我NoImeEditText
在我的XML
文件中使用它
<com.my.package.NoImeEditText
android:id="@+id/etMy"
....
/>
How I can make compatible this EditText with ICS??? Thanks.
我如何使这个 EditText 与 ICS 兼容???谢谢。
采纳答案by Hip Hip Array
Hereis a website that will give you what you need
这是一个可以为您提供所需内容的网站
As a summary, it provides links to InputMethodManager
and View
from Android Developers. It will reference to the getWindowToken
inside of View
and hideSoftInputFromWindow()
for InputMethodManager
总而言之,它提供了InputMethodManager
与View
Android 开发人员之间的链接。它将引用getWindowToken
内部View
和hideSoftInputFromWindow()
为InputMethodManager
A better answer is given in the link, hope this helps.
链接中给出了更好的答案,希望这会有所帮助。
here is an example to consume the onTouch event:
这是使用 onTouch 事件的示例:
editText_input_field.setOnTouchListener(otl);
private OnTouchListener otl = new OnTouchListener() {
public boolean onTouch (View v, MotionEvent event) {
return true; // the listener has consumed the event
}
};
Here is another example from the same website. This claims to work but seems like a bad idea since your EditBox is NULL it will be no longer an editor:
这是来自同一网站的另一个示例。这声称有效,但似乎是一个坏主意,因为您的 EditBox 为 NULL,它将不再是编辑器:
MyEditor.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
int inType = MyEditor.getInputType(); // backup the input type
MyEditor.setInputType(InputType.TYPE_NULL); // disable soft input
MyEditor.onTouchEvent(event); // call native handler
MyEditor.setInputType(inType); // restore input type
return true; // consume touch even
}
});
Hope this points you in the right direction
希望这为您指明了正确的方向
回答by Oleksii Malovanyi
Below code is both for API >= 11 and API < 11. Cursor is still available.
下面的代码适用于 API >= 11 和 API < 11。游标仍然可用。
/**
* Disable soft keyboard from appearing, use in conjunction with android:windowSoftInputMode="stateAlwaysHidden|adjustNothing"
* @param editText
*/
public static void disableSoftInputFromAppearing(EditText editText) {
if (Build.VERSION.SDK_INT >= 11) {
editText.setRawInputType(InputType.TYPE_CLASS_TEXT);
editText.setTextIsSelectable(true);
} else {
editText.setRawInputType(InputType.TYPE_NULL);
editText.setFocusable(true);
}
}
回答by kuelye
You can also use setShowSoftInputOnFocus(boolean)directly on API 21+ or through reflection on API 14+:
您还可以直接在 API 21+ 上或通过对 API 14+ 的反射使用setShowSoftInputOnFocus(boolean):
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
editText.setShowSoftInputOnFocus(false);
} else {
try {
final Method method = EditText.class.getMethod(
"setShowSoftInputOnFocus"
, new Class[]{boolean.class});
method.setAccessible(true);
method.invoke(editText, false);
} catch (Exception e) {
// ignore
}
}
回答by K_Anas
try: android:editable="false"
or android:inputType="none"
尝试:android:editable="false"
或android:inputType="none"
回答by Nishara MJ
Add below properties to the Edittext controller in the layout file
将以下属性添加到布局文件中的 Edittext 控制器
<Edittext
android:focusableInTouchMode="true"
android:cursorVisible="false"
android:focusable="false" />
I have been using this solution for while and it works fine for me.
我一直在使用这个解决方案,它对我来说很好用。
回答by Suragch
Disable the keyboard (API 11 to current)
禁用键盘(API 11 到当前)
This is the best answer I have found so far to disable the keyboard (and I have seen a lot of them).
这是迄今为止我找到的禁用键盘的最佳答案(我见过很多)。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // API 21
editText.setShowSoftInputOnFocus(false);
} else { // API 11-20
editText.setTextIsSelectable(true);
}
There is no need to use reflection or set the InputType
to null.
无需使用反射或将 设置InputType
为 null。
Re-enable the keyboard
重新启用键盘
Here is how you re-enable the keyboard if needed.
如果需要,您可以通过以下方法重新启用键盘。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // API 21
editText.setShowSoftInputOnFocus(true);
} else { // API 11-20
editText.setTextIsSelectable(false);
editText.setFocusable(true);
editText.setFocusableInTouchMode(true);
editText.setClickable(true);
editText.setLongClickable(true);
editText.setMovementMethod(ArrowKeyMovementMethod.getInstance());
editText.setText(editText.getText(), TextView.BufferType.SPANNABLE);
}
See this Q&A for why the complicated pre API 21 version is needed to undo setTextIsSelectable(true)
:
请参阅此问答,了解为什么需要复杂的 pre API 21 版本来撤消setTextIsSelectable(true)
:
This answer needs to be more thoroughly tested.
这个答案需要更彻底的测试。
I have tested the setShowSoftInputOnFocus
on higher API devices, but after @androiddeveloper's comment below, I see that this needs to be more thoroughly tested.
我已经setShowSoftInputOnFocus
在更高的 API 设备上进行了测试,但是在下面@androiddeveloper 的评论之后,我发现这需要进行更彻底的测试。
Here is some cut-and-paste code to help test this answer. If you can confirm that it does or doesn't work for API 11 to 20, please leave a comment. I don't have any API 11-20 devices and my emulator is having problems.
这里有一些剪切和粘贴代码来帮助测试这个答案。如果您可以确认它对 API 11 到 20 是否有效,请发表评论。我没有任何 API 11-20 设备,而且我的模拟器有问题。
activity_main.xml
活动_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
android:background="@android:color/white">
<EditText
android:id="@+id/editText"
android:textColor="@android:color/black"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:text="enable keyboard"
android:onClick="enableButtonClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="disable keyboard"
android:onClick="disableButtonClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
MainActivity.java
主活动.java
public class MainActivity extends AppCompatActivity {
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
}
// when keyboard is hidden it should appear when editText is clicked
public void enableButtonClick(View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // API 21
editText.setShowSoftInputOnFocus(true);
} else { // API 11-20
editText.setTextIsSelectable(false);
editText.setFocusable(true);
editText.setFocusableInTouchMode(true);
editText.setClickable(true);
editText.setLongClickable(true);
editText.setMovementMethod(ArrowKeyMovementMethod.getInstance());
editText.setText(editText.getText(), TextView.BufferType.SPANNABLE);
}
}
// when keyboard is hidden it shouldn't respond when editText is clicked
public void disableButtonClick(View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // API 21
editText.setShowSoftInputOnFocus(false);
} else { // API 11-20
editText.setTextIsSelectable(true);
}
}
}
回答by android developer
Gathering solutions from multiple places here on StackOverflow, I think the next one sums it up:
在 StackOverflow 上从多个地方收集解决方案,我想下一个总结一下:
If you don't need the keyboard to be shown anywhere on your activity, you can simply use the next flags which are used for dialogs (got from here) :
如果您不需要在活动的任何位置显示键盘,您只需使用用于对话框的下一个标志(从这里获得):
getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM, WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
If you don't want it only for a specific EditText, you can use this (got from here) :
如果您不希望它仅用于特定的 EditText,您可以使用它(从这里获得):
public static boolean disableKeyboardForEditText(@NonNull EditText editText) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
editText.setShowSoftInputOnFocus(false);
return true;
}
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
try {
final Method method = EditText.class.getMethod("setShowSoftInputOnFocus", new Class[]{boolean.class});
method.setAccessible(true);
method.invoke(editText, false);
return true;
} catch (Exception ignored) {
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2)
try {
Method method = TextView.class.getMethod("setSoftInputShownOnFocus", boolean.class);
method.setAccessible(true);
method.invoke(editText, false);
return true;
} catch (Exception ignored) {
}
return false;
}
Or this (taken from here) :
或者这个(取自这里):
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
editText.setShowSoftInputOnFocus(false);
else
editText.setTextIsSelectable(true);
回答by Vijay
I found this solution which works for me. It also places the cursor, when clicked on EditText at the correct position.
我找到了这个对我有用的解决方案。当单击 EditText 时,它还会将光标放置在正确的位置。
EditText editText = (EditText)findViewById(R.id.edit_mine);
// set OnTouchListener to consume the touch event
editText.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
v.onTouchEvent(event); // handle the event first
InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(v.getWindowToken(), 0); // hide the soft keyboard
}
return true;
}
});
回答by Kanagalingam
editText.setShowSoftInputOnFocus(false);
回答by Abel Terefe
// only if you completely want to disable keyboard for
// that particular edit text
your_edit_text = (EditText) findViewById(R.id.editText_1);
your_edit_text.setInputType(InputType.TYPE_NULL);