今天的任务为设置对话框的显示内容以及fragment间的数据传递,代码如下:
dialog_date.xml:
<?xml version="1.0" encoding="utf-8"?> <DatePicker xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/dialog_date_picker" android:layout_width="wrap_content" android:layout_height="wrap_content" android:calendarViewShown="false"> </DatePicker>
DatePickerFragment.java:
package com.bignerdranch.android.criminallntent; import android.app.Dialog; import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.support.v7.app.AlertDialog; import android.view.LayoutInflater; import android.view.View; import android.widget.DatePicker; import java.util.Calendar; import java.util.Date; public class DatePickerFragment extends DialogFragment { 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){ 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,null) .create(); } }
CrimeFragment.java:
package com.bignerdranch.android.criminallntent; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.text.Editable; import android.text.TextWatcher; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.EditText; import java.util.UUID; public class CrimeFragment extends Fragment{ private static final String ARG_CRIME_ID="crime_id"; private static final String DIALOG_DATE="DialogDate"; private Crime mCrime; private EditText mTitleField; private Button mDateButton; private CheckBox mSolvedCheckBox; public static CrimeFragment newInstance(UUID crimeId){ Bundle args=new Bundle(); args.putSerializable(ARG_CRIME_ID,crimeId); CrimeFragment fragment=new CrimeFragment(); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); UUID crimeId=(UUID)getArguments().getSerializable(ARG_CRIME_ID); //mCrime=new Crime(); //UUID crimeId=(UUID) getActivity().getIntent().getSerializableExtra(CrimeActivity.EXTRA_CRIME_ID); mCrime=CrimeLab.get(getActivity()).getCrime(crimeId); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState){ View v=inflater.inflate(R.layout.fragment_crime,container,false); mTitleField=(EditText)v.findViewById(R.id.crime_title); mTitleField.setText(mCrime.getTitle()); mTitleField.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { //This space intentionally left bank } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { mCrime.setTitle(s.toString()); } @Override public void afterTextChanged(Editable s) { //This one too } }); mDateButton=(Button)v.findViewById(R.id.crime_date); mDateButton.setText(mCrime.getDate().toString()); //mDateButton.setEnabled(false); mDateButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { FragmentManager manager=getFragmentManager(); //DatePickerFragment dialog=new DatePickerFragment(); DatePickerFragment dialog=DatePickerFragment.newInstance(mCrime.getDate()); dialog.show(manager,DIALOG_DATE); } }); mSolvedCheckBox=(CheckBox)v.findViewById(R.id.crime_solved); mSolvedCheckBox.setChecked(mCrime.isSolved()); mSolvedCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { mCrime.setSolved(isChecked); } }); return v; } }
截图如下: