{"id":1560,"date":"2021-12-17T10:17:59","date_gmt":"2021-12-17T02:17:59","guid":{"rendered":"https:\/\/badgameshow.com\/fly\/?p=1560"},"modified":"2021-12-17T10:19:34","modified_gmt":"2021-12-17T02:19:34","slug":"android-%e5%9b%9b%e6%a0%bc%e9%a9%97%e8%ad%89%e7%a2%bc%e6%88%96%e5%af%86%e7%a2%bc","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/fly\/android-%e5%9b%9b%e6%a0%bc%e9%a9%97%e8%ad%89%e7%a2%bc%e6%88%96%e5%af%86%e7%a2%bc\/","title":{"rendered":"Android \u56db\u683c\u9a57\u8b49\u78bc\u6216\u5bc6\u78bc"},"content":{"rendered":"<h1>Android \u56db\u683c\u9a57\u8b49\u78bc\u6216\u5bc6\u78bc<\/h1>\n<h5>\u56db\u683c\u9a57\u8b49\u78bc\u9700\u8981\u62c6\u5206\u70ba\u56db\u500bEditText\u8f38\u5165\u6846\uff0c\u4e26\u4e14\u505a\u4e00\u4e9b\u5224\u65b7\u4f86\u63a7\u5236\u8df3\u4e0b\u4e00\u683c\u548c\u522a\u9664\uff0c\u9084\u8981\u6301\u7e8c\u76e3\u807d\u56db\u500bEditText\u3002<\/h5>\n<hr \/>\n<h4>\u6587\u7ae0\u76ee\u9304<\/h4>\n<ol>\n<li><a href=\"#a\">UI<\/a><\/li>\n<li><a href=\"#b\">\u76e3\u807d\u662f\u5426\u8f38\u5165<\/a><\/li>\n<li><a href=\"#c\">\u76e3\u807d\u522a\u9664<\/a><\/li>\n<li><a href=\"#d\">\u7a0b\u5f0f\u78bc\u7bc4\u4f8b<\/a><\/li>\n<li><a href=\"#e\">\u6548\u679c\u5c55\u793a<\/a><\/li>\n<li><a href=\"#f\">Github<\/a><\/li>\n<\/ol>\n<hr \/>\n<p><a id=\"a\"><\/a><\/p>\n<h4>1.UI<\/h4>\n<pre><code class=\"language-XML line-numbers\">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\n&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n    xmlns:app=\"http:\/\/schemas.android.com\/apk\/res-auto\"\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\"\n    tools:context=\".MainActivity\"&gt;\n\n    &lt;androidx.appcompat.widget.AppCompatTextView\n        android:id=\"@+id\/appCompatTextView\"\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:layout_marginTop=\"24dp\"\n        android:text=\"\u8f38\u5165\u9a57\u8b49\u78bc\"\n        android:textColor=\"#FE735F\"\n        android:textSize=\"40sp\"\n        app:layout_constraintEnd_toEndOf=\"parent\"\n        app:layout_constraintStart_toStartOf=\"parent\"\n        app:layout_constraintTop_toTopOf=\"parent\" \/&gt;\n\n    &lt;androidx.appcompat.widget.AppCompatEditText\n        android:id=\"@+id\/one\"\n        android:layout_width=\"0dp\"\n        android:layout_height=\"wrap_content\"\n        android:layout_marginTop=\"46dp\"\n        android:background=\"@null\"\n        android:cursorVisible=\"false\"\n        android:inputType=\"numberDecimal\"\n        android:maxLength=\"1\"\n        android:textColor=\"#707070\"\n        android:textSize=\"60sp\"\n        app:layout_constraintEnd_toEndOf=\"@+id\/one_line\"\n        app:layout_constraintStart_toStartOf=\"@+id\/one_line\"\n        app:layout_constraintTop_toBottomOf=\"@+id\/appCompatTextView\" \/&gt;\n\n    &lt;View\n        android:id=\"@+id\/one_line\"\n        android:layout_width=\"40dp\"\n        android:layout_height=\"2dp\"\n        android:background=\"#BCBCBC\"\n        app:layout_constraintEnd_toStartOf=\"@+id\/two_line\"\n        app:layout_constraintHorizontal_bias=\"0.5\"\n        app:layout_constraintStart_toStartOf=\"parent\"\n        app:layout_constraintTop_toBottomOf=\"@+id\/one\" \/&gt;\n\n    &lt;androidx.appcompat.widget.AppCompatEditText\n        android:id=\"@+id\/two\"\n        android:layout_width=\"0dp\"\n        android:layout_height=\"wrap_content\"\n        android:background=\"@null\"\n        android:cursorVisible=\"false\"\n        android:inputType=\"numberDecimal\"\n        android:maxLength=\"1\"\n        android:textColor=\"#707070\"\n        android:textSize=\"60sp\"\n        app:layout_constraintEnd_toEndOf=\"@+id\/two_line\"\n        app:layout_constraintStart_toStartOf=\"@+id\/two_line\"\n        app:layout_constraintTop_toTopOf=\"@+id\/one\" \/&gt;\n\n    &lt;View\n        android:id=\"@+id\/two_line\"\n        android:layout_width=\"40dp\"\n        android:layout_height=\"2dp\"\n        android:background=\"#BCBCBC\"\n        app:layout_constraintEnd_toStartOf=\"@+id\/three_line\"\n        app:layout_constraintHorizontal_bias=\"0.5\"\n        app:layout_constraintStart_toEndOf=\"@+id\/one_line\"\n        app:layout_constraintTop_toBottomOf=\"@+id\/two\" \/&gt;\n\n    &lt;androidx.appcompat.widget.AppCompatEditText\n        android:id=\"@+id\/three\"\n        android:layout_width=\"0dp\"\n        android:layout_height=\"wrap_content\"\n        android:background=\"@null\"\n        android:cursorVisible=\"false\"\n        android:inputType=\"numberDecimal\"\n        android:maxLength=\"1\"\n        android:textColor=\"#707070\"\n        android:textSize=\"60sp\"\n        app:layout_constraintEnd_toEndOf=\"@+id\/three_line\"\n        app:layout_constraintStart_toStartOf=\"@+id\/three_line\"\n        app:layout_constraintTop_toTopOf=\"@+id\/two\" \/&gt;\n\n    &lt;View\n        android:id=\"@+id\/three_line\"\n        android:layout_width=\"40dp\"\n        android:layout_height=\"2dp\"\n        android:background=\"#BCBCBC\"\n        app:layout_constraintEnd_toStartOf=\"@+id\/four_line\"\n        app:layout_constraintHorizontal_bias=\"0.5\"\n        app:layout_constraintStart_toEndOf=\"@+id\/two_line\"\n        app:layout_constraintTop_toBottomOf=\"@+id\/three\" \/&gt;\n\n    &lt;androidx.appcompat.widget.AppCompatEditText\n        android:id=\"@+id\/four\"\n        android:layout_width=\"0dp\"\n        android:layout_height=\"wrap_content\"\n        android:background=\"@null\"\n        android:cursorVisible=\"false\"\n        android:inputType=\"numberDecimal\"\n        android:maxLength=\"1\"\n        android:textColor=\"#707070\"\n        android:textSize=\"60sp\"\n        app:layout_constraintEnd_toEndOf=\"@+id\/four_line\"\n        app:layout_constraintStart_toStartOf=\"@+id\/four_line\"\n        app:layout_constraintTop_toTopOf=\"@+id\/three\" \/&gt;\n\n    &lt;View\n        android:id=\"@+id\/four_line\"\n        android:layout_width=\"40dp\"\n        android:layout_height=\"2dp\"\n        android:background=\"#BCBCBC\"\n        app:layout_constraintEnd_toEndOf=\"parent\"\n        app:layout_constraintHorizontal_bias=\"0.5\"\n        app:layout_constraintStart_toEndOf=\"@+id\/three_line\"\n        app:layout_constraintTop_toBottomOf=\"@+id\/four\" \/&gt;\n\n&lt;\/androidx.constraintlayout.widget.ConstraintLayout&gt;\n<\/code><\/pre>\n<p><a id=\"b\"><\/a><\/p>\n<h4>2.\u76e3\u807d\u662f\u5426\u8f38\u5165<\/h4>\n<pre><code class=\"language-Kotlin line-numbers\">binding.one.addTextChangedListener {\n    if (it?.length == 1) {\n        binding.two.requestFocus()\n        binding.oneLine.setBackgroundColor(Color.parseColor(\"#7E7E7E\"))\n    } else {\n        binding.oneLine.setBackgroundColor(Color.parseColor(\"#BCBCBC\"))\n    }\n}\n\nbinding.two.addTextChangedListener {\n    if (it?.length == 1) {\n        binding.three.requestFocus()\n        binding.twoLine.setBackgroundColor(Color.parseColor(\"#7E7E7E\"))\n    } else {\n        binding.twoLine.setBackgroundColor(Color.parseColor(\"#BCBCBC\"))\n    }\n}\n\nbinding.three.addTextChangedListener {\n    if (it?.length == 1) {\n        binding.four.requestFocus()\n        binding.threeLine.setBackgroundColor(Color.parseColor(\"#7E7E7E\"))\n    } else {\n        binding.threeLine.setBackgroundColor(Color.parseColor(\"#BCBCBC\"))\n    }\n}\n\nbinding.four.addTextChangedListener {\n    if (it?.length == 1) {\n        binding.four.clearFocus()\n        SoftKeyboardUtils.showOrHideSoftKeyboard(this)\n        binding.fourLine.setBackgroundColor(Color.parseColor(\"#7E7E7E\"))\n        Toast.makeText(this, \"<span class=\"katex math inline\">{binding.one.text}<\/span>{binding.two.text}<span class=\"katex math inline\">{binding.three.text}<\/span>{binding.four.text}\", Toast.LENGTH_LONG).show()\n    } else {\n        binding.fourLine.setBackgroundColor(Color.parseColor(\"#BCBCBC\"))\n    }\n}\n<\/code><\/pre>\n<p><a id=\"c\"><\/a><\/p>\n<h4>3.\u76e3\u807d\u522a\u9664<\/h4>\n<pre><code class=\"language-Kotlin line-numbers\">binding.two.setOnKeyListener { _, keyCode, event -&gt;\n    if (keyCode == KeyEvent.KEYCODE_DEL &amp;&amp; event.action == KeyEvent.ACTION_UP) {\n        binding.one.requestFocus()\n    }\n    false\n}\nbinding.three.setOnKeyListener { _, keyCode, event -&gt;\n    if (keyCode == KeyEvent.KEYCODE_DEL &amp;&amp; event.action == KeyEvent.ACTION_UP) {\n        binding.two.requestFocus()\n    }\n    false\n}\nbinding.four.setOnKeyListener { _, keyCode, event -&gt;\n    if (keyCode == KeyEvent.KEYCODE_DEL &amp;&amp; event.action == KeyEvent.ACTION_UP) {\n        binding.three.requestFocus()\n    }\n    false\n}\n\nbinding.one.setOnFocusChangeListener { v, hasFocus -&gt;\n    if (hasFocus) {\n        binding.one.text?.clear()\n        binding.two.text?.clear()\n        binding.three.text?.clear()\n        binding.four.text?.clear()\n    }\n}\n\nbinding.two.setOnFocusChangeListener { v, hasFocus -&gt;\n    if (hasFocus) {\n        if (binding.one.text.isNullOrEmpty()) {\n            binding.one.requestFocus()\n            SoftKeyboardUtils.showOrHideSoftKeyboard(this)\n        } else {\n            binding.two.text?.clear()\n            binding.three.text?.clear()\n            binding.four.text?.clear()\n        }\n    }\n}\n\nbinding.three.setOnFocusChangeListener { v, hasFocus -&gt;\n    if (hasFocus) {\n        if (binding.one.text.isNullOrEmpty()) {\n            binding.one.requestFocus()\n            SoftKeyboardUtils.showOrHideSoftKeyboard(this)\n        } else {\n            binding.three.text?.clear()\n            binding.four.text?.clear()\n        }\n    }\n}\n\nbinding.four.setOnFocusChangeListener { v, hasFocus -&gt;\n    if (hasFocus) {\n        if (binding.one.text.isNullOrEmpty()) {\n            binding.one.requestFocus()\n            SoftKeyboardUtils.showOrHideSoftKeyboard(this)\n        } else {\n            binding.four.text?.clear()\n        }\n    }\n}\n<\/code><\/pre>\n<p><a id=\"d\"><\/a><\/p>\n<h4>4.\u7a0b\u5f0f\u78bc\u7bc4\u4f8b<\/h4>\n<pre><code class=\"language-Kotlin line-numbers\">package com.example.codevalidatedemo\n\nimport android.app.Activity\nimport android.content.Context\nimport android.graphics.Color\nimport androidx.appcompat.app.AppCompatActivity\nimport android.os.Bundle\nimport android.text.Editable\nimport android.view.KeyEvent\nimport android.view.View\nimport android.view.inputmethod.InputMethodManager\nimport android.widget.Toast\nimport androidx.core.widget.addTextChangedListener\nimport com.example.codevalidatedemo.databinding.ActivityMainBinding\n\nclass MainActivity : AppCompatActivity() {\n\n    lateinit var binding: ActivityMainBinding\n\n    override fun onCreate(savedInstanceState: Bundle?) {\n        super.onCreate(savedInstanceState)\n\n        binding = ActivityMainBinding.inflate(layoutInflater)\n        setContentView(binding.root)\n        codeListener()\n    }\n\n    private fun codeListener() {\n        clearCode()\n\n        binding.one.addTextChangedListener {\n            if (it?.length == 1) {\n                binding.two.requestFocus()\n                binding.oneLine.setBackgroundColor(Color.parseColor(\"#7E7E7E\"))\n            } else {\n                binding.oneLine.setBackgroundColor(Color.parseColor(\"#BCBCBC\"))\n            }\n        }\n\n        binding.two.addTextChangedListener {\n            if (it?.length == 1) {\n                binding.three.requestFocus()\n                binding.twoLine.setBackgroundColor(Color.parseColor(\"#7E7E7E\"))\n            } else {\n                binding.twoLine.setBackgroundColor(Color.parseColor(\"#BCBCBC\"))\n            }\n        }\n\n        binding.three.addTextChangedListener {\n            if (it?.length == 1) {\n                binding.four.requestFocus()\n                binding.threeLine.setBackgroundColor(Color.parseColor(\"#7E7E7E\"))\n            } else {\n                binding.threeLine.setBackgroundColor(Color.parseColor(\"#BCBCBC\"))\n            }\n        }\n\n        binding.four.addTextChangedListener {\n            if (it?.length == 1) {\n                binding.four.clearFocus()\n                SoftKeyboardUtils.showOrHideSoftKeyboard(this)\n                binding.fourLine.setBackgroundColor(Color.parseColor(\"#7E7E7E\"))\n                Toast.makeText(this, \"<span class=\"katex math inline\">{binding.one.text}<\/span>{binding.two.text}<span class=\"katex math inline\">{binding.three.text}<\/span>{binding.four.text}\", Toast.LENGTH_LONG).show()\n            } else {\n                binding.fourLine.setBackgroundColor(Color.parseColor(\"#BCBCBC\"))\n            }\n        }\n    }\n\n    private fun clearCode() {\n        binding.two.setOnKeyListener { _, keyCode, event -&gt;\n            if (keyCode == KeyEvent.KEYCODE_DEL &amp;&amp; event.action == KeyEvent.ACTION_UP) {\n                binding.one.requestFocus()\n            }\n            false\n        }\n        binding.three.setOnKeyListener { _, keyCode, event -&gt;\n            if (keyCode == KeyEvent.KEYCODE_DEL &amp;&amp; event.action == KeyEvent.ACTION_UP) {\n                binding.two.requestFocus()\n            }\n            false\n        }\n        binding.four.setOnKeyListener { _, keyCode, event -&gt;\n            if (keyCode == KeyEvent.KEYCODE_DEL &amp;&amp; event.action == KeyEvent.ACTION_UP) {\n                binding.three.requestFocus()\n            }\n            false\n        }\n\n        binding.one.setOnFocusChangeListener { v, hasFocus -&gt;\n            if (hasFocus) {\n                binding.one.text?.clear()\n                binding.two.text?.clear()\n                binding.three.text?.clear()\n                binding.four.text?.clear()\n            }\n        }\n\n        binding.two.setOnFocusChangeListener { v, hasFocus -&gt;\n            if (hasFocus) {\n                if (binding.one.text.isNullOrEmpty()) {\n                    binding.one.requestFocus()\n                    SoftKeyboardUtils.showOrHideSoftKeyboard(this)\n                } else {\n                    binding.two.text?.clear()\n                    binding.three.text?.clear()\n                    binding.four.text?.clear()\n                }\n            }\n        }\n\n        binding.three.setOnFocusChangeListener { v, hasFocus -&gt;\n            if (hasFocus) {\n                if (binding.one.text.isNullOrEmpty()) {\n                    binding.one.requestFocus()\n                    SoftKeyboardUtils.showOrHideSoftKeyboard(this)\n                } else {\n                    binding.three.text?.clear()\n                    binding.four.text?.clear()\n                }\n            }\n        }\n\n        binding.four.setOnFocusChangeListener { v, hasFocus -&gt;\n            if (hasFocus) {\n                if (binding.one.text.isNullOrEmpty()) {\n                    binding.one.requestFocus()\n                    SoftKeyboardUtils.showOrHideSoftKeyboard(this)\n                } else {\n                    binding.four.text?.clear()\n                }\n            }\n        }\n    }\n}\n\nobject SoftKeyboardUtils {\n\n    fun showOrHideSoftKeyboard(activity: Activity) {\n        val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager\n        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS)\n    }\n\n    fun showSoftKeyboard(activity: Activity) {\n        val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager\n        imm.showSoftInput(\n            activity.currentFocus,\n            InputMethodManager.SHOW_FORCED\n        )\n    }\n\n    fun hideSoftKeyboard(activity: Activity) {\n        val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager\n        imm.hideSoftInputFromWindow(\n            activity.currentFocus?.windowToken,\n            InputMethodManager.HIDE_NOT_ALWAYS\n        )\n    }\n\n    fun getStateKeyboard(activity: Activity): Boolean {\n        val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager\n        return imm.isActive\n    }\n}\n<\/code><\/pre>\n<p><a id=\"e\"><\/a><\/p>\n<h4>5.\u6548\u679c\u5c55\u793a<\/h4>\n<p><a href=\"https:\/\/badgameshow.com\/fly\/wp-content\/uploads\/2021\/12\/Screenrecorder-2021-12-17-10-12-22-145.gif\"><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/fly\/wp-content\/uploads\/2021\/12\/Screenrecorder-2021-12-17-10-12-22-145.gif\" width=\"40%\"\/><\/a><\/p>\n<p><a id=\"f\"><\/a><\/p>\n<h4>6.Github<\/h4>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/github.com\/MuHongWeiWei\/CodeValidateDemo\" target=\"_blank\" rel=\"noopener\">Android \u56db\u683c\u9a57\u8b49\u78bc\u6216\u5bc6\u78bc Github<\/a><\/p>\n\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>Android \u56db\u683c\u9a57\u8b49\u78bc\u6216\u5bc6\u78bc \u56db\u683c\u9a57\u8b49\u78bc\u9700\u8981\u62c6\u5206\u70ba\u56db\u500bEditText\u8f38\u5165\u6846\uff0c\u4e26\u4e14\u505a\u4e00\u4e9b\u5224\u65b7\u4f86\u63a7\u5236\u8df3\u4e0b\u4e00\u683c &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[5],"tags":[13,211,15],"class_list":["post-1560","post","type-post","status-publish","format-standard","hentry","category-android","tag-android","tag-code","tag-kotlin"],"_links":{"self":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/1560","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/comments?post=1560"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/1560\/revisions"}],"predecessor-version":[{"id":1563,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/1560\/revisions\/1563"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/media?parent=1560"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/categories?post=1560"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/tags?post=1560"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}