PopupWindow重叠与虚拟键盘

时间:2018-05-08 06:05:13

标签: java android kotlin popupwindow

我在显示PopupWindow时出现问题,弹出窗口与键盘重叠。在这里我的结果

enter image description here

这是我的弹出代码:

class NoteHintList(val view: View, val listener: MenuDetailFragment.OnItemListClickListener?){
    var noteHintList: List<String> = ArrayList<String>()
    val popupWindow = PopupWindow(view.context)

    fun setNoteList(noteHint: List<String>){
        this.noteHintList = noteHint
    }

    // ============ TODO: Pop up note hint ================================================================
    fun popupWindow(): PopupWindow {
        val popUpContents = arrayOfNulls<String>(noteHintList.size)
        ArrayList(noteHintList).toArray(popUpContents)

        val listView = ListView(view.context)
        listView.adapter = noteHintAdapter(popUpContents)

        // set the item click listener
        listView.setOnItemClickListener { parent, view, position, id ->
            listener!!.onItemListClickListener(listView.getItemAtPosition(position).toString())
        }

//        val params: LinearLayout.LayoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
//        params.setMargins(16,0,16,0)


        popupWindow.setFocusable(false)
        popupWindow.contentView = view
//        popupWindow.setWidth(WindowManager.LayoutParams.MATCH_PARENT - 32)
//        popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT)
        popupWindow.setContentView(listView)

        return popupWindow
    }

    // ============ TODO: Popup list adapter for pop up ===================================================
    private fun noteHintAdapter(array: Array<String?>): ArrayAdapter<Any> {

        return object : ArrayAdapter<Any>(view.context, android.R.layout.simple_list_item_1, array) {

            override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {

                // setting the ID and text for every items in the list

                val text = getItem(position)!!.toString()
                // visual settings for the list item
                val listItem = TextView(context)
                listItem.setText(text)
                listItem.tag = position
                listItem.textSize = 22f
                listItem.setPadding(10, 10, 10, 10)
                listItem.setTextColor(Color.WHITE)
                return listItem
            }
        }
    }
}

弹出文件将在文本更改时显示。这是我的代码

txtHint.addTextChangedListener(object : TextWatcher {
     override fun afterTextChanged(s: Editable?) {
         val text = txtHint.text.toString()
         if (Pattern.compile("^\\s+$").matcher(text).find() || text == "") {
             popupNoteHintList!!.setNoteList(noteHintAllList)
         } else if (text.split(",").size > 0) {
              val textSplit = text.split(",")
              val myData = noteHintAllList.filter { s ->
                   s.toLowerCase().contains(textSplit[textSplit.size - 1].toLowerCase().replace(Regex("^ | \$"), "")) &&
                   s.toLowerCase() != textSplit[textSplit.size - 1].toLowerCase().replace(Regex("(^ +)|( +$)"), "")
              }
         popupNoteHintList!!.setNoteList(myData)
         if (myData.size > 0) {
             popupNoteHintList!!.popupWindow().showAsDropDown(txtHint, 0, 0)
         }
     }
 }

 override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
     popupNoteHintList!!.popupWindow().dismiss()
 }

 override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
      popupNoteHintList!!.popupWindow().dismiss()
 }

})

如何使弹出窗口不与键盘重叠,如果重叠,可能会移到EditText以上,但如果不重叠,则仍然在EditText

所以结果如下:

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以使用afterTextChanged方法隐藏软键盘,以便在弹出窗口时始终隐藏键盘。

afterTextChanged方法中,添加以下内容以隐藏键盘:

val imm = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0) //Hide soft keyboard.