簡體   English   中英

如何在EditText上隱藏Android軟鍵盤

[英]How to hide Android soft keyboard on EditText

我有一個帶有一些EditText字段和一些按鈕的Activity,以方便通常用於填充這些字段的內容。 但是,當我們用戶觸摸其中一個EditText字段時,會自動顯示Android軟鍵盤。 我希望它默認保持隱藏狀態, 除非用戶長按菜單按鈕。 我已經找到了解決方案,並找到了幾個答案,但到目前為止我無法讓它們工作。

我嘗試過以下方法:

1 - 在onCreate方法中,

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

2 - 同樣在onCreate方法中,

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);

3 - 和fIn清單文件,

<activity android:name=".activityName" android:windowSoftInputMode="stateAlwaysHidden"/>

這些方法都不起作用。 只要用戶單擊EditText字段,就會出現軟鍵盤。 如果用戶通過長按菜單鍵明確顯示軟鍵盤,我只想顯示軟鍵盤。

為什么這不起作用?

這會對你有所幫助

editText.setInputType(InputType.TYPE_NULL);

編輯:

要顯示軟鍵盤,您必須在long key press event of menu button編寫以下代碼

editText.setInputType(InputType.TYPE_CLASS_TEXT);
            editText.requestFocus();
            InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            mgr.showSoftInput(editText, InputMethodManager.SHOW_FORCED);

您需要在AndroidManifest.xmlActivity添加以下屬性。

<activity
    ...
    android:windowSoftInputMode="stateHidden|adjustResize"
    ...
/>

我有時會使用一些技巧來做到這一點。 我在布局的頂部放置了一個隱形焦點支架。 就像這樣

 <EditText android:id="@id/editInvisibleFocusHolder"
          style="@style/InvisibleFocusHolder"/>

有這種風格

<style name="InvisibleFocusHolder">
    <item name="android:layout_width">0dp</item>
    <item name="android:layout_height">0dp</item>
    <item name="android:focusable">true</item>
    <item name="android:focusableInTouchMode">true</item>
    <item name="android:inputType">none</item>
</style>

然后在onResume我會打電話

    editInvisibleFocusHolder.setInputType(InputType.TYPE_NULL);
    editInvisibleFocusHolder.requestFocus();

這對我來說很有效,從1.6到4.x

經過長時間查看TextView類,我發現了一種防止鍵盤出現的方法。 訣竅是它出現后立即隱藏它,所以我搜索了鍵盤出現后調用的方法並隱藏它。

實現了EditText類

public class NoImeEditText extends EditText {

    public NoImeEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * This method is called before keyboard appears when text is selected.
     * So just hide the keyboard
     * @return
     */
    @Override
    public boolean onCheckIsTextEditor() {
        hideKeyboard();

        return super.onCheckIsTextEditor();
    }

    /**
     * This methdod is called when text selection is changed, so hide keyboard to prevent it to appear
     * @param selStart
     * @param selEnd
     */
    @Override
    protected void onSelectionChanged(int selStart, int selEnd) {
        super.onSelectionChanged(selStart, selEnd);

        hideKeyboard();
    }

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

和風格

<com.my.app.CustomViews.NoImeEditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:editable="false"
    android:background="@null"
    android:textSize="@dimen/cell_text" />

即使我將EditorInfo.TYPE_NULL設置為視圖,軟鍵盤仍然保持上升。 除了我從nik431的答案得到的想法之外,沒有一個答案對我有用:

editText.setCursorVisible(false);
editText.setFocusableInTouchMode(false);
editText.setFocusable(false);

我的測試結果:

使用setInputType

editText.setInputType(InputType.TYPE_NULL);

軟鍵盤消失,但光標也會消失。

使用setShowSoftInputOnFocus

editText.setShowSoftInputOnFocus(false)

它按預期工作。

以下行正是正在尋找的內容。 該方法已包含在API 21 ,因此適用於API 21及更高版本。

edittext.setShowSoftInputOnFocus(false);

似乎有很多種方法可以阻止系統鍵盤出現,無論是以編程方式還是以xml方式顯示。 但是,這是支持API 11之前設備的方式。

// prevent system keyboard from appearing
if (android.os.Build.VERSION.SDK_INT >= 11) {
    editText.setRawInputType(InputType.TYPE_CLASS_TEXT);
    editText.setTextIsSelectable(true);
} else {
    editText.setRawInputType(InputType.TYPE_NULL);
    editText.setFocusable(true);
}

讓我們嘗試在您的xml中為EditText設置以下屬性

android:focusableInTouchMode="true" android:cursorVisible="false".

如果您想在啟動活動時隱藏軟鍵盤,請瀏覽此鏈接

weekText = (EditText) layout.findViewById(R.id.weekEditText);
weekText.setInputType(InputType.TYPE_NULL);

基於相同簡單指令的三種方式:

一個)。 結果像locate(1)一樣簡單:

