Simulating User Touch Action on EditText in Listview

I have a ListView with an invisible EditText and a clickable TextView in each listview item. I am trying to implement a behaviour where clicking on the TextView will make the EditText visible, focusing on the EditText and make the soft keyboard appear automatically.

I am using adjustResize as my input method instead of adjustPan because apparently the S3 i am testing the feature on goes bonkers with adjustPan . (Specifically, when soft keyboard appears, the whole view gets pushed up, thats great. But, as soon as i start typing, the whole view slide back down as if the soft keyboard isnt there! But it is! Problem doesnt occur with HTC One). The listview also has afterDecendents .

When the listview has more items that can fit into the screen entirely, a problem occurs where the softkeyboard triggered is not responsive to the editText.

I programatically trigger the softkeyboard with imm.showSoftInput(holder.editText,0) and the entire view re-adjust itself to accomodate the softkeyboard. Once the softkeyboard is shown and the view completed its relayout, all key entries except backspace are not received by the edittext. That means i cannot add any words or numbers to the edittext but i can delete them, which is weird as hell.

Another funny thing is, I realized that if i do not programatically show the soft keyboard and have the user tap on the visible edittext to reveal the softkeyboard, everything works fine. (Which also baffles me because I suspected the listview recycling and focusing mechanism is the one causing all this problems. Where softkeyboard appears, causes listview to relayout hence recycling of views so reference to the editText is lost etc.)

So, ultimately, my question is, is there any way for me to reliably programatically simulate a user tap on the visible edit text so that i can trigger a softkeyboard that works properly with the "clicked" edittext?

I have tried the following method too and it doesnt work reliably (sometimes it works, sometimes it doesnt. Playing with the delay duration doesnt do anything.).

holder.editText.postDelayed(new Runnable(){
    @Override
    public void run() {
         holder.editText.requestFocus();
         holder.editText.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN , 0, 0, 0));
         holder.editText.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP , 0, 0, 0));
         }
}}, 500);

Other permutation of answers i have tried from many SO questions also didnt really work reliably. Example questions: How to show soft-keyboard when edittext is focused , How come the Android soft keyboard is not responding to EditText? , Android: Dialog box show soft keyboard automatically when focus is on an EditText not working


Q Is there any way for me to reliably programatically simulate a user tap on the visible edit text

so that i can trigger a softkeyboard that works properly with the "clicked" edittext?

Yes,

Add this when you want simulate your click on the EditText

          editText.performClick();    //softkeyboard will open
          editText.setPressed(true);  // edittext will get highlighted
          editText.invalidate();        //Invalidate the whole view

Done!!

链接地址: http://www.djcxy.com/p/24234.html

上一篇: Android RecyclerView Edittext问题

下一篇: 在ListView中模拟EditText上的用户触摸操作