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

1.添加获取mmse试题库UI跳转

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

+ 272 - 41
mmse/src/main/java/com/yingyangfly/mmse/fragment/CountFragment.kt

@@ -32,6 +32,18 @@ class CountFragment : BaseFragment<FragmentCountBinding>() {
     private var fourthQuestion: QuestionsBean? = null
     private var fifthQuestion: QuestionsBean? = null
 
+    private var questionOne = false
+    private var questionTwo = false
+    private var questionThree = false
+    private var questionFour = false
+    private var questionFive = false
+
+    private val stringBufferOne = StringBuffer()
+    private val stringBufferTwo = StringBuffer()
+    private val stringBufferThree = StringBuffer()
+    private val stringBufferFour = StringBuffer()
+    private val stringBufferFive = StringBuffer()
+
     override fun onCreate(savedInstanceState: Bundle?) {
         questionId = arguments?.getInt("questionId") ?: 14
         choiceItems = arguments?.getString("choiceItems") ?: ""
@@ -53,16 +65,13 @@ class CountFragment : BaseFragment<FragmentCountBinding>() {
             adapter.onNumClickListener = { bean: String, type: String ->
                 when (type) {
                     "0" -> {
-
-
+                        append(bean)
                     }
                     "1" -> {
-
-
+                        delete()
                     }
                     "2" -> {
-
-
+                        save()
                     }
                 }
             }
@@ -92,6 +101,46 @@ class CountFragment : BaseFragment<FragmentCountBinding>() {
                     )
                 }
             }
+
+            editResultOne.setOnFocusChangeListener { v, hasFocus ->
+                questionOne = hasFocus
+                binding.editResultOne.postDelayed(
+                    { closeKeyBord(binding.editResultOne, mContext) },
+                    300
+                )
+            }
+
+            editResultTwo.setOnFocusChangeListener { v, hasFocus ->
+                questionTwo = hasFocus
+                binding.editResultTwo.postDelayed(
+                    { closeKeyBord(binding.editResultTwo, mContext) },
+                    300
+                )
+            }
+
+            editResultThree.setOnFocusChangeListener { v, hasFocus ->
+                questionThree = hasFocus
+                binding.editResultThree.postDelayed(
+                    { closeKeyBord(binding.editResultThree, mContext) },
+                    300
+                )
+            }
+
+            editResultFour.setOnFocusChangeListener { v, hasFocus ->
+                questionFour = hasFocus
+                binding.editResultFour.postDelayed(
+                    { closeKeyBord(binding.editResultFour, mContext) },
+                    300
+                )
+            }
+
+            editResultFive.setOnFocusChangeListener { v, hasFocus ->
+                questionFive = hasFocus
+                binding.editResultFive.postDelayed(
+                    { closeKeyBord(binding.editResultFive, mContext) },
+                    300
+                )
+            }
         }
     }
 
