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