警告对话框确认在android系统 [英] Alert Dialog Validation in android

查看:133
本文介绍了警告对话框确认在android系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我点击保存按钮,而无需输入值对话框得到disappear.I要显示的对话框以及想做的validation.Please帮助我,如果有人知道。

下面是我的code:

  TextView的tvUserName =新的TextView(本);
            TextView的tvPassword =新的TextView(本);
            TextView的tvURL =新的TextView(本);
            最终的EditText etUserName =新的EditText(本);
            最终的EditText etPassword =新的EditText(本);
            最后AlertDialog.Builder警报=新AlertDialog.Builder(本);
            登录的LinearLayout =新的LinearLayout(本);
            login.setOrientation(1); // 1是垂直取向
            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(登录);
            alert.setTitle(getResources()的getString(R.string.login));
            alert.setPositiveButton(getResources()。的getString(R.string.login),新DialogInterface.OnClickListener(){
                公共无效的onClick(最终DialogInterface对话,诠释whichButton){
                    。strUserName中= etUserName.getText()的toString();
                    XmlUtil.username = strUserName中;
                    。strPassword = etPassword.getText()的toString();
                    。strhwdXml = etURL.getText()的toString();
                    如果((strUserName.length()== 0)||(strPassword.length()== 0){
    Toast.makeText(getBaseContext(),请输入用户名和密码,Toast.LENGTH_SHORT).show();                    }其他{
                        Toast.makeText(getBaseContext(),成功,Toast.LENGTH_SHORT).show();
                    }


解决方案

 私人无效showAlertBox(){
        AlertDialog alertBox = NULL;
        alertBox =新AlertDialog.Builder(本).create();
        alertBox.setTitle(titleText);
        alertBox.setMessage(MessageText中);
        alertBox.setCancelable(假);        LayoutInflater吹气= LayoutInflater.from(本);
        查看alert_webview = inflater.inflate(R.layout.alert_layout,NULL);
        alertBox.setView(alert_webview);
        btnfromlayout =(按钮)alert_webview.findViewById(R.id.btnclose);
        btnfromlayout.setOnClickListener(新OnClickListener(){            @覆盖
            公共无效的onClick(视图v){
                //无论你想在点击...比较
                //如果(strUserName.equalsIgnoreCase()||
                // strPassword.equalsIgnoreCase()){
                //你敬酒味精
                //}其他{
                // cAlert.dismiss();
                //}
                Log.i(关闭,关闭);
            }
        });        alertBox.show();
    }
@覆盖
    公共无效的onClick(视图v){
        INT VID = v.getId();
        开关(VID){
        案例R.id.btnclose:
            //无论你想在点击...比较
//如果(strUserName.equalsIgnoreCase()|| strPassword.equalsIgnoreCase()){
//你敬酒味精
//}其他{
// cAlert.dismiss();
//}
            Log.i(关闭,关闭);
            打破;
        默认:
            打破;
        }    }

R.layout.YOUR_XML_LAYOUT_FILE

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直>    <的TextView
        机器人:ID =@ + ID / txtcustmessage
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=TXT自定义布局警报/>    <按钮
        机器人:ID =@ + ID / btnclose
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=关闭自定义/>< / LinearLayout中>



///像上面的布局U可以与设计登录控件创建自己的布局
并设置点击listners按照布局和所需的行动

When i click the save button without entering values the dialog gets disappear.I want to show the dialog box as well as want to do the validation.Please help me if anybody knows.

Here is my code:

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

解决方案

   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>


/// Like the above layout u can create your own layout for login controls with design and set click listners as per the layout and required action

这篇关于警告对话框确认在android系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