1.升级Crime类
package com.criminalintent; import java.util.UUID; import java.util.Date; public class Crime { private UUID uuid; private String title; //新增date solved 两个字段 private Date date; private boolean solved; public Crime(){ uuid=UUID.randomUUID(); date=new Date(); //新增date } public UUID getUuid() { return uuid; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } //以下是新增加的 public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } public boolean isSolved() { return solved; } public void setSolved(boolean solved) { this.solved = solved; } }
2.更新fragment_crime.xml 视图
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/crime_title_label" style="?android:listSeparatorTextViewStyle"/> <EditText android:id="@+id/crime_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" android:hint="@string/crime_title_hint"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/crime_details_label" style="?android:listSeparatorTextViewStyle"/> <Button android:id="@+id/crime_date" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="16dp" android:layout_marginRight="16dp"/> <CheckBox android:id="@+id/crime_solved" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" android:text="@string/crime_solved_label"/> </LinearLayout>
3.更新CrimeFragment.java类文件
package com.criminalintent; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; 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.TextView; public class CrimeFragment extends Fragment { private Crime crime; private TextView titleFiled; //新增 dateButton solvedCheckBox字段 private Button dateButton; private CheckBox solvedCheckBox; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); crime=new Crime(); } @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view=inflater.inflate(R.layout.fragment_crime,container,false); titleFiled= (TextView) view.findViewById(R.id.crime_title); titleFiled.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { crime.setTitle(charSequence.toString()); } @Override public void afterTextChanged(Editable editable) { } }); //新增控制button checkbox 的代码 dateButton= (Button) view.findViewById(R.id.crime_date); dateButton.setText(crime.getDate().toString()); dateButton.setEnabled(false); solvedCheckBox= (CheckBox) view.findViewById(R.id.crime_solved); solvedCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean isSolved) { crime.setSolved(isSolved); } }); return view; } }
最后运行效果图如下: