0

やあみんな私はActivityいくつかのEditTextフィールドを持っています。

ユーザーは、保存を押す前にすべてのテキスト フィールドに入力する必要がありますButton

これが私がうまくいくと思ったものです。フィールドに入力されたテキストを確認しましたEditText。テキストが null でない場合は、それぞれのデータがデータベースに保存されますが、テキストが null の場合は、テキスト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;
    }

}
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フィールドはnullforgetText()ではなく、 を返します""

.getText().toString().trim().equals("")各フィールドでどうかを確認してください。

于 2012-07-25T18:10:09.763 に答える