Преглед изворни кода

1.添加获取moca试题库module

王鹏鹏 пре 2 година
родитељ
комит
b8b3b2b354

+ 301 - 15
moca/src/main/java/com/yingyangfly/moca/alternatingconnection/AlternatingConnectionFragment.kt

@@ -1,12 +1,14 @@
 package com.yingyangfly.moca.alternatingconnection
 
+import android.text.TextUtils
 import androidx.core.os.bundleOf
 import androidx.navigation.Navigation
 import com.yingyang.moca.R
 import com.yingyang.moca.databinding.FragmentAlternatingConnectionBinding
 import com.yingyangfly.baselib.base.BaseFragment
 import com.yingyangfly.baselib.db.QuestionsBean
-import com.yingyangfly.baselib.ext.setOnSingleClickListener
+import com.yingyangfly.baselib.ext.toast
+import com.yingyangfly.moca.entity.MyLine
 
 /**
  * 交替连线测验
@@ -18,6 +20,15 @@ class AlternatingConnectionFragment : BaseFragment<FragmentAlternatingConnection
      */
     var questionId = 32
     var question: QuestionsBean? = null
+    private val myLines = mutableListOf<MyLine>()
+    private var endx = 0
+    private var endy = 0
+    private val stringBuffer: StringBuffer = StringBuffer()
+
+    /**
+     * 判断是否选择
+     */
+    private var firstChecked = true
 
     override fun initViews() {
 
@@ -25,17 +36,297 @@ class AlternatingConnectionFragment : BaseFragment<FragmentAlternatingConnection
 
     override fun initListener() {
         binding {
-            btnPrevious.setOnSingleClickListener {
+            btnPrevious.setOnClickListener {
+                myLines.clear()
+                firstChecked = true
+                signatureView.clearData()
+                tvFirst.isEnabled = true
+                tvSeconds.isEnabled = true
+                tvThird.isEnabled = true
+                tvFourth.isEnabled = true
+                tvFifth.isEnabled = true
+                tvOne.isEnabled = true
+                tvTwo.isEnabled = true
+                tvThree.isEnabled = true
+                tvFour.isEnabled = true
+                tvFive.isEnabled = true
+                tvFirst.isChecked = false
+                tvSeconds.isChecked = false
+                tvThird.isChecked = false
+                tvFourth.isChecked = false
+                tvFifth.isChecked = false
+                tvOne.isChecked = false
+                tvTwo.isChecked = false
+                tvThree.isChecked = false
+                tvThird.isChecked = false
+                tvFour.isChecked = false
+                tvFive.isChecked = false
+                if (stringBuffer.toString().isNullOrEmpty().not()) {
+                    stringBuffer.delete(0, stringBuffer.toString().length)
+                }
+            }
+            btnNext.setOnClickListener {
+                if (stringBuffer.toString().isNullOrEmpty()) {
+                    "请连线".toast()
+                } else {
+                    if (TextUtils.equals("1甲2乙3丙4丁5戊", stringBuffer.toString())) {
+                        if (question != null) {
+                            question?.score = question?.reviewScore
+                            question?.correct = "1"
+                            dao?.update(question!!)
+                        }
+                    } else {
+                        if (question != null) {
+                            question?.score = "0"
+                            question?.correct = "0"
+                            dao?.update(question!!)
+                        }
+                    }
+                    val bundle = bundleOf("questionId" to 33)
+                    val controller = Navigation.findNavController(it)
+                    controller.navigate(
+                        R.id.action_alternatingconnectionFragment_to_signatureFragment,
+                        bundle
+                    )
+                }
+            }
+            tvFirst.setOnCheckedChangeListener { buttonView, isChecked ->
+                if (isChecked) {
+                    stringBuffer.append("甲")
+                    tvFirst.isEnabled = false
+                    if (firstChecked) {
+                        firstChecked = false
+                        endx = tvFirst.left + tvFirst.width / 2
+                        endy = tvFirst.top + tvFirst.height / 2
+                    } else {
+                        val myLine = MyLine()
+                        val x = tvFirst.left + tvFirst.width / 2
+                        val y = tvFirst.top + tvFirst.height / 2
+                        myLine.startx = x
+                        myLine.starty = y
+                        myLine.endx = endx
+                        myLine.endy = endy
+                        myLines.add(myLine)
+                        signatureView.setData(myLines)
+                        endx = x
+                        endy = y
+                    }
+                }
+            }
 
+            tvOne.setOnCheckedChangeListener { buttonView, isChecked ->
+                if (isChecked) {
+                    stringBuffer.append("1")
+                    tvOne.isEnabled = false
+                    if (firstChecked) {
+                        firstChecked = false
+                        endx = tvOne.left + tvOne.width / 2
+                        endy = tvOne.top + tvOne.height / 2
+                    } else {
+                        val myLine = MyLine()
+                        val x = tvOne.left + tvOne.width / 2
+                        val y = tvOne.top + tvOne.height / 2
+                        myLine.startx = x
+                        myLine.starty = y
+                        myLine.endx = endx
+                        myLine.endy = endy
+                        myLines.add(myLine)
+                        signatureView.setData(myLines)
+                        endx = x
+                        endy = y
+                    }
+                }
             }
-            btnNext.setOnSingleClickListener {
-
-                val bundle = bundleOf("questionId" to 33)
-                val controller = Navigation.findNavController(it)
-                controller.navigate(
-                    R.id.action_alternatingconnectionFragment_to_signatureFragment,
-                    bundle
-                )
+
+            tvSeconds.setOnCheckedChangeListener { buttonView, isChecked ->
+                if (isChecked) {
+                    stringBuffer.append("乙")
+                    tvSeconds.isEnabled = false
+                    if (firstChecked) {
+                        firstChecked = false
+                        endx = tvSeconds.left + tvSeconds.width / 2
+                        endy = tvSeconds.top + tvSeconds.height / 2
+                    } else {
+                        val myLine = MyLine()
+                        val x = tvSeconds.left + tvSeconds.width / 2
+                        val y = tvSeconds.top + tvSeconds.height / 2
+                        myLine.startx = x
+                        myLine.starty = y
+                        myLine.endx = endx
+                        myLine.endy = endy
+                        myLines.add(myLine)
+                        signatureView.setData(myLines)
+                        endx = x
+                        endy = y
+                    }
+                }
+            }
+            tvTwo.setOnCheckedChangeListener { buttonView, isChecked ->
+                if (isChecked) {
+                    stringBuffer.append("2")
+                    tvTwo.isEnabled = false
+                    if (firstChecked) {
+                        firstChecked = false
+                        endx = tvTwo.left + tvTwo.width / 2
+                        endy = tvTwo.top + tvTwo.height / 2
+                    } else {
+                        val myLine = MyLine()
+                        val x = tvTwo.left + tvTwo.width / 2
+                        val y = tvTwo.top + tvTwo.height / 2
+                        myLine.startx = x
+                        myLine.starty = y
+                        myLine.endx = endx
+                        myLine.endy = endy
+                        myLines.add(myLine)
+                        signatureView.setData(myLines)
+                        endx = x
+                        endy = y
+                    }
+                }
+            }
+
+            tvThird.setOnCheckedChangeListener { buttonView, isChecked ->
+                if (isChecked) {
+                    stringBuffer.append("丙")
+                    tvThird.isEnabled = false
+                    if (firstChecked) {
+                        firstChecked = false
+                        endx = tvThird.left + tvThird.width / 2
+                        endy = tvThird.top + tvThird.height / 2
+                    } else {
+                        val myLine = MyLine()
+                        val x = tvThird.left + tvThird.width / 2
+                        val y = tvThird.top + tvThird.height / 2
+                        myLine.startx = x
+                        myLine.starty = y
+                        myLine.endx = endx
+                        myLine.endy = endy
+                        myLines.add(myLine)
+                        signatureView.setData(myLines)
+                        endx = x
+                        endy = y
+                    }
+                }
+            }
+
+            tvThree.setOnCheckedChangeListener { buttonView, isChecked ->
+                if (isChecked) {
+                    stringBuffer.append("3")
+                    tvThree.isEnabled = false
+                    if (firstChecked) {
+                        firstChecked = false
+                        endx = tvThree.left + tvThree.width / 2
+                        endy = tvThree.top + tvThree.height / 2
+                    } else {
+                        val myLine = MyLine()
+                        val x = tvThree.left + tvThree.width / 2
+                        val y = tvThree.top + tvThree.height / 2
+                        myLine.startx = x
+                        myLine.starty = y
+                        myLine.endx = endx
+                        myLine.endy = endy
+                        myLines.add(myLine)
+                        signatureView.setData(myLines)
+                        endx = x
+                        endy = y
+                    }
+                }
+            }
+
+            tvFourth.setOnCheckedChangeListener { buttonView, isChecked ->
+                if (isChecked) {
+                    stringBuffer.append("丁")
+                    tvFourth.isEnabled = false
+                    if (firstChecked) {
+                        firstChecked = false
+                        endx = tvFourth.left + tvFourth.width / 2
+                        endy = tvFourth.top + tvFourth.height / 2
+                    } else {
+                        val myLine = MyLine()
+                        val x = tvFourth.left + tvFourth.width / 2
+                        val y = tvFourth.top + tvFourth.height / 2
+                        myLine.startx = x
+                        myLine.starty = y
+                        myLine.endx = endx
+                        myLine.endy = endy
+                        myLines.add(myLine)
+                        signatureView.setData(myLines)
+                        endx = x
+                        endy = y
+                    }
+                }
+            }
+
+            tvFour.setOnCheckedChangeListener { buttonView, isChecked ->
+                if (isChecked) {
+                    stringBuffer.append("4")
+                    tvFour.isEnabled = false
+                    if (firstChecked) {
+                        firstChecked = false
+                        endx = tvFour.left + tvFour.width / 2
+                        endy = tvFour.top + tvFour.height / 2
+                    } else {
+                        val myLine = MyLine()
+                        val x = tvFour.left + tvFour.width / 2
+                        val y = tvFour.top + tvFour.height / 2
+                        myLine.startx = x
+                        myLine.starty = y
+                        myLine.endx = endx
+                        myLine.endy = endy
+                        myLines.add(myLine)
+                        signatureView.setData(myLines)
+                        endx = x
+                        endy = y
+                    }
+                }
+            }
+
+            tvFifth.setOnCheckedChangeListener { buttonView, isChecked ->
+                if (isChecked) {
+                    stringBuffer.append("戊")
+                    tvFifth.isEnabled = false
+                    if (firstChecked) {
+                        firstChecked = false
+                        endx = tvFifth.left + tvFifth.width / 2
+                        endy = tvFifth.top + tvFifth.height / 2
+                    } else {
+                        val myLine = MyLine()
+                        val x = tvFifth.left + tvFifth.width / 2
+                        val y = tvFifth.top + tvFifth.height / 2
+                        myLine.startx = x
+                        myLine.starty = y
+                        myLine.endx = endx
+                        myLine.endy = endy
+                        myLines.add(myLine)
+                        signatureView.setData(myLines)
+                        endx = x
+                        endy = y
+                    }
+                }
+            }
+
+            tvFive.setOnCheckedChangeListener { buttonView, isChecked ->
+                if (isChecked) {
+                    stringBuffer.append("5")
+                    tvFive.isEnabled = false
+                    if (firstChecked) {
+                        firstChecked = false
+                        endx = tvFive.left + tvFive.width / 2
+                        endy = tvFive.top + tvFive.height / 2
+                    } else {
+                        val myLine = MyLine()
+                        val x = tvFive.left + tvFive.width / 2
+                        val y = tvFive.top + tvFive.height / 2
+                        myLine.startx = x
+                        myLine.starty = y
+                        myLine.endx = endx
+                        myLine.endy = endy
+                        myLines.add(myLine)
+                        signatureView.setData(myLines)
+                        endx = x
+                        endy = y
+                    }
+                }
             }
         }
     }
@@ -55,11 +346,6 @@ class AlternatingConnectionFragment : BaseFragment<FragmentAlternatingConnection
     private fun loadData() {
         if (dao != null) {
             question = dao?.getQuestion(questionId)
-            if (question != null) {
-                question?.score = question?.reviewScore
-                question?.correct = "1"
-                dao?.update(question!!)
-            }
         }
     }
 }

+ 58 - 11
moca/src/main/java/com/yingyangfly/moca/widget/LinkLineView.java

@@ -4,15 +4,28 @@ import android.content.Context;
 import android.graphics.Canvas;
 import android.graphics.Color;
 import android.graphics.Paint;
+import android.os.Build;
 import android.util.AttributeSet;
-import android.widget.RelativeLayout;
 
 import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
+import androidx.annotation.RequiresApi;
+import androidx.constraintlayout.widget.ConstraintLayout;
 
-public class LinkLineView extends RelativeLayout {
+import com.yingyang.moca.R;
+import com.yingyangfly.baselib.utils.ScreenUtil;
+import com.yingyangfly.moca.entity.MyLine;
 
-    Paint linePaint = new Paint();
+import java.util.ArrayList;
+import java.util.List;
+
+public class LinkLineView extends ConstraintLayout {
+
+    private Context context;
+    private boolean isReset = false;
+    private List<MyLine> linkLineBeanList = new ArrayList<>();
+
+    private List<MyLine> myLines = new ArrayList<>();
 
     public LinkLineView(Context context) {
         this(context, null);
@@ -24,22 +37,56 @@ public class LinkLineView extends RelativeLayout {
 
     public LinkLineView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
         super(context, attrs, defStyleAttr);
-        initLinePaint();
+        init(context);
     }
 
-    private void initLinePaint() {
-        // TODO Auto-generated method stub
-        linePaint.setColor(Color.BLUE);
-        linePaint.setStrokeWidth(2);
-        linePaint.setAntiAlias(true);
-        linePaint.setStrokeCap(Paint.Cap.ROUND);
+    private void init(Context context) {
+        this.context = context;
     }
 
+    @RequiresApi(api = Build.VERSION_CODES.M)
     @Override
     protected void dispatchDraw(Canvas canvas) {
         super.dispatchDraw(canvas);
+        // 先清除掉原有绘制的线
+        if (isReset) {
+            for (MyLine item : linkLineBeanList) {
+                if (item != null) {
+                    Paint paint = new Paint();
+                    paint.setColor(Color.TRANSPARENT);
+                    paint.setStrokeWidth(ScreenUtil.INSTANCE.dip2px(context, 2));
+                    canvas.drawLine(item.getStartx(), item.getStarty(), item.getEndx(), item.getEndy(), paint);
+                }
+            }
+        } else {
+            for (MyLine item : linkLineBeanList) {
+                if (item != null) {
+                    Paint paint = new Paint();
+                    paint.setColor(context.getColor(R.color.color_FF4A76FF));
+                    paint.setStrokeWidth(ScreenUtil.INSTANCE.dip2px(context, 2));
+                    canvas.drawLine(item.getStartx(), item.getStarty(), item.getEndx(), item.getEndy(), paint);
+                }
+            }
+        }
+    }
 
+    /**
+     * 练习
+     *
+     * @param linkDataBeanList
+     */
+    public void setData(List<MyLine> linkDataBeanList) {
+        if (linkDataBeanList == null || linkDataBeanList.size() == 0) {
+            return;
+        }
+        isReset = false;
+        linkLineBeanList.clear();
+        linkLineBeanList.addAll(linkDataBeanList);
+        invalidate();
+    }
 
-
+    public void clearData() {
+        isReset = true;
+        invalidate();
     }
 }

+ 138 - 11
moca/src/main/res/layout/fragment_alternating_connection.xml

@@ -47,43 +47,170 @@
                 android:layout_width="match_parent"
                 android:layout_height="match_parent">
 
-                <androidx.appcompat.widget.AppCompatTextView
+                <androidx.appcompat.widget.AppCompatCheckBox
                     android:id="@+id/tvFirst"
                     android:layout_width="@dimen/divider_60px"
                     android:layout_height="@dimen/divider_60px"
-                    android:layout_alignParentTop="true"
-                    android:layout_alignParentEnd="true"
-                    android:layout_centerInParent="true"
                     android:layout_marginTop="@dimen/divider_37px"
                     android:layout_marginEnd="@dimen/divider_218px"
                     android:background="@drawable/bg_selected_text"
+                    android:button="@null"
                     android:gravity="center"
                     android:text="@string/first"
                     android:textColor="@android:color/white"
                     android:textSize="@dimen/divider_30px"
-                    android:textStyle="bold" />
+                    android:textStyle="bold"
+                    app:layout_constraintEnd_toEndOf="parent"
+                    app:layout_constraintTop_toTopOf="parent" />
 
-                <androidx.appcompat.widget.AppCompatTextView
+                <androidx.appcompat.widget.AppCompatCheckBox
                     android:id="@+id/tvTwo"
                     android:layout_width="@dimen/divider_60px"
                     android:layout_height="@dimen/divider_60px"
-                    android:layout_below="@+id/tvFirst"
-                    android:layout_alignParentEnd="true"
-                    android:layout_centerInParent="true"
                     android:layout_marginTop="@dimen/divider_9px"
                     android:layout_marginEnd="@dimen/divider_148px"
                     android:background="@drawable/bg_selected_text"
+                    android:button="@null"
                     android:gravity="center"
                     android:text="@string/two"
                     android:textColor="@android:color/white"
                     android:textSize="@dimen/divider_30px"
-                    android:textStyle="bold" />
+                    android:textStyle="bold"
+                    app:layout_constraintEnd_toEndOf="parent"
+                    app:layout_constraintTop_toBottomOf="@+id/tvFirst" />
+
+                <androidx.appcompat.widget.AppCompatCheckBox
+                    android:id="@+id/tvSeconds"
+                    android:layout_width="@dimen/divider_60px"
+                    android:layout_height="@dimen/divider_60px"
+                    android:layout_marginTop="@dimen/divider_18px"
+                    android:layout_marginEnd="@dimen/divider_42px"
+                    android:background="@drawable/bg_selected_text"
+                    android:button="@null"
+                    android:gravity="center"
+                    android:text="@string/seconds"
+                    android:textColor="@android:color/white"
+                    android:textSize="@dimen/divider_30px"
+                    android:textStyle="bold"
+                    app:layout_constraintEnd_toStartOf="@+id/tvTwo"
+                    app:layout_constraintTop_toBottomOf="@+id/tvFirst" />
+
+                <androidx.appcompat.widget.AppCompatCheckBox
+                    android:id="@+id/tvOne"
+                    android:layout_width="@dimen/divider_60px"
+                    android:layout_height="@dimen/divider_60px"
+                    android:layout_marginTop="@dimen/divider_13px"
+                    android:layout_marginEnd="@dimen/divider_42px"
+                    android:background="@drawable/bg_selected_text"
+                    android:button="@null"
+                    android:gravity="center"
+                    android:text="@string/one"
+                    android:textColor="@android:color/white"
+                    android:textSize="@dimen/divider_30px"
+                    android:textStyle="bold"
+                    app:layout_constraintEnd_toStartOf="@+id/tvSeconds"
+                    app:layout_constraintTop_toBottomOf="@+id/tvFirst" />
+
+                <androidx.appcompat.widget.AppCompatCheckBox
+                    android:id="@+id/tvFifth"
+                    android:layout_width="@dimen/divider_60px"
+                    android:layout_height="@dimen/divider_60px"
+                    android:layout_marginStart="@dimen/divider_265px"
+                    android:layout_marginTop="@dimen/divider_45px"
+                    android:background="@drawable/bg_selected_text"
+                    android:button="@null"
+                    android:gravity="center"
+                    android:text="@string/fifth"
+                    android:textColor="@android:color/white"
+                    android:textSize="@dimen/divider_30px"
+                    android:textStyle="bold"
+                    app:layout_constraintStart_toStartOf="parent"
+                    app:layout_constraintTop_toTopOf="parent" />
+
+                <androidx.appcompat.widget.AppCompatCheckBox
+                    android:id="@+id/tvFive"
+                    android:layout_width="@dimen/divider_60px"
+                    android:layout_height="@dimen/divider_60px"
+                    android:layout_marginStart="@dimen/divider_179px"
+                    android:layout_marginTop="@dimen/divider_101px"
+                    android:background="@drawable/bg_selected_text"
+                    android:button="@null"
+                    android:gravity="center"
+                    android:text="@string/five"
+                    android:textColor="@android:color/white"
+                    android:textSize="@dimen/divider_30px"
+                    android:textStyle="bold"
+                    app:layout_constraintStart_toStartOf="parent"
+                    app:layout_constraintTop_toTopOf="parent" />
+
+                <androidx.appcompat.widget.AppCompatCheckBox
+                    android:id="@+id/tvFourth"
+                    android:layout_width="@dimen/divider_60px"
+                    android:layout_height="@dimen/divider_60px"
+                    android:layout_marginStart="@dimen/divider_148px"
+                    android:layout_marginTop="@dimen/divider_31px"
+                    android:background="@drawable/bg_selected_text"
+                    android:button="@null"
+                    android:gravity="center"
+                    android:text="@string/fourth"
+                    android:textColor="@android:color/white"
+                    android:textSize="@dimen/divider_30px"
+                    android:textStyle="bold"
+                    app:layout_constraintStart_toStartOf="parent"
+                    app:layout_constraintTop_toBottomOf="@+id/tvFive" />
+
+                <androidx.appcompat.widget.AppCompatCheckBox
+                    android:id="@+id/tvThird"
+                    android:layout_width="@dimen/divider_60px"
+                    android:layout_height="@dimen/divider_60px"
+                    android:layout_marginStart="@dimen/divider_134px"
+                    android:layout_marginTop="@dimen/divider_45px"
+                    android:background="@drawable/bg_selected_text"
+                    android:button="@null"
+                    android:gravity="center"
+                    android:text="@string/third"
+                    android:textColor="@android:color/white"
+                    android:textSize="@dimen/divider_30px"
+                    android:textStyle="bold"
+                    app:layout_constraintStart_toEndOf="@+id/tvFourth"
+                    app:layout_constraintTop_toBottomOf="@+id/tvOne" />
+
+                <androidx.appcompat.widget.AppCompatCheckBox
+                    android:id="@+id/tvThree"
+                    android:layout_width="@dimen/divider_60px"
+                    android:layout_height="@dimen/divider_60px"
+                    android:layout_marginStart="@dimen/divider_36px"
+                    android:layout_marginTop="@dimen/divider_52px"
+                    android:background="@drawable/bg_selected_text"
+                    android:button="@null"
+                    android:gravity="center"
+                    android:text="@string/three"
+                    android:textColor="@android:color/white"
+                    android:textSize="@dimen/divider_30px"
+                    android:textStyle="bold"
+                    app:layout_constraintStart_toEndOf="@+id/tvThird"
+                    app:layout_constraintTop_toBottomOf="@+id/tvSeconds" />
+
+                <androidx.appcompat.widget.AppCompatCheckBox
+                    android:id="@+id/tvFour"
+                    android:layout_width="@dimen/divider_60px"
+                    android:layout_height="@dimen/divider_60px"
+                    android:layout_marginStart="@dimen/divider_48px"
+                    android:layout_marginTop="@dimen/divider_5px"
+                    android:background="@drawable/bg_selected_text"
+                    android:button="@null"
+                    android:gravity="center"
+                    android:text="@string/four"
+                    android:textColor="@android:color/white"
+                    android:textSize="@dimen/divider_30px"
+                    android:textStyle="bold"
+                    app:layout_constraintStart_toEndOf="@+id/tvFourth"
+                    app:layout_constraintTop_toBottomOf="@+id/tvFourth" />
 
             </com.yingyangfly.moca.widget.LinkLineView>
 
         </com.google.android.material.card.MaterialCardView>
 
-
         <androidx.appcompat.widget.AppCompatButton
             android:id="@+id/btnPrevious"
             android:layout_width="@dimen/divider_240px"

+ 1 - 0
moca/src/main/res/values/dimens.xml

@@ -84,6 +84,7 @@
     <dimen name="divider_147px" tools:ignore="ResourceName">147px</dimen>
     <dimen name="divider_140px" tools:ignore="ResourceName">140px</dimen>
     <dimen name="divider_135px" tools:ignore="ResourceName">135px</dimen>
+    <dimen name="divider_134px" tools:ignore="ResourceName">134px</dimen>
     <dimen name="divider_133px" tools:ignore="ResourceName">133px</dimen>
     <dimen name="divider_130px" tools:ignore="ResourceName">130px</dimen>
     <dimen name="divider_128px" tools:ignore="ResourceName">128px</dimen>