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

发布于 2024-12-03 01:14:39 字数 2251 浏览 1 评论 0原文

我知道这里有很多关于这个问题的问题。但他们都没有我正在寻找的答案。

我在 ScrollView 中有 7 个 ET。当我启动应用程序时,没有 ET 具有焦点,因为我已将以下两行添加到我的整体布局中:

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

当我单击 ET 时,会显示我想要的软键盘,然后我设置值(假设为 20)。我按“2”,然后按“0”,然后按后退按钮。此时键盘消失,但焦点仍然存在。当按后退按钮隐藏键盘时,我也想清除焦点。

因为当焦点被清除时,ET 的布局就按照我想要的那样设置了。

他们都有一些代码,即:

    // 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);
            }
        }
    });

我使用焦点侦听器将 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");
                }
            }
        }
    }

};

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. When I start the application no ET has focus because I've added the following two lines to my overall layout:

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). I press a '2' followed by a '0' and then press the back button. 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.

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.

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");
                }
            }
        }
    }

};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不一样的天空 2024-12-10 01:14:39
 Just override `onBackPressed()` 

活动中的方法并检查 Edittext 的条件
跑步前集中注意力
super.onBackPressed();

 @Override
            public void onBackPressed() {
        
                if( et_search.isFocused()){
                    et_search.clearFocus();
                }else {
                    super.onBackPressed();
                }
            }
 Just override `onBackPressed()` 

method in your activity and check condition for Edittext
focus before run
super.onBackPressed();

 @Override
            public void onBackPressed() {
        
                if( et_search.isFocused()){
                    et_search.clearFocus();
                }else {
                    super.onBackPressed();
                }
            }
踏雪无痕 2024-12-10 01:14:39

为此,您必须在布局文件的父布局上获取 onTouchListener。在 TouchListener 上,您必须编写代码以在单击 EditText 外部时隐藏键盘。请按照以下 XML 布局和 Java 类来解决此问题。

请按照以下网址解决此问题 http: //amitthaperandroidquery.blogspot.com/2011/10/remove-keyboard-after-click-outside.html

For this you have to take the onTouchListener on the parent layout of the Layout File. on the TouchListener you have to code to hide the Keyboard when click outside the EditText. Please follow following XML Layout and Java class to resolve this issue.

Please follow the follow the following url to resolve this issue http://amitthaperandroidquery.blogspot.com/2011/10/remove-keyboard-after-click-outside.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文