【问题标题】:Android how to change contents of edittext in real time [duplicate]Android如何实时更改edittext的内容[重复]
【发布时间】:2013-07-25 10:04:14
【问题描述】:

我正在寻找将一个单位转换为另一个单位(比如货币)的应用程序。所以它由2个编辑文本组成。一种是用户输入值,另一种是包含结果。现在,在这里,我希望在第一个编辑文本中输入值时,转换后的值出现在第二个编辑文本中,而不是使用“转换”按钮将值放入第二个编辑文本中。我怎样才能做到这一点? 谢谢

【问题讨论】:

    标签: android android-edittext


    【解决方案1】:

    为此使用TextWatcher。在用户输入的EditText 上设置:

    myEditText1.addTextChangedListener(new TextWatcher() {
    
            @Override
            public void afterTextChanged(Editable s) {
                            String value = s.toString();
    
                            // Perform computations using this string
                            // For example: parse the value to an Integer and use this value
    
                            // Set the computed value to the other EditText
                            myEditText2.setText(computedValue);
            }
    
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
            }
    
            @Override
            public void onTextChanged(final CharSequence s, int start, int before, int count) {             
    
            }
    
        });
    

    编辑 1

    检查空字符串"":

    myEditText1.addTextChangedListener(new TextWatcher() {
    
        @Override
        public void afterTextChanged(Editable s) {
            String value = s.toString();
    
            if (value.equals("")) {
                myEditText1.setText("0");
    
                // You may not need this line, because "myEditText1.setText("0")" will
                // trigger this method again and go to else block, where, if your code is set up
                // correctly, myEditText2 will get the value 0. So, try without the next line
                // and if it doesn't work, put it back.
                myEditText2.setText("0");
            } else {
    
                // Perform computations using this string
                // For example: parse the value to an Integer and use this value
    
                // Set the computed value to the other EditText
                myEditText2.setText(computedValue);
            }
    }
    
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
        }
    
        @Override
        public void onTextChanged(final CharSequence s, int start, int before, int count){              
    
        }
    
    });
    

    【讨论】:

    • 是的,这行得通。但例如,我输入要转换为卢比的美元值 23。它给出了正确的输出。然后,如果我按后退按钮将其更改为首先删除“3”。最后,如果我也尝试删除“2”,那么它就会崩溃。如果我清空第一个编辑文本,我希望它变为 0 美元和 0 卢比。我该怎么做?
    • 是的,这是因为每当myEditText1 中的文本发生更改时,都会触发onTextChanged(CharSequence, int, int, int)。您必须检查空字符串""。请参阅上面的编辑 1
    • @ShivamBhalla 你可能收到了NumberFormatException。上面的编辑 1 应该可以解决您的问题。
    • 我试过了,但我仍然面临同样的问题,numberformatexception invalid double:""
    • 对此有何想法?
    猜你喜欢
    • 1970-01-01
    • 2017-11-25
    • 1970-01-01
    • 1970-01-01
    • 2012-11-08
    • 2018-06-23
    • 2018-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多