使用TextWatcher获得进入两个值的EditText,没有一个按钮,这些值相乘。自动计算。 [英] Using TextWatcher to get values entered to two edittext and multiplying these values without a button. Automatic computation.
问题描述
我要当用户进入所要求的两个数值为EditText上,而无需按钮,该应用程序自动计算。
有没有办法做到这一点,而无需使用TextWatcher?
这是我的尝试。为什么犯规工作?该应用程序关闭力时的EditText输入数字。
公共类BodyMassIndex扩展活动{双重效果;
的EditText年龄,身高,体重;TextView的tvResult;INT ageInt;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
// TODO自动生成方法存根
super.onCreate(savedInstanceState);
的setContentView(R.layout.bmi);
初始化(); TextWatcher textWatcher =新TextWatcher(){ @覆盖
公共无效afterTextChanged(编辑S){
// TODO自动生成方法存根
计算(); } @覆盖
公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,
INT后){
// TODO自动生成方法存根 } @覆盖
公共无效之前onTextChanged(CharSequence中,诠释开始,诠释,
诠释计数){
// TODO自动生成方法存根 } }; height.addTextChangedListener(textWatcher);
weight.addTextChangedListener(textWatcher);}私人无效初始化(){
// TODO自动生成方法存根 年龄=(EditText上)findViewById(R.id.etAge);
身高=(EditText上)findViewById(R.id.etHeight);
体重=(EditText上)findViewById(R.id.etWeight); tvResult =(的TextView)findViewById(R.id.tvResult);
}私人无效计算(){
// TODO自动生成方法存根
双heightInt = 1;
双weightInt = 0; 如果(高度!= NULL)
heightInt = Double.parseDouble(height.getText()的toString()); 如果(重量!= NULL)
weightInt = Double.parseDouble(weight.getText()的toString()); 结果= weightInt /(heightInt * heightInt);
字符串textResult =你的BMI为+结果;
tvResult.setText(textResult);
}}
您应该设置的EditText 0它大概强制关闭,因为你正试图从具有什么也没有一个EditText解析一个空字符串的默认值
编辑:试试这个
如果(高度!= NULL)
heightInt = Double.parseDouble(!height.getText()。的toString()。等于()?
。height.getText()的toString():0);如果(重量!= NULL)
weightInt = Double.parseDouble(!weight.getText()。的toString()。等于()?
。weight.getText()的toString():0);
使用条件这样你在哪里,无论什么你会被解析这不会抛出一个异常值
I want the app to automatically compute when the user enters the requested two values into edittext, without needing a button.
Is there a way to do it without using TextWatcher?
here is my attempt. Why doesnt it work? The app force shuts when a number is entered in edittext.
'
public class BodyMassIndex extends Activity
{
double result;
EditText age, height, weight;
TextView tvResult;
int ageInt;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.bmi);
initialize();
TextWatcher textWatcher = new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
calculate();
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
}
};
height.addTextChangedListener(textWatcher);
weight.addTextChangedListener(textWatcher);
}
private void initialize() {
// TODO Auto-generated method stub
age = (EditText) findViewById(R.id.etAge);
height = (EditText) findViewById(R.id.etHeight);
weight = (EditText) findViewById(R.id.etWeight);
tvResult = (TextView) findViewById(R.id.tvResult);
}
private void calculate() {
// TODO Auto-generated method stub
double heightInt = 1;
double weightInt = 0;
if (height != null)
heightInt = Double.parseDouble(height.getText().toString());
if (weight != null)
weightInt = Double.parseDouble(weight.getText().toString());
result = weightInt / (heightInt * heightInt);
String textResult = "Your BMI is " + result;
tvResult.setText(textResult);
}}
'
You should set the default values of the edittext to 0. Its probably force closing because you are trying to parse an empty string from an edittext that has nothing in it;
EDIT: try this
if (height != null)
heightInt = Double.parseDouble(!height.getText().toString().equals("")?
height.getText().toString() : "0");
if (weight != null)
weightInt = Double.parseDouble(!weight.getText().toString().equals("")?
weight.getText().toString() : "0");
this way your using a condition where no matter what youll be parsing a value that wont throw an exception
这篇关于使用TextWatcher获得进入两个值的EditText,没有一个按钮,这些值相乘。自动计算。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!