Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓;错误的密码或用户名";消息框显示错误_Android_Android Activity - Fatal编程技术网

Android 安卓;错误的密码或用户名";消息框显示错误

Android 安卓;错误的密码或用户名";消息框显示错误,android,android-activity,Android,Android Activity,我正在从事一项有用户名和密码的活动。我使用了else部分中的AlertDialog.Builder来显示“密码或用户名错误”的消息。我不只是为了测试目的而将其与任何数据库连接,使用密码字符串和用户名字符串来比较editText字段的值,在条件下,如果匹配,则将用户带到一个新的屏幕名“NewMenu”。我的问题是,当登录活动启动(应用程序运行)时,它首先显示消息,即else部分,我希望在单击提交按钮且密码或用户名错误后显示此消息。这是密码 public class Login extends Ac

我正在从事一项有用户名和密码的活动。我使用了else部分中的AlertDialog.Builder来显示“密码或用户名错误”的消息。我不只是为了测试目的而将其与任何数据库连接,使用密码字符串和用户名字符串来比较editText字段的值,在条件下,如果匹配,则将用户带到一个新的屏幕名“NewMenu”。我的问题是,当登录活动启动(应用程序运行)时,它首先显示消息,即else部分,我希望在单击提交按钮且密码或用户名错误后显示此消息。这是密码

public class Login extends Activity{


    private String pass=new String();
    private String nam=new String();
    private Button log;
    private View textreg;
    private EditText text1;
    private EditText text2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        textreg=(TextView) findViewById(R.id.textRegister);
        log=(Button) findViewById(R.id.btnsubmit);
        text1=(EditText) findViewById(R.id.txtuname);
        text2=(EditText) findViewById(R.id.txtpassword);



              //IF NOT REGISTERED USERS//
////A textview when clicked takes user to registeration page/////

        textreg.setOnClickListener(new OnClickListener() {

            @Override
                    public void onClick(View view) {
                startActivity(new Intent("com.DRMS.Register"));
                    }
                });


         //                                                        //
        // IF PASSWORD AND USERNAME CORRECT, GOTO NewMenu Activity //
       //                                                        //

        pass="12345";
        nam="12345";

        if(text1.getText().toString().equals(nam) && text2.getText().toString().equals(pass) ){


            log.setOnClickListener(new OnClickListener() {

                @Override

                        public void onClick(View view) {
                    startActivity(new Intent("com.DRMS.NewMenu"));
                        }
                    });

        }

            else{
            AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(this);

            dlgAlert.setMessage("wrong password or username");
            dlgAlert.setTitle("Error Message...");
            dlgAlert.setPositiveButton("OK", null);
            dlgAlert.setCancelable(true);
            dlgAlert.create().show();

            dlgAlert.setPositiveButton("Ok",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });
            }
    }
}

您应该在click事件中编写此alertview代码

log.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View view)
    {
        if(text1.getText().toString().equals(nam) && text2.getText().toString().equals(pass) )
        {
            AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(this);

            dlgAlert.setMessage("wrong password or username");
            dlgAlert.setTitle("Error Message...");
            dlgAlert.setPositiveButton("OK", null);
            dlgAlert.setCancelable(true);
            dlgAlert.create().show();

            dlgAlert.setPositiveButton("Ok",
                    new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                }
            });
        }
        else
        {
            startActivity(new Intent("com.DRMS.NewMenu"));
        }
    }
});

}
您不需要在oncreate方法中检查用户名n pass


希望能有帮助

您应该在单击事件中编写此alertview代码

log.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View view)
    {
        if(text1.getText().toString().equals(nam) && text2.getText().toString().equals(pass) )
        {
            AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(this);

            dlgAlert.setMessage("wrong password or username");
            dlgAlert.setTitle("Error Message...");
            dlgAlert.setPositiveButton("OK", null);
            dlgAlert.setCancelable(true);
            dlgAlert.create().show();

            dlgAlert.setPositiveButton("Ok",
                    new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                }
            });
        }
        else
        {
            startActivity(new Intent("com.DRMS.NewMenu"));
        }
    }
});

}
public class Second extends Activity {

    private String pass = new String();
    private String nam = new String();
    private Button log;
    private View textreg;
    private EditText text1;
    private EditText text2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        textreg = (TextView) findViewById(R.id.textRegister);
        log = (Button) findViewById(R.id.btnsubmit);
        text1 = (EditText) findViewById(R.id.txtuname);
        text2 = (EditText) findViewById(R.id.txtpassword);

        // IF NOT REGISTERED USERS//
        // //A textview when clicked takes user to registeration page/////

        textreg.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                startActivity(new Intent("com.DRMS.Register"));
            }
        });

        // //
        // IF PASSWORD AND USERNAME CORRECT, GOTO NewMenu Activity //
        // //

        pass = "12345";
        nam = "12345";

        log.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                if (text1.getText().toString().equals(nam)
                        && text2.getText().toString().equals(pass)) {
                    startActivity(new Intent("com.DRMS.NewMenu"));
                } else {
                    AlertDialog.Builder dlgAlert = new AlertDialog.Builder(
                            getApplicationContext());

                    dlgAlert.setMessage("wrong password or username");
                    dlgAlert.setTitle("Error Message...");
                    dlgAlert.setPositiveButton("OK", null);
                    dlgAlert.setCancelable(true);
                    dlgAlert.create().show();

                    dlgAlert.setPositiveButton("Ok",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int which) {

                                }
                            });
                }
            }
        });

    }

}
您不需要在oncreate方法中检查用户名n pass


