-1

我的应用程序中有以下代码。有一个登录按钮在我看来可以正常工作。我已经覆盖了视图上的选项菜单,并将登录代码放在附加到选项菜单的侦听器中。当我按下选项菜单登录按钮时,第一次点击没有任何反应,但在随后的点击中一切正常。为什么是这样?

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.layout.menuentryoptionsmenu, menu);
        return true;

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {


        case R.id.login:
                        item.setOnMenuItemClickListener(new OnMenuItemClickListener(){

                            @Override
                            public boolean onMenuItemClick(MenuItem item) {
                                // TODO Auto-generated method stub
                                Log.e(TAG, "login clicked from opts menu");
                                 compId = "100";
                                    String theUsername = userName.getText().toString();
                                    thePassword = passwordPin.getText().toString();
                                    String loginType = "1";



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

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


            return true;


        case R.id.changeuser:
            if(isAllowChangeUser.equalsIgnoreCase("false")){
            item.setVisible(false);
            }
            return true;



        default:

            return super.onOptionsItemSelected(item);
        }
    }
4

2 回答 2

3

移除点击监听器。只需将整个登录代码放在case R.id.login:.

那是因为 onOptionsItemSelected 已经是点击了,不用再创建和设置了。

于 2012-10-16T08:36:22.717 回答
1

似乎第一次单击选项菜单项时,您只是添加了 OnMenuItemClickListener 侦听器。第二次单击时,您将触发该侦听器。

Try removing the onMenuItemClickListener code and put the code in your onMenuItemClick function into the switch statement directly.

于 2012-10-16T08:37:57.437 回答