在android中单击edittext时如何阻止虚拟键盘?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1845285/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:42:08  来源:igfitidea点击:

how to block virtual keyboard while clicking on edittext in android?

android

提问by deepthi

How to block virtual keyboardwhile clicking on edittext in android

virtual keyboard在android中单击edittext时如何阻止

回答by Anthony Forloney

Hereis a website that will give you what you need.

是一个可以为您提供所需内容的网站。

As a summary, it provides links to InputMethodManagerand Viewfrom Android Developers. It will reference to the getWindowTokeninside of Viewand hideSoftInputFromWindow()for InputMethodManager

总而言之,它提供了InputMethodManagerViewAndroid 开发人员之间的链接。它将引用getWindowToken内部ViewhideSoftInputFromWindow()InputMethodManager

A better answer is given in the link, hope this helps.

链接中给出了更好的答案,希望这会有所帮助。

EDIT

编辑

From the link posted above, 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 EditBoxis NULLit will be no longer an editor:

这是来自同一网站的另一个示例。这要求工作,但似乎是一个糟糕的主意,因为你EditBoxNULL这将是不再编辑:

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 event
}
});

Hope this points you in the right direction

希望这为您指明了正确的方向

回答by Symfony Dev

A simpler way, is to set focusable property of edit text false. In your EditText's XML set android:focusable="false"

一种更简单的方法是将编辑文本的可聚焦属性设置为 false。在你的 EditText 的 XML 中设置 android:focusable="false"

回答by ACengiz

Another simpler way is adding android:focusableInTouchMode="false"line to your EditText's xml. Hope this helps.

另一种更简单的方法是android:focusableInTouchMode="false"在您EditText的 xml 中添加一行。希望这可以帮助。

回答by Kaushik NP

The best way to do this is by setting the flag textIsSelectablein EditText to true. This will hide the SoftKeyboard permanently for the EditText but also will provide the added bonus of retaining the cursor and you'll be able to select/copy/cut/paste.

最好的方法是将textIsSelectableEditText 中的标志设置为true。这将为 EditText 永久隐藏 SoftKeyboard,但还将提供保留光标的额外好处,您将能够选择/复制/剪切/粘贴。

You can set it in your xml layout like this:

您可以像这样在 xml 布局中设置它:

<EditText
    android:textIsSelectable="true"
    ...
/>

Or programmatically, like this:

或者以编程方式,像这样:

EditText editText = (EditText) findViewById(R.id.editText);
editText.setTextIsSelectable(true);

For anyone using API 10 and below, hack is provided here : https://stackoverflow.com/a/20173020/7550472

对于使用 API 10 及以下版本的任何人,此处提供了 hack:https: //stackoverflow.com/a/20173020/7550472

回答by GeekYouUp

For cursor positioning you can use Selection.setSelection(...), i just tried this and it worked:

对于您可以使用的光标定位Selection.setSelection(...),我刚刚尝试了这个并且它有效:

final EditText editText = (EditText) findViewById(R.id.edittext);
editText.setOnTouchListener(new View.OnTouchListener() {
     @Override
     public boolean onTouch(View view, MotionEvent motionEvent) {

         //change the text here

         Selection.setSelection(editText.getText(), editText.length());
         return true;
     }
});