我的问题是,我的首选项 Activity 从 AppCompatPreferenceActivity
扩展,并且我需要显示 DialogFragment
。所以当我执行以下操作时:
DialogFragmente dialogFragment= new MyDialogFragmentClass()
DialogFragment.show(getSupportFragmentManager(),"My Dialog")
我收到错误,因为我无法使用 getSupportFragmentManager()
方法。所以我的问题是,如果我的类从 AppCompatPreferenceActivity 扩展,我该如何使用该方法。
我尝试使用 getFragmentManager()
代替,但它也不起作用。
这是我的类(class):
public class Settings_acticity extends AppCompatPreferenceActivity {
private SwitchPreference banner_switch;
private SharedPreferences sharedPreferences;
private ListPreference interstial_ad;
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
DialogFragment dialogFragment = new MyDialog();
dialogFragment.show(getSupportFragmentManager(), "My dialog");
}
}
以及 DialogFragment 类:
public class MyDialog extends DialogFragment {
public MyDialog(){
// Required empty public constructor
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View vista=getActivity().getLayoutInflater().inflate(R.layout.fragment_MyDialog,null);
setCancelable(false);
builder.setView(vista).setPositiveButton(R.string.aceptar, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//Yes Code
}
}).setNegativeButton(R.string.cancelar, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//No code
}
});
return builder.create();
}
}
感谢您的帮助!
最佳答案
这可能是因为您使用支持库中的 DialogFragment
。
尝试通过导入 android.app.DialogFragment
而不是 android.support.v4.app.DialogFragment
来使用原始版本
然后您可以显示对话框:
MyDialog myDialog = new MyDialog();
myDialog.show(getFragmentManager(), "My Dialog");
关于java - 从 AppCompatPreferenceActivity 扩展的类中的 DialogFragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47409907/