[英]Want to change action bar icon when click on ok button of AlertDialog in Android Studio
I have been trying to write something so that when an alertDialog popup, I click ok then login icon will be hidden, logout and settings icon will be visible.我一直在尝试写一些东西,以便当 alertDialog 弹出时,我单击确定然后登录图标将被隐藏,注销和设置图标将可见。 But I just can't get it to work.
但我就是无法让它工作。 Hopefully someone can help me.
希望有人可以帮助我。 I have been at it for quite sometime...
我已经有一段时间了......
in MainActivity:在主活动中:
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
// Login button
if (id == R.id.loginButton){
LoginDialog loginDialog = new LoginDialog();
loginDialog.show(getSupportFragmentManager(), "loginDialog");
}
// Logout Button
if (id == R.id.logoutButton){
}
// Setting Button
if (id == R.id.settingsButton){
}
return super.onOptionsItemSelected(item);
}
in LoginDialog class:在 LoginDialog 类中:
public class LoginDialog extends AppCompatDialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.login_dialog, null);
builder.setView(view)
.setTitle("Login")
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
/* I tried
Menu menu = findItem(R.id.loginButton).setVisible(false);
but that's not correct
I tried this here because it works in MainActivity but not here */
}
});
return builder.create();
}
}
Create interface创建接口
interface MyInterface{
public void hideMenuItem();
}
Make Activity implement interface使Activity实现接口
class MainActivity implements MyInterface {
@Override
public void hideMenuItem(){
menu.findItem(R.id.item_to_show).setVisible(false);
}
So your DialogFragment's onClick looks like this所以你的 DialogFragment 的 onClick 看起来像这样
@Override
public void onClick(DialogInterface dialog, int which) {
MyInterface myInterface = (MyInterface) getActivity();
if(myInterface != null){
myInterface.hideMenuItem();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.