英文:
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并将逻辑放入其中。只需确保更改edittext
和textview
的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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论