android:focusableInTouchMode="true"

在布局中任何先前元素的配置中,例如:

如果您的整個布局由以下內容組成:

<ImageView>

<EditTextView>

<EditTextView>

<EditTextView>

然后你可以在ImageView參數中編寫(1),這將抓住android的注意力而不是EditText。

B)。 如果您有另一個先前元素而不是ImageView,您可能需要將(2)添加到(1):

android:focusable="true"

C)。 您還可以在視圖元素的頂部創建一個空元素:

<LinearLayout
  android:focusable="true"
  android:focusableInTouchMode="true"
  android:layout_width="0px"
  android:layout_height="0px" />

這個替代方案直到這一點成為我所見過的最簡單的結果。 希望能幫助到你...

隱藏鍵盤

editText.setInputType(InputType.TYPE_NULL);

顯示鍵盤

etData.setInputType(InputType.TYPE_CLASS_TEXT);
etData.setFocusableInTouchMode(true);

在父布局中

android:focusable="false"

只需使用EditText.setFocusable(false); 在活動中

或在xml中使用

android:focusable="false"

您好使用此代碼它對我來說100%正常工作

EditText ee=new EditText(this);
ee.setShowSoftInputOnFocus(false);

*但你只想隱藏鍵盤上的edittext點擊下面給出的代碼將工作但鍵盤圖標和后退按鈕的向上標志將創建*給定的代碼是這樣的

        EditText ee=new EditText(this);//create edittext
        final   View.OnTouchListener disable =new View.OnTouchListener() {
        public boolean onTouch (View v, MotionEvent event) {
        v.onTouchEvent(event);
        InputMethodManager img = 
        (InputMethodManager)v.getContext().getSystemService(INPUT_METHOD_SERVICE);
        if (img != null) {
        img.hideSoftInputFromWindow(v.getWindowToken(),0);
        }
        return true;
        }
        };
        //call touch listener
       ee.setOnTouchListener(disable);

在文本更改偵聽器的情況下也使用它

        EditText ee= new EditText(this);
        Text=new TextWatcher(){

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int 
        after) {
        InputMethodManager img = (InputMethodManager) 
        getSystemService(INPUT_METHOD_SERVICE);
        img.hideSoftInputFromWindow(ee.getWindowToken(), 0);

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        InputMethodManager img = (InputMethodManager) 
        getSystemService(INPUT_METHOD_SERVICE);
        img.hideSoftInputFromWindow(ee.getWindowToken(), 0);

        }
       @Override
        public void afterTextChanged(Editable s) {
        InputMethodManager img = (InputMethodManager) 
        getSystemService(INPUT_METHOD_SERVICE);
        img.hideSoftInputFromWindow(ee.getWindowToken(), 0);

        }
        };
       ee.addTextChangedListener(Text);

在onclick監聽器的情況下也使用它

 EditText ee=new EditText(this);
 ee.setOnClickListener(new View.OnClickListener() {
 public void onClick(View v) {

 InputMethodManager img= (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
 img.hideSoftInputFromWindow(ee.getWindowToken(),0);

 }});

所有代碼都可以使用,但對我來說,第一個代碼是最好的

只需使用以下方法

private fun hideKeyboard(activity: Activity, editText: EditText) {
    editText.clearFocus()
    (activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager).hideSoftInputFromWindow(editText.windowToken, 0)
}
   public class NonKeyboardEditText extends AppCompatEditText {

    public NonKeyboardEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onCheckIsTextEditor() {
        return false;
    }
}

並添加

NonKeyboardEditText.setTextIsSelectable(true);

暫無
暫無

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

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