如何在列表视图中隐藏键盘?

How to hide a keyboard in a listview?

当我在列表视图的 editText 中输入数字时,qwerty 键盘仍然显示。

请查看此动画 GIF https://ibb.co/4K9dz1N 以了解问题所在。 (请点击“lire le GIF”启动动画 GIF)。

我已经实现了在 Whosebug 中找到的两个函数(一个带有 hidekeyboard 函数的 setOnItemClickListener)但是没有结果...

这是我的应用程序的代码。

主要 activity :

import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.view.inputmethod.InputMethodManager
import android.widget.AdapterView.OnItemClickListener
import android.widget.EditText
import android.widget.ListView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity


class MainActivity : AppCompatActivity() {
    lateinit var textView2: TextView
    lateinit var listView: ListView

    //var adapter: MyAdapter? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "KotlinApp"
        listView = findViewById(R.id.listView)
        textView2 = findViewById(R.id.textView2)



        listView.adapter=CustomAdapter(DataClass(),this)



        var textView2: TextView
        textView2 = findViewById(R.id.textView2)
        var editText : EditText


        listView.setOnItemClickListener({ parent, view, position, id -> hideKeyboard(this) })






    }



    fun hideKeyboard(context: Context) {
        try {
            val inputManager: InputMethodManager =
                context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            val view = (context as Activity).currentFocus
            if (view != null) {
                inputManager.hideSoftInputFromWindow(
                    view.windowToken,
                    InputMethodManager.HIDE_NOT_ALWAYS
                )
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }




    fun Update()
    {
    }


    fun fonction ()
    {
        var textView2: TextView

    }


    fun fonction2 ()
    {
        var textView2: TextView
       // textView2 = findViewById(R.id.textView)
        //textView2.text="?????"
    }

    private fun generateData(): List<String> {
        val data: MutableList<String> = ArrayList()
        for (i in 0..99) {
            data.add(i.toString() + "th Element")
        }
        return data
    }
}

class DataClass(){
    var list1: MutableList<String> = generateData2() as MutableList<String>
    var list2: MutableList<String> = generateData3() as MutableList<String>
    var list3: MutableList<String> = generateData4() as MutableList<String>

    private fun generateData2(): List<String> {
        val data: MutableList<String> = mutableListOf()
        for (i in 0..99) {
            data.add(i.toString())
        }
        return data
    }

    private fun generateData3(): List<String> {
        val data: MutableList<String> =mutableListOf()
        for (i in 0..99) {
            data.add("")
        }
        return data
    }

    private fun generateData4(): List<String> {
        val data: MutableList<String> = mutableListOf()
        for (i in 0..99) {
            data.add("")
        }

        return data
    }

}

自定义适配器

import android.app.Activity
import android.content.Context
import android.os.Handler
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import android.widget.BaseAdapter
import android.widget.EditText
import android.widget.TextView


//Class MyAdapter
class CustomAdapter(private val data: DataClass, private val activity: MainActivity) : BaseAdapter() {

    override fun getCount(): Int {
        return data.list1.size
    }
    override fun getItem(position: Int): Any {
        return position
    }
    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    fun fonction3(position: Int) {
        if(data.list3[position ]!=""){
        data.list2[position]="Input"}
        var plage1: Int = 0
        var plage2: Int = 0
        var bornesuivante: Int = 0
        var borneprécédente: Int = 0
        //if (position >= 0 && position <=100) {
            var j: Int = 1
            var k: Int = 1
            //recherche vers le bas du prochain data non nul


            loop1@ for (i in 1..data.list3.size-position-1) {
                if (data.list2[position + i] == "") {//Log.i("tag",j.toString())
                    //Log.i("tag",(position+i).toString())

                    j = j + 1
                } else {

                    var bornesuivante = j + position

                    plage1 = bornesuivante - position
                    break@loop1
                }
            }
        Log.i("plage1 ",plage1 .toString())

            loop2@  for (i in 1..position) {

                    if (data.list2[position - i] == "") {//Log.i("tag",j.toString())
                        //Log.i("tag",(position+i).toString())

                        k = k + 1
                    } else {

                        var borneprécédente = position - k

                        plage2 = position - borneprécédente
                        break@loop2

                }
           }

        Log.i("plage2 ",plage2 .toString())
            /*Log.i("position", position.toString())
            Log.i("borne précédente", borneprécédente.toString())
            Log.i("plage 2", plage2.toString())
            // data.list3[position] = 2.toString()//bornesuivante.toString()
            Log.i("tag", data.list2[position].toString())
            Log.i("tag", data.list2[bornesuivante].toString())
            Log.i("tag", bornesuivante.toString())*/

//if (data.list2[position] !="0" && data.list2[bornesuivante] !="0") {
            for (k in 1..plage1 - 1) {

                data.list3[position + k] = ((plage1 - k) * (data.list3[position].toDouble()) + k * data.list3[position + plage1].toDouble()).toString()
                data.list3[position + k] = (data.list3[position + k].toDouble() / (plage1)).toInt().toString()
                Log.i("k",k.toString())
                Log.i("plage1",plage1.toString())
                Log.i("data position",data.list3[position].toString())
                Log.i("data position+plage1",data.list3[position+plage1].toString())

//a=true                // }
            }

            for (k in 1..plage2 - 1) {

                data.list3[position - k] = ((plage2 - k) * (data.list3[position].toDouble()) + k * data.list3[position-plage2 ].toDouble()).toString()
                data.list3[position - k] = (data.list3[position - k].toDouble() / (plage2)).toInt().toString()

                //Log.i("tag6", (data.list3[position + k].toDouble()/(plage)).toString())
                //Log.i("tag7","!!!!")
//a=true                // }
            }


            // data.list2[position] = (0.5 * (data.list2[position - 1].toDouble() + data.list2[position + 1].toDouble())).toString()

            //Log.i("tag",bornesuivante.toString())
            //activity.Update()
            //   }

            // Log.i("tag",a.toString())
            //return a
        }


  //  }
        override fun getView(position: Int, convertView: View?, parent: ViewGroup): View? {
            val convertView: View? = LayoutInflater.from(parent.context).inflate(R.layout.row, parent, false)

            val Edit1: EditText = convertView!!.findViewById(R.id.Edit1)
            val Edit2: EditText = convertView.findViewById(R.id.Edit2)
            val textView: TextView = convertView.findViewById(R.id.textView)
            val textView3: TextView = convertView.findViewById(R.id.textView3)
            Edit1.setText(data.list1[position])
            Edit2.setText(data.list3[position])
            //textView.setText(data.list3[position])
            textView3.setText(data.list3[position])
            Edit1.addTextChangedListener(object : TextWatcher {

                override fun afterTextChanged(p0: Editable?) {
                    data.list1[position] = Edit1.text.toString()
                    //Edit1.setText(data.list1[position])
                    //textView.setText(data.list3[position])
                    // activity.fonction()
                }

                override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                }

                override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                }
            })


            Edit2.addTextChangedListener(object : TextWatcher {

                override fun afterTextChanged(p0: Editable?) {
                    Handler().postDelayed({
                        data.list3[position] = Edit2.text.toString()



                    if (!Edit2.text.toString().isBlank()) {
                        fonction3(getItem(position) as Int)

                        //Edit2.setText(data.list2[position])
                        //activity.fonction2()

                        //textView.setText(data.list3[position])
                        Handler().postDelayed({
                            notifyDataSetChanged()
                        }, 100)
                    }}, 1000)
                    //textView3.setText(data.list3[position])
                }

                override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

                }

                override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

                }
                //  })
            })

            return convertView


        }


    }

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="8dp"

    android:orientation="vertical">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="611dp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView" />
</LinearLayout>

row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="4dp">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/Edit1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="true"
        android:hint="hint" />

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/Edit2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="true"
        android:hint="hint"
        android:inputType="numberSigned"
        android:numeric="integer" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="32dp"
        android:layout_weight="1"
        android:singleLine="true"
        android:imeActionLabel="Done"
        android:imeOptions="actionDone"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:singleLine="true"
        android:imeActionLabel="Done"
        android:imeOptions="actionDone"
        android:text="TextView" />
</LinearLayout>

感谢您的帮助

将以下代码添加到您的 RecyclerViewListView 的父视图中

android:descendantFocusability="blocksDescendants"
android:focusable="false"
android:focusableInTouchMode="false"

在您的编辑文本 xml 中添加此 android:imeOptions="actionDone"。这将在完成按钮上隐藏键盘。