简体   繁体   English

隐藏键盘并按按钮显示

[英]Hide Keyboard and show it by button

I am creating a program that write in EditText by barcode reader so I don't want to show the keyboard immediately even if I focused on it I don't wanna it to be visible , I need to press a button to show keyboard only to Edit sometimes . 我正在创建一个通过条形码阅读器在EditText中编写的程序,所以即使我专注于该程序,我也不想立即显示键盘,我也不想让它可见,我需要按下一个按钮以仅显示键盘有时编辑。 and thanks 谢谢

I would disable the EditText button from the beginning: 我将从一开始就禁用EditText按钮:

editText.setEnabled(false);

And to answer your question, yes. 并回答您的问题,是的。 Even if it is disabled, you can change the text. 即使已禁用,也可以更改文本。 Disabled only means user can't change it. 禁用仅表示用户无法更改它。 You can programmatically edit it. 您可以以编程方式对其进行编辑。

Then when the button is pressed: 然后,当按下按钮时:

button.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        editText.setEnabled(true);
        editText.requestFocus();
    }
});

This should automatically show the keyboard when the button is pressed. 当按下按钮时,它将自动显示键盘。

Bonus: If you want to disable the EditText once the editing is done, you can do this: 奖励:如果要在编辑完成后禁用EditText ,则可以执行以下操作:

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if(!hasFocus) {
            editText.setEnabled(false);
        }
    }
});

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

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