0

在我的登录活动中,我在选项菜单“登录”和“更改用户”上创建了 2 个按钮。如果用户单击更改用户,则数据将从手机中删除。我显示一个对话框,要求用户确认此操作。

在用户注销一次然后不在用户名和密码文本视图中输入任何凭据之前,它似乎工作正常。如果当用户单击登录按钮时这些为空,则会显示一条 toast 消息,显示“请输入有效名称”,但还会显示对话框询问用户是否要更改用户。

我的问题是,当此代码不在选项菜单中的登录案例中时,为什么会显示要求用户确认他们想要更改用户的对话框?这就像当用户单击登录时使用空字段然后更改用户代码执行。在此先感谢马特。

@Override
    public boolean onOptionsItemSelected(MenuItem item) {

        Cursor allFromCompIdTable = nfcscannerapplication.loginValidate.queryAllFromCompanyIdTable();

        if(allFromCompIdTable.getCount() > 0){

            if(allFromCompIdTable.moveToLast()){

                compId = allFromCompIdTable.getString(allFromCompIdTable
                        .getColumnIndex(LoginValidate.C_COMPANY_ID_OUTSIDE_APP_PURPOSES));

            }

        } 

        if(isAllowChangeUser.equalsIgnoreCase("false")){
            if(item.getItemId() == R.id.changeuser)
            item.setVisible(false);
            }

       switch (item.getItemId()) {


        case R.id.login:




            Log.e(TAG, "login clicked from opts menu");



                theUsername = userName.getText().toString();

                if(theUsername.trim().equalsIgnoreCase("")){

                    Toast.makeText(
                            EntryActivity.this,
                            "Please enter a valid name.",
                            Toast.LENGTH_LONG).show();
                    onCreate(sIs);

                }else{

                thePassword = passwordPin.getText().toString();
                String loginType = "1";

                if(isSavePassword.trim().equalsIgnoreCase("false")){

                    if( ! thePassword.trim().equalsIgnoreCase("")){
                String[] params = new String[]{compId, theUsername, thePassword, loginType}; 

                //validate user Asynchonously on background thread
                AsyncValidateCarer avc = new AsyncValidateCarer();
                avc.execute(params);
                    }else{
                        Toast.makeText(
                                EntryActivity.this,
                                "Please enter a valid password.",
                                Toast.LENGTH_LONG).show();
                    }
                }else if(isSavePassword.trim().equalsIgnoreCase("true")){

                     Cursor c = nfcscannerapplication.loginValidate.queryAllFromCarer();
                     String firstNameFromDB = null; 
                     String passwordFromDB = null;
                     String carerIdFromDB = null;

                    if(c != null && c.getCount() > 0){

                    c.moveToLast();
                    firstNameFromDB = c.getString(c.getColumnIndex(LoginValidate.C_CARER_FIRSTNAME));
                    passwordFromDB = c.getString(c.getColumnIndex(LoginValidate.C_PASSWORD));
                    carerIdFromDB = c.getString(c.getColumnIndex(LoginValidate.C_CARER_ID));
                    carerID = carerIdFromDB;

                    }else{
                        String[] params = new String[]{compId, theUsername, thePassword, loginType}; 

                        //validate user Asynchonously on background thread
                        AsyncValidateCarer avc = new AsyncValidateCarer();
                        avc.execute(params);
                    }

                    if(theUsername.trim().equalsIgnoreCase(firstNameFromDB) && 
                                                     thePassword.trim().equalsIgnoreCase(passwordFromDB)){

                    Intent intent2 = new Intent(EntryActivity.this,
                            NfcscannerActivity.class);
                    intent2.setAction(CUSTOM_QRCODE_ACTION);
                    intent2.putExtra("carerid", carerID);
                    startActivity(intent2);

                    }
                }


            return true;                    

                }



        case R.id.changeuser:


AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

            alertDialogBuilder.setMessage("Are you sure you want to change user? All call history and messages will be deleted.")
            .setCancelable(false)
            .setPositiveButton("OK",
            new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int id){

                Log.e(TAG, "change user button clicked");
                nfcscannerapplication.loginValidate.deleteTableCarer();
                nfcscannerapplication.loginValidate.deleteTableTransactions();
                Toast.makeText(
                        EntryActivity.this,
                        "Carer logged out",
                        Toast.LENGTH_LONG).show();
                EntryActivity.this.onCreate(sIs);

            }
            });
            alertDialogBuilder.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int id){



            }
            });
            AlertDialog alert = alertDialogBuilder.create();
            alert.show();


            return true;



        default:

            return super.onOptionsItemSelected(item);
        }
    }
4

2 回答 2

2

添加一个

break;

在开关的每个项目的末尾。

于 2013-07-24T08:24:02.763 回答
0

在您的 switch 语句案例中发生了失败。

一切都很好,直到theUsername.trim().equalsIgnoreCase("")计算到true。执行进入 if 块,绕过 return 语句,继续case R.id.changeuser:

case R.id.login:

        Log.e(TAG, "login clicked from opts menu");

            theUsername = userName.getText().toString();

            if(theUsername.trim().equalsIgnoreCase("")){
                 Toast.makeText(
                        EntryActivity.this,
                        "Please enter a valid name.",
                        Toast.LENGTH_LONG).show();
                onCreate(sIs);

            }else{
于 2013-07-24T08:29:32.360 回答