gpt4 book ai didi

android - 在 fragment 中显示 DialogFragment

转载 作者:太空狗 更新时间:2023-10-29 16:12:38 25 4
gpt4 key购买 nike

我一直在尝试在 Fragment 中显示 DialogFragment 但没有成功。这是我的代码:

@Override
public void onResume() {
super.onResume();
Log.d(TAG, "onResume");
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null && !user.isEmailVerified()) {
InfoDialogFragment dialog = new InfoDialogFragment();
dialog.show(getActivity().getSupportFragmentManager(), "InfoDialogFragment");
}
}

每当该 fragment 出现在屏幕上时,屏幕就会变暗,当我触摸屏幕时它会恢复正常对比度,但我看不到对话框 fragment 。我错过了什么?

这是 DialogFragment 的代码:

public class InfoDialogFragment extends DialogFragment {
private InfoDialogListener listener;

@Override
public void onAttach(Context activity) {
super.onAttach(activity);
// Verify that the host activity implements the callback interface
try {
// Instantiate the InfoDialogListener so we can send events to the host
listener = (InfoDialogListener) activity;
} catch (ClassCastException e) {
// The activity doesn't implement the interface, throw exception
throw new ClassCastException(activity.toString()
+ " must implement DeleteListListener");
}
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
View layout = inflater.inflate(R.layout.fragment_info, null);

TextView message = (TextView) layout.findViewById(R.id.message);
message.setText(getActivity().getString(R.string.user_not_verified));
//
Button ok = (Button) layout.findViewById(R.id.ok_button);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onDialogOKClick(InfoDialogFragment.this);
}
});
return builder.create();
}


public interface InfoDialogListener {
void onDialogOKClick(DialogFragment dialog);

}

谢谢

最佳答案

使用 getChildFragmentManager 而不是 getActivity().getSupportFragmentManager()。这些是不同的实例。

关于android - 在 fragment 中显示 DialogFragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41261120/

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