ActionBar 菜单刷新
ActionBar menu refresh
我有一个简单的要求,即在操作栏菜单中有 2 个菜单项。第一个用于使用 google 帐户登录,第二个用于注销。我想根据用户的状态隐藏一个选项。即,如果用户已登录,我想隐藏其选项,当 he/she 已注销时,隐藏注销选项。问题是登录成功后我仍然看到登录选项。但是当我再次点击登录时,一切正常。即我看到注销选项。我还在登录时使用了 invalidateOptionsMenu() 但菜单没有第一次刷新。但注销就像一个魅力。这是我的代码
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
MenuItem login = menu.findItem(R.id.action_login);
MenuItem logout = menu.findItem(R.id.action_logout);
if(account!=null) {
login.setVisible(false);
logout.setVisible(true);
} else {
login.setVisible(true);
logout.setVisible(false);
}
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
Intent intent = new Intent(this, EditRssFeedActivity.class);
startActivity(intent);
return true;
} else if (id == R.id.action_login) {
signIn(mGoogleSignInClient);
greetUser();
invalidateOptionsMenu();
} else if(id == R.id.action_logout) {
mGoogleSignInClient.signOut()
.addOnCompleteListener(this, new OnCompleteListener<Void>() {
@Override
public void onComplete(Task<Void> task) {
Toast.makeText(MainActivity.this,"User logged out",Toast.LENGTH_SHORT).show();
}
});
invalidateOptionsMenu();
}
return super.onOptionsItemSelected(item);
}
private void signIn(GoogleSignInClient mGoogleSignInClient) {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
MenuItem item = findViewById(R.id.action_login);;
if (requestCode == RC_SIGN_IN && item !=null) {
item.setVisible(false);
invalidateOptionsMenu();
greetUser();
}
}
谁能帮我每次触发菜单刷新?
试试这个应该行得通,通过这个我们在登录或注销交互后有 hiding/displaying 个项目...
MenuItem login;
MenuItem logout;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
login = menu.findItem(R.id.action_login);
logout = menu.findItem(R.id.action_logout);
if(account!=null) {
login.setVisible(false);
logout.setVisible(true);
} else {
login.setVisible(true);
logout.setVisible(false);
}
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
Intent intent = new Intent(this, EditRssFeedActivity.class);
startActivity(intent);
return true;
} else if (id == R.id.action_login) {
signIn(mGoogleSignInClient);
greetUser();
logout.setVisible(true);
login.setVisible(false);
} else if(id == R.id.action_logout) {
mGoogleSignInClient.signOut()
.addOnCompleteListener(this, new OnCompleteListener<Void>() {
@Override
public void onComplete(Task<Void> task) {
Toast.makeText(MainActivity.this,"User logged out",Toast.LENGTH_SHORT).show();
}
});
logout.setVisible(false);
login.setVisible(true);
}
return super.onOptionsItemSelected(item);
}
private void signIn(GoogleSignInClient mGoogleSignInClient) {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
MenuItem item = findViewById(R.id.action_login);;
if (requestCode == RC_SIGN_IN && item !=null) {
item.setVisible(false);
invalidateOptionsMenu();
greetUser();
}
}
希望这对您有所帮助
编辑
先调用onCreate方法,在完成之前调用onCreateOptionsMenu。
在带有官方 Honeycomb-style 操作栏的设备和应用程序上也是如此。如果没有操作栏,onCreateOptionsMenu() 不应被调用,直到用户调出菜单,通常是通过按下 MENU 按钮。
我有一个简单的要求,即在操作栏菜单中有 2 个菜单项。第一个用于使用 google 帐户登录,第二个用于注销。我想根据用户的状态隐藏一个选项。即,如果用户已登录,我想隐藏其选项,当 he/she 已注销时,隐藏注销选项。问题是登录成功后我仍然看到登录选项。但是当我再次点击登录时,一切正常。即我看到注销选项。我还在登录时使用了 invalidateOptionsMenu() 但菜单没有第一次刷新。但注销就像一个魅力。这是我的代码
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
MenuItem login = menu.findItem(R.id.action_login);
MenuItem logout = menu.findItem(R.id.action_logout);
if(account!=null) {
login.setVisible(false);
logout.setVisible(true);
} else {
login.setVisible(true);
logout.setVisible(false);
}
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
Intent intent = new Intent(this, EditRssFeedActivity.class);
startActivity(intent);
return true;
} else if (id == R.id.action_login) {
signIn(mGoogleSignInClient);
greetUser();
invalidateOptionsMenu();
} else if(id == R.id.action_logout) {
mGoogleSignInClient.signOut()
.addOnCompleteListener(this, new OnCompleteListener<Void>() {
@Override
public void onComplete(Task<Void> task) {
Toast.makeText(MainActivity.this,"User logged out",Toast.LENGTH_SHORT).show();
}
});
invalidateOptionsMenu();
}
return super.onOptionsItemSelected(item);
}
private void signIn(GoogleSignInClient mGoogleSignInClient) {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
MenuItem item = findViewById(R.id.action_login);;
if (requestCode == RC_SIGN_IN && item !=null) {
item.setVisible(false);
invalidateOptionsMenu();
greetUser();
}
}
谁能帮我每次触发菜单刷新?
试试这个应该行得通,通过这个我们在登录或注销交互后有 hiding/displaying 个项目...
MenuItem login;
MenuItem logout;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
login = menu.findItem(R.id.action_login);
logout = menu.findItem(R.id.action_logout);
if(account!=null) {
login.setVisible(false);
logout.setVisible(true);
} else {
login.setVisible(true);
logout.setVisible(false);
}
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
Intent intent = new Intent(this, EditRssFeedActivity.class);
startActivity(intent);
return true;
} else if (id == R.id.action_login) {
signIn(mGoogleSignInClient);
greetUser();
logout.setVisible(true);
login.setVisible(false);
} else if(id == R.id.action_logout) {
mGoogleSignInClient.signOut()
.addOnCompleteListener(this, new OnCompleteListener<Void>() {
@Override
public void onComplete(Task<Void> task) {
Toast.makeText(MainActivity.this,"User logged out",Toast.LENGTH_SHORT).show();
}
});
logout.setVisible(false);
login.setVisible(true);
}
return super.onOptionsItemSelected(item);
}
private void signIn(GoogleSignInClient mGoogleSignInClient) {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
MenuItem item = findViewById(R.id.action_login);;
if (requestCode == RC_SIGN_IN && item !=null) {
item.setVisible(false);
invalidateOptionsMenu();
greetUser();
}
}
希望这对您有所帮助
编辑
先调用onCreate方法,在完成之前调用onCreateOptionsMenu。
在带有官方 Honeycomb-style 操作栏的设备和应用程序上也是如此。如果没有操作栏,onCreateOptionsMenu() 不应被调用,直到用户调出菜单,通常是通过按下 MENU 按钮。