我有以下观点:
当我单击其中一个编辑文本时,将出现一个软键盘。关闭此键盘后,单击后我的导航后退按钮不起作用。在单击其中一种编辑文本之前,我的后退按钮可以正常工作。
“验证”片段的代码:
class VerifyPhoneFragment : Fragment() {
lateinit var appContext: Context
private var editTexts: ArrayList<EditText> = ArrayList()
private var focusedElement: Int = 0
private var verifyButton: TextView? = null
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_verify_phone, container, false)
val backButton = view.findViewById<TextView>(R.id.backButtonAbout)
backButton.setOnClickListener {
activity!!.onBackPressed()
}
verifyButton = view.findViewById(R.id.btn_verify)
verifyButton?.setOnClickListener {
val dataConnection = DataConnection()
var code: String = ""
editTexts.forEach {
code += it.text.toString()
}
dataConnection.doGet("/phone/verification",
hashMapOf("token" to code),
{ result: Any?, message: String ->
activity!!.runOnUiThread {
Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
if(result != null && result is Boolean && result)
activity!!.onBackPressed()
}
}, Boolean::class.java)
}
editTexts.add(view.findViewById(R.id.editText1))
editTexts.add(view.findViewById(R.id.editText2))
editTexts.add(view.findViewById(R.id.editText3))
editTexts.add(view.findViewById(R.id.editText4))
editTexts.add(view.findViewById(R.id.editText5))
editTexts.add(view.findViewById(R.id.editText6))
editTexts.forEach { et ->
et.isEnabled = false
et.setOnKeyListener(object : View.OnKeyListener {
override fun onKey(v: View, keyCode: Int, event: KeyEvent): Boolean {
if (keyCode == KeyEvent.KEYCODE_DEL && event.action !=KeyEvent.ACTION_DOWN) {
if((focusedElement > 0 && focusedElement != 5) || (focusedElement == 5 && editTexts[focusedElement].text.toString().length == 0)) {
focusedElement--
editTexts[focusedElement].isEnabled = true
editTexts[focusedElement].requestFocus()
editTexts[focusedElement].setText("")
editTexts[focusedElement+1].isEnabled = false
}
else if(focusedElement == 5 && editTexts[focusedElement].text.toString().length != 0){
editTexts[focusedElement].setText("")
}
}
else if(keyCode >= 7 && keyCode <= 16 && event.action !=KeyEvent.ACTION_DOWN){
if(focusedElement < 5)
{
editTexts[focusedElement].setText((keyCode - 7).toString())
focusedElement++
editTexts[focusedElement].isEnabled = true
editTexts[focusedElement].requestFocus()
editTexts[focusedElement-1].isEnabled = false
}
else {
editTexts[focusedElement].setText((keyCode - 7).toString())
editTexts[focusedElement].clearFocus()
hideKeyboard(editTexts[focusedElement])
}
}
checkIfVerifyShouldBeEnabled()
return true
}
})
}
editTexts[focusedElement].isEnabled = true
return view
}
private fun hideKeyboard(view: View) {
val imm = view.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(view.windowToken, 0)
}
private fun checkIfVerifyShouldBeEnabled(){
if(editTexts[5].text.toString().length > 0) {
verifyButton?.isEnabled = true
verifyButton?.setTextColor(resources.getColor(R.color.white))
}
else{
verifyButton?.isEnabled = false
verifyButton?.setTextColor(resources.getColor(R.color.grey))
}
}
override fun onAttach(context: Context?) {
super.onAttach(context)
appContext = context!!
}
}
此片段的布局代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/background_payment_1">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar6"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:contentInsetStart="0dp">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/backButtonAbout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="10dp"
android:includeFontPadding="false"
android:text="@string/cancel"
android:textColor="@color/white"
android:textSize="19sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.3" />
<TextView
android:id="@+id/btn_verify"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginEnd="10dp"
android:includeFontPadding="false"
android:text="@string/verify"
android:textColor="@color/grey"
android:textSize="19sp"
android:enabled="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.3" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.Toolbar>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="80dp"
android:text="@string/verification_code"
android:textColor="@color/white"
style="@style/ToolBarHeaderText"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.49"
app:layout_constraintVertical_chainStyle="spread" />
<TextView
android:id="@+id/textView4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="@string/message_phone_verification"
android:textAlignment="center"
android:textSize="18sp"
android:textColor="@color/white"
app:layout_constraintEnd_toEndOf="@+id/linearLayout"
app:layout_constraintStart_toStartOf="@+id/linearLayout"
app:layout_constraintTop_toBottomOf="@+id/textView3" />
<android.support.constraint.ConstraintLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="45dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="45dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView4">
<android.support.v7.widget.CardView
android:id="@+id/cv1"
android:layout_width="40dp"
android:layout_height="50dp"
android:backgroundTint="#76FFFFFF"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/cv2"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:cardCornerRadius="5dp"
app:cardElevation="0dp">
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@color/white"
android:background="@color/transparent"
android:textAlignment="center"
android:inputType="number"/>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/cv2"
android:layout_width="40dp"
android:layout_height="50dp"
android:backgroundTint="#76FFFFFF"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/cv3"
app:layout_constraintStart_toEndOf="@+id/cv1"
app:layout_constraintTop_toTopOf="parent"
app:cardCornerRadius="5dp"
app:cardElevation="0dp">
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@color/white"
android:background="@color/transparent"
android:textAlignment="center"
android:inputType="number"/>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/cv3"
android:layout_width="40dp"
android:layout_height="50dp"
android:backgroundTint="#76FFFFFF"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/cv4"
app:layout_constraintStart_toEndOf="@+id/cv2"
app:layout_constraintTop_toTopOf="parent"
app:cardCornerRadius="5dp"
app:cardElevation="0dp">
<EditText
android:id="@+id/editText3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@color/white"
android:background="@color/transparent"
android:textAlignment="center"
android:inputType="number"/>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/cv4"
android:layout_width="40dp"
android:layout_height="50dp"
android:backgroundTint="#76FFFFFF"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/cv5"
app:layout_constraintStart_toEndOf="@+id/cv3"
app:layout_constraintTop_toTopOf="parent"
app:cardCornerRadius="5dp"
app:cardElevation="0dp">
<EditText
android:id="@+id/editText4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@color/white"
android:background="@color/transparent"
android:textAlignment="center"
android:inputType="number"/>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/cv5"
android:layout_width="40dp"
android:layout_height="50dp"
android:backgroundTint="#76FFFFFF"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/cv6"
app:layout_constraintStart_toEndOf="@+id/cv4"
app:layout_constraintTop_toTopOf="parent"
app:cardCornerRadius="5dp"
app:cardElevation="0dp"
android:inputType="number">
<EditText
android:id="@+id/editText5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@color/white"
android:background="@color/transparent"
android:textAlignment="center"
android:inputType="number"/>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/cv6"
android:layout_width="40dp"
android:layout_height="50dp"
android:backgroundTint="#76FFFFFF"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/cv5"
app:layout_constraintTop_toTopOf="parent"
app:cardCornerRadius="5dp"
app:cardElevation="0dp">
<EditText
android:id="@+id/editText6"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@color/white"
android:background="@color/transparent"
android:textAlignment="center"
android:inputType="number"/>
</android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
所以我的问题是,如何获得导航后退按钮的焦点?还是根本就没有焦点问题? 顺便说一句,取消按钮始终可以正常工作!
答案 0 :(得分:1)
我发现了问题。当用户手动关闭键盘时,焦点仍位于当前编辑文本上。因此,当他然后按下导航后退按钮时,会触发onKey事件,但我没有处理后退键事件。 所以我只添加了以下代码,一切正常:
else if(keyCode == KeyEvent.KEYCODE_BACK && event.action != KeyEvent.ACTION_DOWN){
activity?.onBackPressed()
}