Android创建对话框的方法有以下几种:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("标题")
.setMessage("消息")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// 确定按钮的点击事件
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// 取消按钮的点击事件
}
});
AlertDialog dialog = builder.create();
dialog.show();
public class MyDialogFragment extends DialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("标题")
.setMessage("消息")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// 确定按钮的点击事件
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// 取消按钮的点击事件
}
});
return builder.create();
}
}
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popupView = inflater.inflate(R.layout.popup_layout, null);
PopupWindow popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.showAtLocation(parentView, Gravity.CENTER, 0, 0);
public class MyCustomDialog extends Dialog {
public MyCustomDialog(@NonNull Context context) {
super(context);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_layout);
// 设置对话框的其他属性和交互逻辑
}
}