gpt4 book ai didi

java - 想要在Android Studio中单击AlertDialog的确定按钮时更改操作栏图标

转载 作者:行者123 更新时间:2023-12-01 18:01:55 25 4
gpt4 key购买 nike

我一直在尝试写一些东西,以便当弹出警报对话框时,我单击“确定”,然后登录图标将被隐藏,注销和设置图标将可见。但我就是无法让它发挥作用。希望有人能帮助我。我已经从事这个工作有一段时间了...

在主 Activity 中:

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);
}

在 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();
}
}

最佳答案

创建界面

interface MyInterface{
public void hideMenuItem();
}

让Activity实现接口(interface)

class MainActivity implements MyInterface {

@Override
public void hideMenuItem(){
menu.findItem(R.id.item_to_show).setVisible(false);
}

所以你的 DialogFragment 的 onClick 看起来像这样

@Override
public void onClick(DialogInterface dialog, int which) {
MyInterface myInterface = (MyInterface) getActivity();
if(myInterface != null){
myInterface.hideMenuItem();
}
}

关于java - 想要在Android Studio中单击AlertDialog的确定按钮时更改操作栏图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60613541/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com