Android程式設計權威指南(第二版)— 第13章挑戰練習-13.6

fulinux發表於2017-10-09

程式碼所屬的位置是:https://gitee.com/fulinux/criminalIntent

commit f12acd8c13420f8e5020510b3fd1567da5f5696f

Author: fulinux <fulinux@sina.com>
Date:   Mon Oct 9 16:24:15 2017 +0800

    完成13.6的挑戰練習:刪除crime記錄

diff --git a/app/src/main/java/com/example/android/criminalintent/CrimeFragment.java b/app/src/main/java/com/example/android/criminalintent/CrimeFragment.java
index e62059e..0052186 100644
--- a/app/src/main/java/com/example/android/criminalintent/CrimeFragment.java
+++ b/app/src/main/java/com/example/android/criminalintent/CrimeFragment.java
@@ -10,6 +10,9 @@ import android.text.Editable;
 import android.text.TextWatcher;
 import android.text.format.DateFormat;
 import android.view.LayoutInflater;
+import android.view.Menu;
+import android.view.MenuInflater;
+import android.view.MenuItem;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.Button;
@@ -67,6 +70,7 @@ public class CrimeFragment extends Fragment {
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
+        setHasOptionsMenu(true);
 //        mCrime = new Crime();
 //        UUID crimeId = (UUID) getActivity().getIntent()
 //                .getSerializableExtra(CrimePagerActivity.EXTRA_CRIME_ID);
@@ -157,6 +161,28 @@ public class CrimeFragment extends Fragment {
         }
     }

+    @Override
+    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
+        super.onCreateOptionsMenu(menu, inflater);
+        inflater.inflate(R.menu.fragment_crime_page, menu);
+    }
+
+    @Override
+    public boolean onOptionsItemSelected(MenuItem item) {
+        switch (item.getItemId()) {
+            case R.id.menu_item_del_crime:
+                CrimeLab.get(getActivity()).delCrime(mCrime);
+                if(getActivity().getSupportFragmentManager().getBackStackEntryCount() <= 0) {
+                    getActivity().finish();
+                } else {
+                    getActivity().getSupportFragmentManager().popBackStack();
+                }
+                return true;
+            default:
+                return super.onOptionsItemSelected(item);
+        }
+    }
+
     private void updateTime() {
         mTimeButton.setText(DateFormat.format("kk:mm:ss", mCrime.getDate()).toString());
     }
diff --git a/app/src/main/java/com/example/android/criminalintent/CrimeLab.java b/app/src/main/java/com/example/android/criminalintent/CrimeLab.java
index 6c76c88..dc35451 100644
--- a/app/src/main/java/com/example/android/criminalintent/CrimeLab.java
+++ b/app/src/main/java/com/example/android/criminalintent/CrimeLab.java
@@ -36,6 +36,10 @@ public class CrimeLab {
         mCrimes.add(c);
     }

+    public void delCrime(Crime c) {
+        mCrimes.remove(c);
+    }
+
     public List<Crime> getCrimes() {
         return mCrimes;
     }
diff --git a/app/src/main/res/drawable-hdpi/ic_menu_del.png b/app/src/main/res/drawable-hdpi/ic_menu_del.png
new file mode 100644
index 0000000..cfe3b8e
Binary files /dev/null and b/app/src/main/res/drawable-hdpi/ic_menu_del.png differ
diff --git a/app/src/main/res/drawable-mdpi/ic_menu_del.png b/app/src/main/res/drawable-mdpi/ic_menu_del.png
new file mode 100644
index 0000000..3f22013
Binary files /dev/null and b/app/src/main/res/drawable-mdpi/ic_menu_del.png differ
diff --git a/app/src/main/res/drawable-xhdpi/ic_menu_del.png b/app/src/main/res/drawable-xhdpi/ic_menu_del.png
new file mode 100644
index 0000000..10ac02d
Binary files /dev/null and b/app/src/main/res/drawable-xhdpi/ic_menu_del.png differ
diff --git a/app/src/main/res/drawable-xxhdpi/ic_menu_del.png b/app/src/main/res/drawable-xxhdpi/ic_menu_del.png
new file mode 100644
index 0000000..3f7e36b
Binary files /dev/null and b/app/src/main/res/drawable-xxhdpi/ic_menu_del.png differ
diff --git a/app/src/main/res/menu/fragment_crime_page.xml b/app/src/main/res/menu/fragment_crime_page.xml
index fe187c0..0ec6083 100644
--- a/app/src/main/res/menu/fragment_crime_page.xml
+++ b/app/src/main/res/menu/fragment_crime_page.xml
@@ -1,4 +1,11 @@
 <?xml version="1.0" encoding="utf-8"?>
-<menu xmlns:android="http://schemas.android.com/apk/res/android">
+<menu xmlns:android="http://schemas.android.com/apk/res/android"
+      xmlns:app="http://schemas.android.com/apk/res-auto">
+
+    <item
+        android:id="@+id/menu_item_del_crime"
+        android:title="Delete Crime"
+        android:icon="@drawable/ic_menu_del"
+        app:showAsAction="ifRoom"/>

 </menu>
\ No newline at end of file

相關文章