如何在android中的Alert对话框中验证文本框

时间:2016-06-23 09:33:28

标签: java android xml android-studio

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

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();

2 个答案:

答案 0 :(得分:1)

您可以使用带有Toast消息的条件在正面点击按钮上调用文本验证方法。 或者您可以将addTextChangeListener添加到输入文本的textview中,以便每次更改文本时调用验证方法。

答案 1 :(得分:0)

首次导入类为 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();
                }
            }
        });