Android项目开发
对话框的显示
对话框是AlertDialog的一个实例,用户点击日期按钮会弹出对话框。
- 在项目中添加android.support.v7.AlertDialog依赖包。
- 创建DatePickerFragment类;
public class DatePickerFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle saveInstanceState){
return new AlertDialog.Builder(getActivity())
.setView(v)
.setTitle(R.string.date_picker_title)
.setPositiveButton(android.R.string.ok,
.create();
}
- 传入FragmentManager参数,系统会自动创建并提交事务。
private static final String DIALOG_DATE = "DialogDate";
- 设置对话框显示内容,创建AlertDialog
public Dialog onCreateDialog(Bundle saveInstanceState){
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);
View v = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_date,null);
mDatePicker = (DatePicker) v.findViewById(R.id.dialog_date_picker);
mDatePicker.init(year,month,day,null);
return new AlertDialog.Builder(getActivity())
.setView(v)
.setTitle(R.string.date_picker_title)
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int year = mDatePicker.getYear();
int month = mDatePicker.getMonth();
int day = mDatePicker.getDayOfMonth();
Date date = new GregorianCalendar(year,month,day).getTime();
sendResult(Activity.RESULT_OK,date);
}
})
.create();
}
fragment间的数据传递
- 传递crime的日期给Fragment,需新建一个newInstance(Date)方法,然后将Date作为argument附件给Fragment。
为了返回新的日期给Fragment并更新模型层以及对应的视图,需要将日期打包为extra并附加到Intent上,然后调用Fragment.onActivityResult(...)的方法,并传入准备好的Intent参数。
private static final String ARG_DATE = "date";
private DatePicker mDatePicker;
public static DatePickerFragment newInstance(Date date) {
Bundle args = new Bundle();
args.putSerializable(ARG_DATE, date);
DatePickerFragment fragment = new DatePickerFragment();
fragment.setArguments(args);
return fragment;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) { //创建了AlertDialog的实例
//CrimeFragment向DatePickerFragment传递日期
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);
- 在onCreateView方法中创建并配置fragment视图,其间设置按钮的监听事件,设置目标Fragment,传递数据给目标Fragment,响应对话框
public class MainFragment extends Fragment {
private Crime mCrime;
private Button mDateButton;
private static final String DIALOG_DATE="DialogDate";
private static final int REQUEST_DATE=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mCrime=new Crime();
}
//创建和配置fragment视图
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,
Bundle savedInstanceState) {
//加载布局
View v=inflater.inflate(R.layout.fragment_main,container,false);
//生成并使用组件
mDateButton=(Button)v.findViewById(R.id.crime_data);
mDateButton.setText(mCrime.getDate().toString());
//mDateButton.setEnabled(false);
mDateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager manager=getFragmentManager();
//newInstance替换构造方法
//DatePickerFragment dialog=new DatePickerFragment();
DatePickerFragment dialog=DatePickerFragment.newInstance(mCrime.getDate());
//设置目标fragment
dialog.setTargetFragment(MainFragment.this,REQUEST_DATE);
dialog.show(manager,DIALOG_DATE); //显示DialogFragment
}
});
return v;
}
//响应DatePicker对话框
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode!= Activity.RESULT_OK){
return;
}
if (requestCode==REQUEST_DATE){
Date date=(Date) data.getSerializableExtra(DatePickerFragment.EXTRA_DATE);
mCrime.setDate(date);
mDateButton.setText(mCrime.getDate().toString());
}
super.onActivityResult(requestCode, resultCode, data);
}
}