0

嘿伙计们,我有Activity一些EditText领域。

用户需要在按下 save 之前填写所有文本字段Button

这是我认为可行的方法。我检查了在EditText字段中输入的文本,如果文本不为空,则将相应的数据保存在数据库中,但如果文本为空,Toast则显示 a 并显示一条消息以重新输入文本。这是代码:

          account_Number.setText(null);
          customer_Number.setText(null);
          account_Holder.setText(null);
          bank_Name.setText(null);
          branch_Name.setText(null);
          branch_Address.setText(null);
          Ifsc.setText(null);
          Micr.setText(null);
          current_Balance.setText(null);

    AddAccount = (Button) findViewById(R.id.addAccount);
    AddAccount.setOnClickListener(this);

}

// only after all the fields have been filled, should the message data added
// successfully be displayed.
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.addAccount:// try making the user enter in all the fields
        boolean work = true;
        if (account_Number.getText() != null
                && customer_Number.getText() != null
                && account_Holder.getText() != null
                && bank_Name.getText() != null
                && branch_Name.getText() != null
                && branch_Address.getText() != null
                && Ifsc.getText() != null && Micr.getText() != null
                && current_Balance.getText() != null) {
            try {
                String accountNumber = account_Number.getText().toString();

                String customerNumber = customer_Number.getText()
                        .toString();

                String accountHolder = account_Holder.getText().toString();

                String bankName = bank_Name.getText().toString();
                String branchName = branch_Name.getText().toString();
                String branchAddress = branch_Address.getText().toString();

                String ifsc = Ifsc.getText().toString();
                String micr = Micr.getText().toString();
                String currentBalance = current_Balance.getText()
                        .toString();

                DatabaseClass entry = new DatabaseClass(AddnewAccount.this);
                entry.open();
                entry.createEntry(accountNumber, customerNumber,
                        accountHolder, bankName, branchName, branchAddress,
                        ifsc, micr, currentBalance);
            } catch (Exception e) {

                work = false;
                Dialog message = new Dialog(this);
                message.setTitle("Error");
                String error = e.toString();
                TextView tv = new TextView(this);
                tv.setText(error);
                message.setContentView(tv);

                message.show();

            } finally {
                Toast message = Toast.makeText(AddnewAccount.this,
                        "Data Added Successfully", Toast.LENGTH_SHORT);
                message.show();
                Intent goBackHome = new Intent(AddnewAccount.this,
                        AccountManagerActivity.class);
                startActivity(goBackHome);
                finish();

            }
        } else {
            Toast message = Toast.makeText(AddnewAccount.this, // not getting executed
                    "Please Fill All The Fields", Toast.LENGTH_LONG);
            message.show();
            Intent refill = new Intent(AddnewAccount.this,
                    AddnewAccount.class);
            startActivity(refill);
            finish();

        }
        break;
    }

}
4

2 回答 2

1

检查喜欢,,,

String a_number = account_Number.getText().tostring().trim();
String c_number = customer_Number.getText().tostring().trim();

if(TextUtils.isEmpty(a_number) || TextUtils.isEmpty(c_number)){
      Toast.makeText(AddnewAccount.this, 
                "Please Fill All The Fields", Toast.LENGTH_LONG).show();
}else{
      //Nothing is empty...
}  
于 2012-07-25T18:05:26.340 回答
1

空白EditText字段不返回nullfor getText(),而是返回""

只需检查.getText().toString().trim().equals("")每个字段是否。

于 2012-07-25T18:10:09.763 回答