簡體   English   中英

如何在android中點擊edittext時阻止虛擬鍵盤?

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

如何在android中單擊edittext時阻止virtual keyboard

是一個可以滿足您需求的網站。

作為總結,它提供了到InputMethodManager和來自Android開發者的View鏈接。 它將引用View內的getWindowTokenInputMethodManager hideSoftInputFromWindow()

鏈接中給出了更好的答案,希望這會有所幫助。

編輯

從上面發布的鏈接,這是一個使用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
    }
 };

這是同一網站的另一個例子。 這聲稱工作,但似乎是一個壞主意,因為你的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
}
});

希望這能指出你正確的方向

一種更簡單的方法是將編輯文本的可聚焦屬性設置為false。 在EditText的XML集中android:focusable =“false”

另一種更簡單的方法是將android:focusableInTouchMode="false"行添加到EditText的xml中。 希望這可以幫助。

對於光標定位,您可以使用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;
     }
});

執行此操作的最佳方法是將EditText中的標志textIsSelectable設置為true 這將為EditText永久隱藏SoftKeyboard,但也會提供保留光標的額外好處,您將能夠選擇/復制/剪切/粘貼。

您可以在xml布局中設置它,如下所示:

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

或者以編程方式,像這樣:

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

對於使用API​​ 10及更低版本的任何人,此處提供了hack: https//stackoverflow.com/a/20173020/7550472

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM