编辑文本字段需要建议

时间:2012-07-25 18:04:02

标签: android

嘿伙计我有ActivityEditText个字段。

在按下保存Button之前,用户需要填写所有文本字段。

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

          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;
    }

}

2 个答案:

答案 0 :(得分: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...
}  

答案 1 :(得分:1)

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

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