希望能有帮助

您应该在onClick方法中包含if/else条件。这将确保它仅在单击按钮后执行。

您应该在onClick方法中包含if/else条件。这将确保它仅在单击按钮后执行。

您的警报对话框代码将在注册的onClick中出现按钮

您的警报对话框代码将在单击注册按钮时进入

您的算法不正常。创建活动时会调用onCreate(),因此每次创建时都会检查用户名和密码。由于您从未设置文本字段,因此它们的值在第一次启动时永远不会正常,因此您总是在第一次启动时显示错误对话框。你应该这样做:

public class Second extends Activity {

    private String pass = new String();
    private String nam = new String();
    private Button log;
    private View textreg;
    private EditText text1;
    private EditText text2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        textreg = (TextView) findViewById(R.id.textRegister);
        log = (Button) findViewById(R.id.btnsubmit);
        text1 = (EditText) findViewById(R.id.txtuname);
        text2 = (EditText) findViewById(R.id.txtpassword);

        // IF NOT REGISTERED USERS//
        // //A textview when clicked takes user to registeration page/////

        textreg.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                startActivity(new Intent("com.DRMS.Register"));
            }
        });

        // //
        // IF PASSWORD AND USERNAME CORRECT, GOTO NewMenu Activity //
        // //

        pass = "12345";
        nam = "12345";

        log.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                if (text1.getText().toString().equals(nam)
                        && text2.getText().toString().equals(pass)) {
                    startActivity(new Intent("com.DRMS.NewMenu"));
                } else {
                    AlertDialog.Builder dlgAlert = new AlertDialog.Builder(
                            getApplicationContext());

                    dlgAlert.setMessage("wrong password or username");
                    dlgAlert.setTitle("Error Message...");
                    dlgAlert.setPositiveButton("OK", null);
                    dlgAlert.setCancelable(true);
                    dlgAlert.create().show();

                    dlgAlert.setPositiveButton("Ok",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int which) {

                                }
                            });
                }
            }
        });

    }

}
protected void onCreate(Bundle savedInstanceState) {

    // 1. Call super() and init fields

    String pass="12345";
    String nam="12345";

    // 2. Set the onClickListener
    textreg.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            startActivity(new Intent("com.DRMS.Register"));
        }
    });

    // 3. Set the onClickListener for the Login button
    log.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            // 4. Check if fields are null
            if(text1.getText() != null && text2.getText() != null) {

                // 4.a check if username and password are OK
                if(text1.getText().toString().equals(nam) && 
                    text2.getText().toString().equals(pass) )
                {    
                    // Not null and OK, launch the activity            
                    startActivity(new Intent("com.DRMS.NewMenu"));
                } else {
                    // Username or password false, display and an error
                    AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(this);

                    dlgAlert.setMessage("wrong password or username");
                    dlgAlert.setTitle("Error Message...");
                    dlgAlert.setPositiveButton("OK", null);
                    dlgAlert.setCancelable(true);
                    dlgAlert.create().show();

                    dlgAlert.setPositiveButton("Ok",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {

                        }
                    });
                }
            } else {
                // The fields are not filled.
                // Display an error message like "Please enter username/password
            }
        }
    });
}

你的算法不正确。创建活动时会调用onCreate(),因此每次创建时都会检查用户名和密码。由于您从未设置文本字段,因此它们的值在第一次启动时永远不会正常,因此您总是在第一次启动时显示错误对话框。你应该这样做:

protected void onCreate(Bundle savedInstanceState) {

    // 1. Call super() and init fields

    String pass="12345";
    String nam="12345";

    // 2. Set the onClickListener
    textreg.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            startActivity(new Intent("com.DRMS.Register"));
        }
    });

    // 3. Set the onClickListener for the Login button
    log.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            // 4. Check if fields are null
            if(text1.getText() != null && text2.getText() != null) {

                // 4.a check if username and password are OK
                if(text1.getText().toString().equals(nam) && 
                    text2.getText().toString().equals(pass) )
                {    
                    // Not null and OK, launch the activity            
                    startActivity(new Intent("com.DRMS.NewMenu"));
                } else {
                    // Username or password false, display and an error
                    AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(this);

                    dlgAlert.setMessage("wrong password or username");
                    dlgAlert.setTitle("Error Message...");
                    dlgAlert.setPositiveButton("OK", null);
                    dlgAlert.setCancelable(true);
                    dlgAlert.create().show();

                    dlgAlert.setPositiveButton("Ok",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {

                        }
                    });
                }
            } else {
                // The fields are not filled.
                // Display an error message like "Please enter username/password
            }
        }
    });
}

dlgAlert.create().show();应在设置正按钮后出现。一定要回答在editdlgAlert.create().show()之后您面临的问题;应在设置正按钮后出现。在编辑之后,一定要回答您面临的问题。您可以添加
dialog.discouse()内部onClick。您可以添加
dialog.disease()内部onClick。