XCBOSA 3 жил өмнө
parent
commit
b7cf7f0237

+ 15 - 5
src/main/java/com/example/mywordbook/MainActivity.java

@@ -22,7 +22,7 @@ import com.google.android.material.floatingactionbutton.FloatingActionButton;
 public class MainActivity extends AppCompatActivity implements WordCardDelegate, TextWatcher {
 
     private RecyclerView wordListView;
-    private FloatingActionButton plusButton;
+    private FloatingActionButton plusButton, helpButton;
     private EditText findInput;
     private DBHelper db;
     private DBAdapter wordAdapter;
@@ -42,6 +42,16 @@ public class MainActivity extends AppCompatActivity implements WordCardDelegate,
         wordListView.setLayoutManager(new LinearLayoutManager(this));
         wordListView.addItemDecoration(new SpacesItemDecoration(dip2px(getBaseContext(), 20)));
         plusButton.setOnClickListener(view -> plusButtonClicked());
+        helpButton = findViewById(R.id.helpButton);
+        helpButton.setOnClickListener(view -> {
+            TextView text = new TextView(this);
+            text.setText("这是帮助");
+            new AlertDialog.Builder(this)
+                    .setTitle("帮助")
+                    .setView(text)
+                    .setPositiveButton("好", null)
+                    .show();
+        });
     }
 
     private int dip2px(Context context, float dpValue) {
@@ -53,7 +63,7 @@ public class MainActivity extends AppCompatActivity implements WordCardDelegate,
         void finish();
     }
 
-    private void showDialog(PreparedWord word, FinishCallBack finishCallBack) {
+    private void showDialog(PreparedWord word, String title, FinishCallBack finishCallBack) {
         View view = LayoutInflater.from(this).inflate(R.layout.dialog_add_word, null, false);
         EditText wordName = view.findViewById(R.id.addWordName),
                 wordTranslate = view.findViewById(R.id.addWordTranslate),
@@ -62,7 +72,7 @@ public class MainActivity extends AppCompatActivity implements WordCardDelegate,
         wordTranslate.setText(word.getTranslate());
         wordExample.setText(word.getExample());
         new AlertDialog.Builder(this)
-                .setTitle("新建单词")
+                .setTitle(title)
                 .setView(view)
                 .setPositiveButton("确定", (dialogInterface, i) -> {
                     word.setWord(wordName.getText().toString());
@@ -76,7 +86,7 @@ public class MainActivity extends AppCompatActivity implements WordCardDelegate,
 
     private void plusButtonClicked() {
         PreparedWord word = new PreparedWord();
-        showDialog(word, () -> {
+        showDialog(word, "新建单词", () -> {
             db.addWord(word);
             this.wordAdapter.updateWordList();
             this.wordAdapter.notifyDataSetChanged();
@@ -98,7 +108,7 @@ public class MainActivity extends AppCompatActivity implements WordCardDelegate,
 
     @Override
     public void onEdit(Word word, int position) {
-        showDialog(word, () -> {
+        showDialog(word, "修改单词", () -> {
             db.editWord(word.getId(), word);
             this.wordAdapter.updateWordList();
             this.wordAdapter.notifyItemChanged(position);

+ 17 - 0
src/main/java/com/example/mywordbook/WordCardPrototype.java

@@ -1,9 +1,13 @@
 package com.example.mywordbook;
 
+import android.graphics.Color;
+import android.os.Build;
 import android.view.View;
 import android.widget.Button;
+import android.widget.LinearLayout;
 import android.widget.TextView;
 import androidx.annotation.NonNull;
+import androidx.annotation.RequiresApi;
 import androidx.recyclerview.widget.RecyclerView;
 import com.example.mywordbook.db.Word;
 import com.google.android.material.floatingactionbutton.FloatingActionButton;
@@ -15,6 +19,7 @@ public class WordCardPrototype extends RecyclerView.ViewHolder {
 
     private TextView name, translate, example;
     private Button btnDelete, btnEdit;
+    private LinearLayout prototypeLayout;
 
     public WordCardPrototype(@NonNull View itemView) {
         super(itemView);
@@ -25,6 +30,16 @@ public class WordCardPrototype extends RecyclerView.ViewHolder {
         btnDelete.setOnClickListener(view -> delegate.onDelete(word, getAdapterPosition()));
         btnEdit = itemView.findViewById(R.id.btnEdit);
         btnEdit.setOnClickListener(view -> delegate.onEdit(word, getAdapterPosition()));
+        prototypeLayout = itemView.findViewById(R.id.prototypeLayout);
+    }
+
+    private int calculateColor() {
+        int hashR = word.getWord().hashCode(), hashG = hashR >> 3 % 97, hashB = (hashR + hashG) << 3 % 111;
+        hashR = Math.abs(hashR);
+        hashG = Math.abs(hashG);
+        hashB = Math.abs(hashB);
+        int r = hashR % 96, g = hashG % 96, b = hashB % 96;
+        return Color.rgb(r + 128, g + 128, b + 128);
     }
 
     public void updateWord(Word word) {
@@ -32,6 +47,8 @@ public class WordCardPrototype extends RecyclerView.ViewHolder {
         name.setText(word.getWord());
         translate.setText(word.getTranslate());
         example.setText(word.getExample());
+
+        prototypeLayout.setBackgroundColor(calculateColor());
     }
 
 }

+ 13 - 1
src/main/res/layout/activity_main.xml

@@ -15,7 +15,6 @@
         android:inputType="textPersonName"
         android:text="Name"
         app:layout_constraintEnd_toStartOf="@+id/wordListView"
-        app:layout_constraintStart_toStartOf="parent"
         app:layout_constraintTop_toTopOf="parent" />
 
     <androidx.recyclerview.widget.RecyclerView
@@ -34,4 +33,17 @@
             app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toBottomOf="parent"
             android:layout_marginBottom="20dp" android:layout_marginEnd="20dp"
             app:srcCompat="@android:drawable/ic_input_add"/>
+
+    <com.google.android.material.floatingactionbutton.FloatingActionButton
+        android:id="@+id/helpButton"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_marginEnd="90dp"
+        android:layout_marginBottom="20dp"
+        android:clickable="true"
+        app:backgroundTint="#CDDC39"
+        app:layout_constraintBottom_toBottomOf="parent"
+        app:layout_constraintEnd_toEndOf="parent"
+        app:srcCompat="@android:drawable/ic_menu_help" />
+
 </androidx.constraintlayout.widget.ConstraintLayout>

+ 1 - 0
src/main/res/layout/layout_word_card.xml

@@ -26,6 +26,7 @@
         app:backgroundTint="#7B8A69" />
 
     <LinearLayout
+        android:id="@+id/prototypeLayout"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:background="#BA8E7A"