0

The code is very simply. There is an EditText on headerView and an EditText on footerView, make the items' height + headerView's height + footerView's height = the screen's height, after you click the EditText on HeaderView, then click the EditText on FooterView, the bug will appear. Anyone can help me explain the problem, I will appreciate it very much, thank you.

class MainActivity : AppCompatActivity() {
    @SuppressLint("ClickableViewAccessibility")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val listView = findViewById<ListView>(R.id.listView)
        listView.adapter = ArrayAdapter(this,
                android.R.layout.simple_list_item_1,
                arrayListOf("1", "2", "3", "4", "5", "6","1", "2", "3", "4", "5", "6"))
        val headerView = LayoutInflater.from(this).inflate(R.layout.header, listView, false)
        val etHeader = headerView.findViewById<EditText>(R.id.etComment)
        listView.addHeaderView(headerView)

        val footerView = LayoutInflater.from(this).inflate(R.layout.footer, listView, false)
        val etFooter = footerView.findViewById<EditText>(R.id.etComment)
        listView.addFooterView(footerView)

        etHeader.setOnFocusChangeListener { v, hasFocus ->
            Log.e("", "etHeader->setOnFocusChangeListener: $hasFocus")
        }
        etFooter.setOnFocusChangeListener { v, hasFocus ->
           Log.e("", "etFooter->setOnFocusChangeListener: $hasFocus")
        }

    }
}

The whole code is here: https://github.com/tuchangwei/EditTextIssue.

Changwei
  • 672
  • 1
  • 5
  • 16

1 Answers1

0

This is a focus preemption problem. Add the following code.

 etFooter.setOnFocusChangeListener { v, hasFocus ->
        Log.e("", "etFooter->setOnFocusChangeListener: $hasFocus")
        if(hasFocus){
            etFooter.setFocusable(true)
            etFooter.setFocusableInTouchMode(true)
            etFooter.requestFocus()
            etFooter.findFocus()
            val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            imm.showSoftInput(etFooter, InputMethodManager.SHOW_FORCED);
        }
    }
Sunshine
  • 61
  • 2
  • thank you for your reply, but it only works one time. If you click the Back button to make the keyboard disappear, then you click the footerView to make the keyboard show, the question comes back. – Changwei Jan 19 '21 at 07:04