optionsmenu两个案例都执行

时间:2013-07-24 08:20:50

标签: android android-optionsmenu

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

似乎工作正常,直到用户退出一次,然后在用户名和密码textviews中没有输入任何凭据。如果用户单击登录按钮时这些为空,则显示“请输入有效名称”的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);
        }
    }

2 个答案:

答案 0 :(得分:2)

添加

break;

在交换机的每个项目的末尾。

答案 1 :(得分:0)

在switch语句中发生了一个漏报。

theUsername.trim().equalsIgnoreCase("")计算到true之前一切正常。执行进入if-block,绕过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{