3 回答
TA贡献1772条经验 获得超5个赞
完成类似的操作后,您的对话框类应该扩展 Dialog 而不是 Fragment。它应该是:public class GenerateDialog extends Dialog
然后在构造函数上膨胀它:
public class GenerateDialog extends Dialog {
public GenerateDialog(@NonNull Context context) {
super(context, android.R.style.Theme_NoTitleBar_Fullscreen);
setContentView(R.layout.dialog_layout);
}
…
}
在您的 FAB 侦听器上,在您启动对象后,您可以调用 show()(如果您的对象名为 generateDialog,则generateDialog.show();在实例化它后执行,因为它是显示对话框所必需的)。
菲:
public class MainActivity extends AppCompatActivity {
...
GenerateDialog generateDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
generateDialog = new GenerateDialog(this);
floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
generateDialog.show();
}
});
}
}
TA贡献1872条经验 获得超3个赞
你可以像这样显示你的对话框
public class CustomDialog extends Activity {
private static Context context;
private static Dialog dialog;
public static void dialog(final Context ctxt, final String calledBy,
String title, String message, String buttonText) {
context = ctxt;
if( dialog != null && dialog.isShowing() ) return;
dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawableResource(R.color.transparent);
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
dialog.setContentView(R.layout.custom_dialog_single_button);
TextView txtTitle = dialog.findViewById(R.id.txtTitle);
TextView txtMessage = dialog.findViewById(R.id.txtMessage);
TextView tvOK = dialog.findViewById(R.id.tvOK);
txtTitle.setText(title);
txtMessage.setText(message);
tvOK.setText(buttonText);
dialog.show();
}
像这样称呼它
CustomDialogSingleButton.dialog(context,
Constants.SYNC_FILES
, context.getResources().getString(R.string.sync_files_title),
context.getResources().getString(R.string.sync_files_msg), "OK");
只需尝试此代码,您的对话框就会出现
TA贡献1829条经验 获得超4个赞
对话框片段的代码
public class MyCustomDialog extends DialogFragment{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(android.app.DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.dialog_view, container, false);
return v;
}
}
并从您的活动中调用此 MyCustomDialog ,如下所示,
MyCustomDialog dialog = new MyCustomDialog();
Bundle args = new Bundle();
FragmentTransaction ft = getFragmentManager().beginTransaction();
dialog.show(ft, MyCustomDialog.TAG);
添加回答
举报