此处基于支持库,所以要添加支持库
(虽然安卓框架本身有,但用支持库更好)
例程:(基于fragment,包括了自定义Dialog布局,Dialog和fragment间的数据双向传递)
1.1 新建布局XMl(以DatePicker组件为例)
<?xml version="1.0" encoding="utf-8"?>
<DatePicker xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dialog_datePicker"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:calendarViewShown="false">
</DatePicker>
新建dialog类:
导入AlertDialog时,请确认选择的是AppCompat库中的版本:android.support.v7.app.AlertDialog
public class DialogFragment extends AppCompatDialogFragment{
//生成Dialog的方法
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
}
}
1.2 新建dialog类onCreateDialog()方法中
//绑定布局对象
View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_date_picker,null);
1.3 新建dialog类中
//标志私有空间标志
private static final String ARG_DATE = "date";
//标志私有空间标志
public static final String EXTRA_DATE =
"com.bignerdranch.android.criminalintent.date";
//DatePicker组件对象
private DatePicker yDatePicker;
1.4 新建dialog类中
//此是自定义方法
//该方法将新建dialog,并将数据接受到其私有空间,用于fragment向dialog传递数据
public static DialogFragment newInstance(Date date) {
Bundle args = new Bundle();
args.putSerializable(ARG_DATE, date);
DialogFragment fragment = new DialogFragment();
fragment.setArguments(args);
return fragment;
}
//自定义方法
//目的是生成Intent,并将数据传递给该Intent的私有空间,用于dialog向fragment传递数据
private void sendResult(int resultCode, Date date) {
if (getTargetFragment() == null) {
return;
}
Intent intent = new Intent();
intent.putExtra(EXTRA_DATE, date);
getTargetFragment()
.onActivityResult(getTargetRequestCode(), resultCode, intent);
}
1.5 新建dialog类onCreateDialog()方法中
//捕获时间的字符数据,并分离出来
Date date = (Date) getArguments().getSerializable(ARG_DATE);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
//用于获取DatePicker资源并设置
yDatePicker = (DatePicker) view.findViewById(R.id.dialog_datePicker);
yDatePicker.init(year, month, day, null);
1.6 新建dialog类onCreateDialog()方法中
//设置Dialog对象,创建并返回
return new android.support.v7.app.AlertDialog.Builder(getActivity())
.setView(view)//绑定视图
.setTitle(R.string.date_picker_title)//设置标题文本
//按钮事件类型和监听反应,匿名内建类
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int year = yDatePicker.getYear();
int month = yDatePicker.getMonth();
int day = yDatePicker.getDayOfMonth();
Date date = new GregorianCalendar(year, month, day).getTime();
sendResult(Activity.RESULT_OK, date);//调用自定义方法生成意向并传递数据
}
})//设置按钮方式
.create();
1.7 fragment类里
//用于标记关联设置dialog,绑定dialog和fragment
private static final String DIALOG_DATE = "DialogDate";
//用于判断dialog是否有返回值,0为有
private static final int REQUEST_DATE = 0;
1.8 fragment类相关的事件函数里,用于生成dialog
//此是DialogFragment里的自定义方法
//该方法将新建dialog,并将数据传递到其私有空间
DialogFragment dialog = DialogFragment.newInstance(yCrime.getyDate());
//关联设置dialog,绑定dialog和fragment
dialog.setTargetFragment(CrimeFragment.this, REQUEST_DATE);
dialog.show(manager, DIALOG_DATE);
1.9 fragment类里
//接受数据并设置
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != Activity.RESULT_OK) {
return;
}
if (requestCode == REQUEST_DATE) {
//从Dialog的私有空间获取数据
Date date = (Date) data.getSerializableExtra(DialogFragment.EXTRA_DATE);
yCrime.setyDate(date);//更新模型里数据类相应的数据
DateButton.setText(yCrime.getyDate().toString());
}
}