Display automatically an Integer value from Edit Text (entered by the user) to Text View without any actions (such as onButtonClick) in Android

huangapple go评论89阅读模式
英文:

Display automatically an Integer value from Edit Text (entered by the user) to Text View without any actions (such as onButtonClick) in Android

问题

尝试从用户输入的编辑框中获取整数(例如总金额),并通过乘以某个值(例如5%)来显示,在文本视图中自动显示。

对于第一部分,即从编辑框中简单地显示整数到文本视图中,出现了NumberFormatException错误。

temp = (Integer.parseInt(editText.getText().toString()));

textView.setText(temp);

至于第二部分,即自动从编辑框中将值显示到文本视图中,我不知道如何处理。

英文:

Trying to get an integer from Edit Text entered by the user(such as Total Amount) and to display it by multiplying with value(such as 5%) and display it using TextView automatically.

For the 1st part i.e. simply displaying the integer from EditText to TextView got NumberFormatException Error.

'''
temp = (Integer.parseInt(editText.getText().toString()));

textView.setText(temp);

'''

And for the second part i.e. automatically displaying the value from EditText to TextView, no idea how to approach.

答案1

得分: 1

只需使用textwatcher并将逻辑放入其中。只需确保更改edittexttextview的id。

et.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) { }
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
    @Override
    public void afterTextChanged(Editable s) {
        String value = s.toString();
        double outputValue = Integer.parseInt(value) * 0.05;
        textView.setText(String.valueOf(outputValue));
    }
});

确保在edittext中只捕获数字值。

英文:

Just use textwatcher and put the logic in it. Just make sure change the id of edittext and textview

et.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) { }
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
    @Override
    public void afterTextChanged(Editable s) {
        String value = s.toString();
        double outputValue = Integer.parseInt(value) * 0.05;
        textView.setText(String.valueOf(outputValue));
    }
});

Make sure in edittext that it only capture the numerical value.

答案2

得分: 1

你可以尝试这个方法:

editText.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) { }
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
    @Override
    public void afterTextChanged(Editable s) {
        String userInputText = s.toString();
        try {
            int userInputAsNumber = Integer.parseInt(userInputText);
        } catch (NumberFormatException e) {
            System.out.println(e.getMessage());
        }
    }
});

以确保它不会出错。

英文:

you could try this approach

    editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) { }
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
        @Override
        public void afterTextChanged(Editable s) {
            String userInputText = s.toString();
        try {
            val userInputAsNumbger = userInputText.toInt();
        }catch (e:NumberFormatException){
            print(e.message)
        }
        }
    });

to ensure it doesn't error

huangapple
  • 本文由 发表于 2020年9月29日 18:14:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/64117501.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定