简体   繁体   English

防止在EditText Focus上打开软键盘

[英]Prevent Softkeyboard from opening on EditText Focus

I need to prevent softkeyboard from showing up when Edit Text gains focus or if user taps on the edit text because user has to use a barcode scanner as an input method. 我需要防止在“编辑文本”获得焦点或用户点击编辑文本时显示软键盘,因为用户必须使用条形码扫描仪作为输入方法。 But I need is open the keyboard when user clicks/taps a specific button. 但是我需要在用户单击/轻击特定按钮时打开键盘。

I have tried focusableInTouchMode property and it works fine in preventing the softkeyboard from popping but it won't let the edit text gain focus. 我曾尝试focusableInTouchMode财产,它在防止弹出的softkeyboard不错,但它不会让编辑文本获取焦点。

Any hint on how to achieve this? 关于如何实现这一点的任何提示?

myEditText.setInputType(InputType.TYPE_NULL);

myEditText.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // showMyDialog();
    }
});

myEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            // showMyDialog();
        }
    }
});

Try this.... Just return null or anything on onclicklistner.. 尝试这个。

Njoy.. :) And m using it so don't tell me its not working.. LOL.. Njoy .. :)而且我正在使用它,所以不要告诉我它不起作用..大声笑..

I have one condition that if user clicks on edittext then it ll pops DatePicker Dialog and its working. 我有一个条件,如果用户单击edittext,它将弹出DatePicker对话框并开始工作。 So njoy dude...: ;) 所以njoy dude ...:;)

您可以使用此代码隐藏键盘

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

You should use stateAlwaysHidded. 您应该使用stateAlwaysHidded。 Either in runtime as in the first answer, or directly in your project Manifest: 如在第一个答案中那样在运行时中,或者直接在您的项目清单中:

android:windowSoftInputMode="stateAlwaysHidden"

This will prevent keyboard from appearing during onCreate/onResume. 这样可以防止在onCreate / onResume期间出现键盘。

To prevent keyboard appearing after clicking EditText, you can place some blank view with transparent backround right on top of your EditText: 为防止单击EditText后出现键盘,您可以在EditText顶部放置一些带有透明背景的空白视图:

<View
    android:id="@+id/view"
    android:layout_width="editTextWidth"
    android:layout_height="editTextWidth"
    android:background="@android:color/transparent"
    android:clickable="true"/>

The key attribute here is android:clickable="true" . 这里的关键属性是android:clickable="true" It will prevent from gaining focus on click (actually users will click on this blank view, not EditText). 这将阻止您将注意力集中在单击上(实际上用户将单击此空白视图,而不是EditText)。

Then, when user clicks special button you've mentioned, you just remove this blank view and call editText.requestFocus() 然后,当用户单击您提到的特殊按钮时,只需删除此空白视图并调用editText.requestFocus()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM