android中的警报对话框验证

时间:2012-04-16 07:42:13

标签: android

当我单击保存按钮而不输入值时,对话框会消失。我想显示对话框以及想要进行验证。如果有人知道,请帮助我。

这是我的代码:

 TextView tvUserName=new TextView(this);
            TextView tvPassword=new TextView(this);
            TextView tvURL=new TextView(this);
            final EditText etUserName=new EditText(this); 
            final EditText etPassword=new EditText(this);
            final AlertDialog.Builder alert = new AlertDialog.Builder(this);    
            LinearLayout login= new LinearLayout(this);
            login.setOrientation(1); //1 is for vertical orientation
            tvUserName.setText(getResources().getString(R.string.username));
            tvPassword.setText(getResources().getString(R.string.password));
            login.addView(tvURL);
            login.addView(etURL);
            login.addView(tvUserName);
            login.addView(etUserName);
            login.addView(tvPassword);
            etPassword.setInputType(InputType.TYPE_CLASS_TEXT |InputType.TYPE_TEXT_VARIATION_PASSWORD);
            login.addView(etPassword);
            alert.setView(login);
            alert.setTitle(getResources().getString(R.string.login));
            alert.setPositiveButton(getResources().getString(R.string.login), new DialogInterface.OnClickListener() {             
                public void onClick(final DialogInterface dialog, int whichButton) {   
                    strUserName=etUserName.getText().toString();
                    XmlUtil.username=strUserName;
                    strPassword=etPassword.getText().toString();
                    strhwdXml=etURL.getText().toString();
                    if((strUserName.length()==0)||(strPassword.length()==0){
    Toast.makeText(getBaseContext(),"Please enter username and password", Toast.LENGTH_SHORT).show();

                    }else {
                        Toast.makeText(getBaseContext(),"Success", Toast.LENGTH_SHORT).show();
                    }

3 个答案:

答案 0 :(得分:1)

   private void showAlertBox() {
        AlertDialog alertBox = null;
        alertBox = new AlertDialog.Builder(this).create();
        alertBox.setTitle("titleText");
        alertBox.setMessage("messageText");
        alertBox.setCancelable(false);

        LayoutInflater inflater = LayoutInflater.from(this);
        View alert_webview = inflater.inflate(R.layout.alert_layout, null);
        alertBox.setView(alert_webview);
        btnfromlayout = (Button) alert_webview.findViewById(R.id.btnclose);
        btnfromlayout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // Whatever you want on click...compare
                // if(strUserName.equalsIgnoreCase("") ||
                // strPassword.equalsIgnoreCase("")){
                // Your toast msg
                // }else{
                // cAlert.dismiss();
                // }
                Log.i("Close", "Close");
            }
        });

        alertBox.show();
    }


@Override
    public void onClick(View v) {
        int vId = v.getId();
        switch (vId) {
        case R.id.btnclose:
            //Whatever you want on click...compare
//if(strUserName.equalsIgnoreCase("") || strPassword.equalsIgnoreCase("")){
//Your toast msg
//}else{
//          cAlert.dismiss();
//}
            Log.i("Close", "Close");
            break;
        default:
            break;
        }

    }

R.layout.YOUR_XML_LAYOUT_FILE

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/txtcustmessage"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Txt custom Layout For Alert" />

    <Button
        android:id="@+id/btnclose"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Close Custom" />

</LinearLayout>


///与上面的布局一样,你可以为设计的登录控件创建自己的布局 并根据布局和所需的操作设置点击列表器

答案 1 :(得分:0)

我认为制作这种AlertDialog的最佳方法是为它制作一个新的自定义布局,就像你为活动做的那样。如果您正在使用setPositiveButton等,则在按下时会关闭默认对话框。

答案 2 :(得分:0)

最好还包括按钮而不是使用setpositivebutton。如果你有按钮,那么你可以随心所欲。