简体   繁体   English

当显示软键盘时按下后退按钮时清除焦点EditText

[英]Clear focus EditText when back button is pressed when softkeyboard is showing

I know that there are quite some questions out here regarding this question. 我知道这里有很多关于这个问题的问题。 But non of them have the answer that I'm looking for. 但他们中没有人能得到我正在寻找的答案。

I've got 7 ET inside a ScrollView. 我在ScrollView中有7个ET。 When I start the application no ET has focus because I've added the following two lines to my overall layout: 当我启动应用程序时,没有ET有焦点,因为我在我的整体布局中添加了以下两行:

android:focusable="true"
android:focusableInTouchMode="true"

When I click on an ET the softkeyboard is shown, which I want, then I set the value (let say 20). 当我点击ET时,会显示我想要的软键盘,然后我设置了值(比方说20)。 I press a '2' followed by a '0' and then press the back button. 我按下'2'后跟'0'然后按后退按钮。 At this point the keyboard disappears, but the focus stays. 此时键盘消失,但焦点仍然存在。 I would like to clear the focus too when pressing the back button to hide the keyboard. 当按下后退按钮隐藏键盘时,我也希望清除焦点。

Because when the focus is cleared the layout of the ET is set like I want. 因为当清除焦点时,ET的布局设置为我想要的。

They all have about the some code, which is: 他们都有一些代码,这是:

    // Right Cable
    RightCable = (EditText) findViewById (R.id.RightCable);
    RightCable.setRawInputType(Configuration.KEYBOARD_12KEY);
    RightCable.setOnFocusChangeListener(FocusChanged);
    RightCable.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {
            if(RightCable.isFocused()){
                LengthRightCable       = Double.parseDouble(RightCable.getText().toString());
                Calculate();
            }
        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if(s.toString().matches("")) {
                RightCable.setText("0.00");
                Selection.setSelection(RightCable.getText(), 0, 4);
            }
        }
    });

I use a focus listener to change the input of the ET to a number like 5.00 instead of 0. 我使用焦点监听器将ET的输入更改为5.00而不是0的数字。

OnFocusChangeListener FocusChanged = new OnFocusChangeListener() {

    public void onFocusChange(View v, boolean hasFocus) {

        EditText et = (EditText) v;

        et.setSelection(0, et.getText().length());

        if(!hasFocus){
            String userInput = et.getText().toString();

            int dotPos = -1;    

            for (int i = 0; i < userInput.length(); i++) {
                char c = userInput.charAt(i);
                if (c == '.') {
                    dotPos = i;
                }
            }

            if (dotPos == -1){
                et.setText(userInput + ".00");
            } else if(userInput.length() < 5) {
                if ( userInput.length() - dotPos == 1 ) {
                    et.setText(userInput + "00");
                } else if ( userInput.length() - dotPos == 2 ) {
                    et.setText(userInput + "0");
                }
            }
        }
    }

};

For this you have to take the onTouchListener on the parent layout of the Layout File. 为此,您必须在布局文件的父布局上使用onTouchListener。 on the TouchListener you have to code to hide the Keyboard when click outside the EditText. 在TouchListener上,您必须编码以在EditText外部单击时隐藏键盘。 Please follow following XML Layout and Java class to resolve this issue. 请按照以下XML布局和Java类来解决此问题。

Please follow the follow the following url to resolve this issue http://amitthaperandroidquery.blogspot.com/2011/10/remove-keyboard-after-click-outside.html 请按照以下网址解决此问题http://amitthaperandroidquery.blogspot.com/2011/10/remove-keyboard-after-click-outside.html

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

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