@@ -114,53 +163,63 @@ class CountFragment : BaseFragment<FragmentCountBinding>() {
         binding.editResultOne.postDelayed({ closeKeyBord(binding.editResultOne, mContext) }, 300)
         if (dao != null) {
             firstquestion = dao?.getQuestion(questionId)
-            if (firstquestion != null) {
-                val str = if (firstquestion?.inputString.isNullOrEmpty()) {
-                    ""
-                } else {
-                    firstquestion?.inputString
+            if (firstquestion != null && firstquestion!!.inputString.isNullOrEmpty().not()) {
+                binding.editResultOne.post {
+                    if (stringBufferOne.toString().isNullOrEmpty().not()) {
+                        stringBufferOne.delete(0, firstquestion!!.inputString.length - 1)
+                    }
+                    stringBufferOne.append(firstquestion!!.inputString)
+                    binding.editResultOne.setText(firstquestion!!.inputString)
+                    binding.editResultOne.setSelection(firstquestion!!.inputString.length)
                 }
-                binding.editResultOne.setText(str)
             }
             val secondQuestionnId = questionId + 1
             secondQuestion = dao?.getQuestion(secondQuestionnId)
-            if (secondQuestion != null) {
-                val str = if (secondQuestion?.inputString.isNullOrEmpty()) {
-                    ""
-                } else {
-                    secondQuestion?.inputString
+            if (secondQuestion != null && secondQuestion!!.inputString.isNullOrEmpty().not()) {
+                binding.editResultTwo.post {
+                    if (stringBufferTwo.toString().isNullOrEmpty().not()) {
+                        stringBufferTwo.delete(0, secondQuestion!!.inputString.length - 1)
+                    }
+                    stringBufferTwo.append(secondQuestion!!.inputString)
+                    binding.editResultTwo.setText(secondQuestion!!.inputString)
+                    binding.editResultTwo.setSelection(secondQuestion!!.inputString.length)
                 }
-                binding.editResultTwo.setText(str)
             }
             val thirdQuestionId = questionId + 2
             thirdQuestion = dao?.getQuestion(thirdQuestionId)
-            if (thirdQuestion != null) {
-                val str = if (thirdQuestion?.inputString.isNullOrEmpty()) {
-                    ""
-                } else {
-                    thirdQuestion?.inputString
+            if (thirdQuestion != null && thirdQuestion!!.inputString.isNullOrEmpty().not()) {
+                binding.editResultThree.post {
+                    if (stringBufferThree.toString().isNullOrEmpty().not()) {
+                        stringBufferThree.delete(0, thirdQuestion!!.inputString.length - 1)
+                    }
+                    stringBufferThree.append(thirdQuestion!!.inputString)
+                    binding.editResultThree.setText(thirdQuestion!!.inputString)
+                    binding.editResultThree.setSelection(thirdQuestion!!.inputString.length)
                 }
-                binding.editResultThree.setText(str)
             }
             val fourthQuestionId = questionId + 3
             fourthQuestion = dao?.getQuestion(fourthQuestionId)
-            if (fourthQuestion != null) {
-                val str = if (fourthQuestion?.inputString.isNullOrEmpty()) {
-                    ""
-                } else {
-                    fourthQuestion?.inputString
+            if (fourthQuestion != null && fourthQuestion!!.inputString.isNullOrEmpty().not()) {
+                binding.editResultFour.post {
+                    if (stringBufferFour.toString().isNullOrEmpty().not()) {
+                        stringBufferFour.delete(0, fourthQuestion!!.inputString.length - 1)
+                    }
+                    stringBufferFour.append(fourthQuestion!!.inputString)
+                    binding.editResultFour.setText(fourthQuestion!!.inputString)
+                    binding.editResultFour.setSelection(fourthQuestion!!.inputString.length)
                 }
-                binding.editResultFour.setText(str)
             }
             val fifthQuestionId = questionId + 4
             fifthQuestion = dao?.getQuestion(fifthQuestionId)
-            if (fifthQuestion != null) {
-                val str = if (fifthQuestion?.inputString.isNullOrEmpty()) {
-                    ""
-                } else {
-                    fifthQuestion?.inputString
+            if (fifthQuestion != null && fifthQuestion!!.inputString.isNullOrEmpty().not()) {
+                binding.editResultFive.post {
+                    if (stringBufferFive.toString().isNullOrEmpty().not()) {
+                        stringBufferFive.delete(0, fifthQuestion!!.inputString.length - 1)
+                    }
+                    stringBufferFive.append(fifthQuestion!!.inputString)
+                    binding.editResultFive.setText(fifthQuestion!!.inputString)
+                    binding.editResultFive.setSelection(fifthQuestion!!.inputString.length)
                 }
-                binding.editResultFive.setText(str)
             }
         }
     }
@@ -175,7 +234,10 @@ class CountFragment : BaseFragment<FragmentCountBinding>() {
                 binding.editResultOne.requestFocus()
                 binding.editResultOne.isFocusable = true
                 binding.editResultOne.isFocusableInTouchMode = true
-                binding.editResultOne.postDelayed({ closeKeyBord(binding.editResultOne, mContext) }, 300)
+                binding.editResultOne.postDelayed(
+                    { closeKeyBord(binding.editResultOne, mContext) },
+                    300
+                )
                 return false
             } else {
                 if (firstquestion != null) {
@@ -188,7 +250,10 @@ class CountFragment : BaseFragment<FragmentCountBinding>() {
                 binding.editResultTwo.requestFocus()
                 binding.editResultTwo.isFocusable = true
                 binding.editResultTwo.isFocusableInTouchMode = true
-                binding.editResultTwo.postDelayed({ closeKeyBord(binding.editResultOne, mContext) }, 300)
+                binding.editResultTwo.postDelayed(
+                    { closeKeyBord(binding.editResultOne, mContext) },
+                    300
+                )
                 return false
             } else {
                 if (secondQuestion != null) {
@@ -201,7 +266,12 @@ class CountFragment : BaseFragment<FragmentCountBinding>() {
                 binding.editResultThree.requestFocus()
                 binding.editResultThree.isFocusable = true
                 binding.editResultThree.isFocusableInTouchMode = true
-                binding.editResultThree.postDelayed({ closeKeyBord(binding.editResultOne, mContext) }, 300)
+                binding.editResultThree.postDelayed({
+                    closeKeyBord(
+                        binding.editResultOne,
+                        mContext
+                    )
+                }, 300)
                 return false
             } else {
                 if (thirdQuestion != null) {
@@ -214,7 +284,12 @@ class CountFragment : BaseFragment<FragmentCountBinding>() {
                 binding.editResultFour.requestFocus()
                 binding.editResultFour.isFocusable = true
                 binding.editResultFour.isFocusableInTouchMode = true
-                binding.editResultFour.postDelayed({ closeKeyBord(binding.editResultOne, mContext) }, 300)
+                binding.editResultFour.postDelayed({
+                    closeKeyBord(
+                        binding.editResultOne,
+                        mContext
+                    )
+                }, 300)
                 return false
             } else {
                 if (fourthQuestion != null) {
@@ -227,7 +302,12 @@ class CountFragment : BaseFragment<FragmentCountBinding>() {
                 binding.editResultFive.requestFocus()
                 binding.editResultFive.isFocusable = true
                 binding.editResultFive.isFocusableInTouchMode = true
-                binding.editResultFive.postDelayed({ closeKeyBord(binding.editResultOne, mContext) }, 300)
+                binding.editResultFive.postDelayed({
+                    closeKeyBord(
+                        binding.editResultOne,
+                        mContext
+                    )
+                }, 300)
                 return false
             } else {
                 if (fifthQuestion != null) {
@@ -238,4 +318,155 @@ class CountFragment : BaseFragment<FragmentCountBinding>() {
         }
         return true
     }
+
+    /**
+     * 添加数据
+     */
+    private fun append(bean: String) {
+        if (questionOne) {
+            binding.editResultOne.post {
+                stringBufferOne.append(bean)
+                binding.editResultOne.setText(stringBufferOne.toString())
+                binding.editResultOne.setSelection(stringBufferOne.toString().length)
+            }
+        }
+        if (questionTwo) {
+            binding.editResultTwo.post {
+                stringBufferTwo.append(bean)
+                binding.editResultTwo.setText(stringBufferTwo.toString())
+                binding.editResultTwo.setSelection(stringBufferTwo.toString().length)
+            }
+        }
+        if (questionThree) {
+            binding.editResultThree.post {
+                stringBufferThree.append(bean)
+                binding.editResultThree.setText(stringBufferThree.toString())
+                binding.editResultThree.setSelection(stringBufferThree.toString().length)
+            }
+        }
+        if (questionFour) {
+            binding.editResultFour.post {
+                stringBufferFour.append(bean)
+                binding.editResultFour.setText(stringBufferFour.toString())
+                binding.editResultFour.setSelection(stringBufferFour.toString().length)
+            }
+        }
+        if (questionFive) {
+            binding.editResultFive.post {
+                stringBufferFive.append(bean)
+                binding.editResultFive.setText(stringBufferFive.toString())
+                binding.editResultFive.setSelection(stringBufferFive.toString().length)
+            }
+        }
+    }
+
+    /**
+     * 删除数据
+     */
+    private fun delete() {
+        if (questionOne) {
+            binding.editResultOne.post {
+                if (stringBufferOne.toString().isNullOrEmpty().not()) {
+                    val size = stringBufferOne.toString().length - 1
+                    stringBufferOne.deleteCharAt(size)
+                    binding.editResultOne.setText(stringBufferOne.toString())
+                    binding.editResultOne.setSelection(stringBufferOne.toString().length)
+                }
+            }
+        }
+        if (questionTwo) {
+            binding.editResultTwo.post {
+                if (stringBufferTwo.toString().isNullOrEmpty().not()) {
+                    val size = stringBufferTwo.toString().length - 1
+                    stringBufferTwo.deleteCharAt(size)
+                    binding.editResultTwo.setText(stringBufferTwo.toString())
+                    binding.editResultTwo.setSelection(stringBufferTwo.toString().length)
+                }
+            }
+        }
+        if (questionThree) {
+            binding.editResultThree.post {
+                if (stringBufferThree.toString().isNullOrEmpty().not()) {
+                    val size = stringBufferThree.toString().length - 1
+                    stringBufferThree.deleteCharAt(size)
+                    binding.editResultThree.setText(stringBufferThree.toString())
+                    binding.editResultThree.setSelection(stringBufferThree.toString().length)
+                }
+            }
+        }
+        if (questionFour) {
+            binding.editResultFour.post {
+                if (stringBufferFour.toString().isNullOrEmpty().not()) {
+                    val size = stringBufferFour.toString().length - 1
+                    stringBufferFour.deleteCharAt(size)
+                    binding.editResultFour.setText(stringBufferFour.toString())
+                    binding.editResultFour.setSelection(stringBufferFour.toString().length)
+                }
+            }
+        }
+        if (questionFive) {
+            binding.editResultFive.post {
+                if (stringBufferFive.toString().isNullOrEmpty().not()) {
+                    val size = stringBufferFive.toString().length - 1
+                    stringBufferFive.deleteCharAt(size)
+                    binding.editResultFive.setText(stringBufferFive.toString())
+                    binding.editResultFive.setSelection(stringBufferFive.toString().length)
+                }
+            }
+        }
+    }
+
+    /**
+     * 保存数据
+     */
+    private fun save() {
+        if (dao != null) {
+            if (questionOne) {
+                if (firstquestion != null) {
+                    firstquestion!!.inputString = binding.editResultOne.toString().trim()
+                    firstquestion!!.review_id = firstquestion!!.id
+                    firstquestion!!.review_answer = binding.editResultOne.toString().trim()
+                    firstquestion!!.correct = ""
+                    dao?.update(firstquestion!!)
+                }
+            }
+            if (questionTwo) {
+                if (secondQuestion != null) {
+                    secondQuestion!!.inputString = binding.editResultTwo.toString().trim()
+                    secondQuestion!!.review_id = secondQuestion!!.id
+                    secondQuestion!!.review_answer = binding.editResultTwo.toString().trim()
+                    secondQuestion!!.correct = ""
+                    dao?.update(secondQuestion!!)
+                }
+            }
+            if (questionThree) {
+                if (thirdQuestion != null) {
+                    thirdQuestion!!.inputString = binding.editResultThree.toString().trim()
+                    thirdQuestion!!.review_id = thirdQuestion!!.id
+                    thirdQuestion!!.review_answer = binding.editResultThree.toString().trim()
+                    thirdQuestion!!.correct = ""
+                    dao?.update(thirdQuestion!!)
+                }
+            }
+            if (questionFour) {
+                if (fourthQuestion != null) {
+                    fourthQuestion!!.inputString = binding.editResultFour.toString().trim()
+                    fourthQuestion!!.review_id = fourthQuestion!!.id
+                    fourthQuestion!!.review_answer = binding.editResultFour.toString().trim()
+                    fourthQuestion!!.correct = ""
+                    dao?.update(fourthQuestion!!)
+                }
+            }
+            if (questionFive) {
+                if (fifthQuestion != null) {
+                    fifthQuestion!!.inputString = binding.editResultFive.toString().trim()
+                    fifthQuestion!!.review_id = fifthQuestion!!.id
+                    fifthQuestion!!.review_answer = binding.editResultFive.toString().trim()
+                    fifthQuestion!!.correct = ""
+                    dao?.update(fifthQuestion!!)
+                }
+            }
+
+        }
+    }
 }

+ 3 - 1
mmse/src/main/java/com/yingyangfly/mmse/fragment/YearFragment.kt

@@ -114,7 +114,9 @@ class YearFragment : BaseFragment<FragmentYearBinding>() {
                 toastInfo = question!!.reviewItem
                 if (question!!.inputString.isNullOrEmpty().not()) {
                     binding.editYear.post {
-                        stringBuffer.delete(0, question!!.inputString.length - 1)
+                        if (stringBuffer.toString().isNullOrEmpty().not()) {
+                            stringBuffer.delete(0, question!!.inputString.length - 1)
+                        }
                         stringBuffer.append(question!!.inputString)
                         binding.editYear.setText(question!!.inputString)
                         binding.editYear.setSelection(question!!.inputString.length)