我在下面编码如下。但它不起作用。我需要根据哪两个字段输入来执行简单的加法和简单的减法....问题结束......
我需要制作这个文本,因为我不能问问题是否细节不够而且代码不仅仅是细节。所以我只是打字来问我的问题。只需忽略它并查看下面的代码。谢谢
Java代码:
package com.test.easycount;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements TextWatcher {
EditText num1, num2, sum;
int fnumber, snumber, total;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
num1 = (EditText) findViewById(R.id.num1);
num2 = (EditText) findViewById(R.id.num2);
sum = (EditText) findViewById(R.id.sum);
num1.addTextChangedListener(this);
num2.addTextChangedListener(this);
sum.addTextChangedListener(this);
fnumber = Integer.parseInt(num1.getText().toString());
snumber = Integer.parseInt(num2.getText().toString());
total = Integer.parseInt(sum.getText().toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (num1.getText().toString().length() > 0 && num2.getText().toString().length() > 0) {
total = fnumber + snumber;
sum.setText(Integer.toString(total));
} else if (num1.getText().toString().length() > 0 && sum.getText().toString().length() > 0) {
snumber = total - fnumber;
num2.setText(Integer.toString(snumber));
} else if (num2.getText().toString().length() > 0 && sum.getText().toString().length() > 0) {
fnumber = total - snumber;
num1.setText(Integer.toString(fnumber));
}
}
}
请帮我这样做....
答案 0 :(得分:0)
删除初始化变量并将其粘贴到afterTextChanged
@Override
public void afterTextChanged(Editable s) {
try {
fnumber = Integer.parseInt(num1.getText().toString());
} catch (NumberFormatException e) {
fnumber = 0;
Log.e("numberFormat", e.toString());
}
try {
snumber = Integer.parseInt(num2.getText().toString());
} catch (NumberFormatException e) {
snumber = 0;
Log.e("numberFormat", e.toString());
}
try {
total = Integer.parseInt(sum.getText().toString());
} catch (NumberFormatException e) {
total = 0;
Log.e("numberFormat", e.toString());
}
if (num1.getText().toString().length() > 0 && num2.getText().toString().length() > 0) {
total = fnumber + snumber;
sum.setText(Integer.toString(total));
} else if (num1.getText().toString().length() > 0 && sum.getText().toString().length() > 0) {
snumber = total - fnumber;
num2.setText(Integer.toString(snumber));
} else if (num2.getText().toString().length() > 0 && sum.getText().toString().length() > 0) {
fnumber = total - snumber;
num1.setText(Integer.toString(fnumber));
}
}
答案 1 :(得分:0)
你必须在textWatcher中计算fnumber,snumber,total:
假设:
@Override
public void afterTextChanged(Editable s) {
if (num1.getText().toString().length() > 0 && num2.getText().toString().length() > 0) {
fnumber = Integer.parseInt(num1.getText().toString());
snumber = Integer.parseInt(num2.getText().toString());
total = fnumber + snumber;
sum.setText(Integer.toString(total));
} else if (num1.getText().toString().length() > 0 && sum.getText().toString().length() > 0) {
fnumber = Integer.parseInt(num1.getText().toString());
total = Integer.parseInt(sum.getText().toString());
snumber = total - fnumber;
num2.setText(Integer.toString(snumber));
} else if (num2.getText().toString().length() > 0 && sum.getText().toString().length() > 0) {
snumber = Integer.parseInt(num2.getText().toString());
total = Integer.parseInt(sum.getText().toString());
fnumber = total - snumber;
num1.setText(Integer.toString(fnumber));
}
}
从onCreate中删除fnumber,snumber,total initialization。
为便于计算,最好为每个edittext使用单独的textWatcher:
例如:
num1.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
}
});