簡體   English   中英

Android:EditText會自動打開鍵盤,如何停止?

[英]Android: EditText turns on keyboard automatically, how to stop?

我在下面定義的ListView有一個簡單的EditText

應用程序運行時,會選擇EditText並顯示鍵盤。

我不希望這種情況發生。 我不希望它被選中或默認情況下出現鍵盤。

<EditText
    android:id="@+id/search_box"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="type to search titles"
    android:inputType="text"
    android:maxLines="1"
    android:capitalize="none"
    android:linksClickable="false"
    android:autoLink="none"
    android:autoText="true"
    android:singleLine="true" />
<ListView
    android:id="@+id/DetailsListView"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:background="@color/transparent"
    android:cacheColorHint="@color/transparent"
    android:listSelector="@drawable/list_selector"
    android:fastScrollEnabled="true" />

首先標簽是monodroid所以它在C#上,所以接受的答案是正確的。

但這不是我認為作者想要的......這個技巧只是隱藏了鍵盤,但editText仍然有焦點。

要在java和c#中執行此操作,必須將其放在根布局中:

android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"

例如 :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/white"
    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true"
              >
....
</LinearLayout>

我發現這是解決方案:

Window.SetSoftInputMode (SoftInput.StateAlwaysHidden);

注意:這是用於Android的C#而不是Java

在onCreate中添加它

    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

在布局中嘗試這個

<EditText
                    android:id="@+id/myid2"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/myid1"
                    android:clickable="true"
                    android:cursorVisible="false"
                    android:focusable="false"
                    android:hint="@string/hint"
                    android:singleLine="true"
                    android:textColor="@color/black"
                    android:textSize="15dp" />

這在你的代碼中

    myEditText.setOnTouchListener(new OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_UP) {
                //do tasks
            }
            return false;
        }
    });

我不知道它是否有用。 我甚至不知道它做了什么。 但它為我解決了類似的問題。 如果我浪費你的時間,我會

我找到了我的解決方案,你可以嘗試這個,它完美無缺。

xml(我在defualt中將editText的focusableInTouchMode設置為false):

       <EditText ... android:focusableInTouchMode="false" .../>

我在觸摸java后改變了focusableInTouchMode:

        yourEditText.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            yourEditText.setFocusableInTouchMode(true);
            return false;
        }
    });

android:windowSoftInputMode =“stateHidden”對我不起作用。 也沒有弄亂元素焦點和其他技術(甚至將其設置為enabled = false顯示鍵盤)。

我的解決方案:創建一個hideKeyboard函數:

private void hideKeyboard() {
        InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

每當托管文本字段的UI變得可見時觸發它:

對話:

 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            hideKeyboard();
        }

對於小部件:

@Override
public void setVisibility(int visibility) {
    super.setVisibility(visibility);
    hideKeyBoard();
}

暫無
暫無

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

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