我非常新的Android得。 我尝试开发一个Android应用程序获取用户输入项目的总金额。 这里是我的应用程序的草图。 在这里输入图像描述
用户必须输入第一行col1和山口2.但是,第一个行COL3可以进入与否。 在子1周的TextView第一行的合计值应显示。 此值也应显示结果的TextView。 同样,如果用户将数据插入到第二行中,值必须输入到col1和col2的下方。 如果用户喜欢,可以输入值到下山口3.第二行总计值应在子2的TextView显示。 如果用户输入值到第二行中,然后将结果的TextView应自动更新为Sub 1的合计值和子2个textviews。 我尝试这样做如下,但是我无法找到正确的方式来做到这一点。 请如果有人可以帮助我解决这个问题,我真的很感谢。 这是Java类
public class AddTwo extends AppCompatActivity {
EditText edit1, edit2, edit3;
EditText edit4, edit5, edit6;
TextView textViewSub1, textViewSub2, textViewResult;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_add_two);
/*First row variables*/
edit1 = (EditText) findViewById(R.id.editText1);
edit2 = (EditText) findViewById(R.id.editText2);
edit3 = (EditText) findViewById(R.id.editText3);
textViewSub1 = (TextView) findViewById(R.id.TextViewsub1);
/*Second row variables*/
edit4 = (EditText) findViewById(R.id.editText5);
edit5 = (EditText) findViewById(R.id.editText6);
edit6 = (EditText) findViewById(R.id.editText7);
textViewSub2 = (TextView) findViewById(R.id.TextViewsub2);
/*Final Total TextView variable*/
textViewResult = (TextView) findViewById(R.id.textView_result);
//use TextWatcher for edit1 in First Row Col1
edit1.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
//textViewResult.setText(addNumbers());
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
//use TextWatcher for first First Row Col2
edit2.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
//textViewResult.setText(addNumbers());
}
});
//use TextWatcher for first First Row Col3
edit3.addTextChangedListener(new TextWatcher() {
@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 editable) {
textViewResult.setText(addNumbers());
}
});
}
//Define method addString()
private String addNumbers() {
//declaring variables
int number1, number2;
double num1, num2;
double d1, d2;
double da1, da2;
Double orderTotal1 = null, orderTotal2 = null;
Double ordTot1 = null, ordTot2 = null;
DecimalFormat df = new DecimalFormat("0.00##");
//For First Row
/*if only the first and second edittext fields in First Row is filled*/
if ((edit1.getText().toString() != "" && edit1.getText().length() > 0) && (edit2.getText().toString() != "" && edit2.getText().length() > 0)) {
num1 = Double.parseDouble((edit1.getText().toString()));
number1 = Integer.parseInt(edit2.getText().toString());
ordTot1 = num1 * number1;
textViewSub1.setText(df.format(ordTot1));
/*else if the first,secondn third (all) edittext fields in First Row is filled*/
} else if ((edit1.getText().toString() != "" && edit1.getText().length() > 0) && (edit2.getText().toString() != "" && edit2.getText().length() > 0) && (edit3.getText().toString() != "" && edit3.getText().length() > 0)) {
num1 = Double.parseDouble((edit1.getText().toString()));
number1 = Integer.parseInt(edit2.getText().toString());
d1 = Double.parseDouble(edit3.getText().toString());
da1 = 100 - d1;
orderTotal1 = ((da1 * number1) * num1) / 100;
textViewSub1.setText(df.format(orderTotal1));
} else {
number1 = (int) 0.00;
num1 = 0.00;
da1 = 0.00;
}
//For the Second Row
/*if only the first and second edittext fields in Second Row is filled*/
if ((edit4.getText().toString() != "" && edit4.getText().length() > 0) && (edit5.getText().toString() != "" && edit5.getText().length() > 0)) {
num2 = Double.parseDouble((edit4.getText().toString()));
number2 = Integer.parseInt(edit5.getText().toString());
ordTot2 = num2 * number2;
textViewSub2.setText(df.format(ordTot2));
/*else if the first,secondn third (all) edittext fields in Second Row is filled*/
} else if ((edit4.getText().toString() != "" && edit4.getText().length() > 0) && (edit5.getText().toString() != "" && edit5.getText().length() > 0) && (edit6.getText().toString() != "" && edit6.getText().length() > 0)) {
num2 = Double.parseDouble((edit1.getText().toString()));
number2 = Integer.parseInt(edit2.getText().toString());
d2 = Double.parseDouble(edit3.getText().toString());
da2 = 100 - d2;
orderTotal2 = ((da2 * number2) * num2) / 100;
textViewSub2.setText(df.format(orderTotal2));
} else {
number2 = (int) 0.00;
num2 = 0.00;
da2 = 0.00;
}
//return statement
return df.format((orderTotal1) + (orderTotal2));
}
}