如何验证android中警报对话框中的文本框

问题描述:

我正在处理带有一些文本框的警报对话框,其中需要验证文本框,如果它们正确,则弹出消息需要消失这里的问题是,如果用户单击确定按钮(带有空值),弹出消息就会消失.提前致谢...

i am working on alert dialog with some text boxes where the text boxes needed to be validated and if they are correct only then the pop up message need to be disappeared The problem here was, if user clicks ok button(with empty values) the pop up message was disappearing. Thanks in advance...

final AlertDialog.Builder builder = new AlertDialog.Builder(KmsActivity.this);
builder.setTitle("Enter OrderId,BillNo");
builder.setCancelable(false);
final EditText input1 = new EditText(KmsActivity.this);
final EditText input2 = new EditText(KmsActivity.this);
input1.setHint("Enter OrderId");
input2.setHint("Enter BillNo");
LinearLayout linearLayout = new LinearLayout(KmsActivity.this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.addView(input1);
linearLayout.addView(input2);
builder.setView(linearLayout);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface popupDialog, int which) {
        String orderId = input1.getText().toString();
        String billNo = input2.getText().toString();
        Log.d(TAG_NAME, "order id:" + orderId);
        Log.d(TAG_NAME, "bill no" + billNo);
        if (orderId.length()<=0) {
            Toast.makeText(KmsActivity.this, "Please enter Order ID", Toast.LENGTH_LONG).show();
        } else if (billNo.length()<=0) {
            Toast.makeText(KmsActivity.this, "Please enter Bill No", Toast.LENGTH_LONG).show();
        } else {
            tripObjects.get(0).setOrderId(orderId);
            tripObjects.get(0).setBillNo(billNo);
            tripObjects.get(0).saveInBackground();
            Toast.makeText(KmsActivity.this, "values uploaded", Toast.LENGTH_LONG).show();
            popupDialog.cancel();
        }
    }
});
builder.show();

First import class as - import android.support.v7.app.AlertDialog;

然后试试这个 -

final EditText input1 = new EditText(MainActivity.this);
        final EditText input2 = new EditText(MainActivity.this);
        input1.setHint("Enter name1");
        input2.setHint("Enter Name2");
        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        linearLayout.addView(input1);
        linearLayout.addView(input2);

        final AlertDialog builder = new AlertDialog.Builder(MainActivity.this)
                .setTitle("Sign In Failed")
                .setCancelable(false)
                .setMessage("Invalid username or password").setView(linearLayout).setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                }).create();
        builder.show();
        ((AlertDialog)builder).getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (input1.length() <= 0) {
                    Toast.makeText(MainActivity.this, "Please Enter Name", Toast.LENGTH_SHORT).show();

                } else {
                    Toast.makeText(MainActivity.this, "OK", Toast.LENGTH_SHORT).show();
                    builder.dismiss();
                }
            }
        });