在我的应用程序中,我创建了一个简单的自定义对话框。我必须在片段类中调用它。不知道如何继续,任何人都可以帮助我吗?有没有办法我们可以在片段中使用简单的对话而不用Dialogfragment?
答案 0 :(得分:0)
使用对话框片段将是更好的做法。
Dialog Fragment他们给出了不同的对话片段概念,以满足与对话相关的所有需求。除此之外
片段中的简单警告对话框
AlertDialog ad = new AlertDialog.Builder(getActivity())
.create();
ad.setCancelable(false);
ad.setTitle(title);
ad.setMessage(message);
ad.setButton(context.getString(R.string.ok_text), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
ad.show();
片段中的自定义对话框
final Dialog dialog = new Dialog(getActivity);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Title...");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Android custom dialog example!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
更新1:
在这里,我举一个例子,我曾经使用对话框片段显示日期选择器
public static class SelectDateFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar calendar = Calendar.getInstance();
int yy = calendar.get(Calendar.YEAR);
int mm = calendar.get(Calendar.MONTH);
int dd = calendar.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, yy, mm, dd);
}
@Override
public void onDateSet(DatePicker view, int yy, int mm, int dd) {
dobString = myYear + "-" + myMonth + "-" + myDay;
populateSetDate(yy, mm + 1, dd);
}
@Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
}
}
并显示对话框
public void selectDate(View view) {
DialogFragment newFragment = new SelectDateFragment();
newFragment.show(getSupportFragmentManager(), "DatePicker");
}
将从按钮点击调用..