这是我正在查看的当前AddDebt.java部分:
public void ButtonOnClick(View v) {
Intent intent = new Intent(this, MainActivity.class);
EditText debtors = (EditText) findViewById(R.id.editDebtor);
String debtor = debtors.getText().toString();
EditText myEdit = (EditText) findViewById(R.id.editBalance);
String myEditValue = myEdit.getText().toString();
double loanAmount = Double.parseDouble(myEditValue);
EditText myEdit2 = (EditText) findViewById(R.id.editRate);
String myEditValue2 = myEdit2.getText().toString();
double interestRate = Double.parseDouble(myEditValue2);
EditText myEdit3 = (EditText) findViewById(R.id.editTerm);
String myEditValue3 = myEdit3.getText().toString();
Double loanPeriod = Double.parseDouble(myEditValue3);
double r = interestRate/1200;
double r1 = Math.pow(r+1,loanPeriod);
double editMnthlypmt = (double) ((r+(r/(r1-1))) * loanAmount);
DecimalFormat df = new DecimalFormat("#.##");
editMnthlypmt = Double.valueOf(df.format(editMnthlypmt));
TextView textMnthlypmt = (TextView)findViewById(R.id.textMntlypmt);
switch (v.getId()) {
case R.id.calculate:
textMnthlypmt.setText("" + String.valueOf(editMnthlypmt));
break;
case R.id.addDebt:
if(debtors.getText().length() == 0){
Toast.makeText(getApplicationContext(), "Please enter debtors value", Toast.LENGTH_SHORT).show();
debtors.requestFocus();
}else if(myEdit.getText().length() == 0){
Toast.makeText(getApplicationContext(), "Please enter myedit value", Toast.LENGTH_SHORT).show();
myEdit.requestFocus();
}
else
{
//Transferring data to MainActivity
intent.putExtra("debtor",debtor);
intent.putExtra("loanAmount",loanAmount);
intent.putExtra("editMnthlypmt",editMnthlypmt);
//Next moves back to MainActivity
startActivity(intent);
}
break;
}
}
当选择“case R.id.addDebt:”时,我想确保editDebtor,editBalance,editRate和editTerm都已完成。如果没有,我希望它将焦点放在最不完整的最上面的框中。如果完成,我希望它切换到我的意图。
任何建议。
答案 0 :(得分:1)
试试这个,
case R.id.addDebt:
if(debtors.getText().length() == 0){
Toast.makeText(Activityname, "Please enter debtors value", Toast.LENGTH_SHORT).show();
debtors.requestFocus();
}else if(myEdit.getText().length() == 0){
Toast.makeText(Activityname, "Please enter myedit value", Toast.LENGTH_SHORT).show();
myEdit.requestFocus();
}else if().......
}else{
//Add your Intent here
}