在尝试使用ListView内的EditTexts创建接口失败后,我可以对你说“不要!”.当你有足够的物品需要滚动你的ListView时,你会遇到更多麻烦,焦点会在这里跳转,你必须保存EditText的状态等等.似乎一般的想法是在ListView中使用EditText是不值得的.
经过相当多的研究,我可以建议以下方法帮助我:
我继承了ListView并覆盖了layoutChildren方法,在其中我执行以下操作:
@Override
protected void layoutChildren() {
super.layoutChildren();
final EditText tv = (EditText)this.getTag();
if (tv != null)
{
Log.d("RUN", "posting delayed");
this.post(new Runnable() {
@Override
public void run() {
Log.d("RUN", "requesting focus in runnable");
tv.requestFocusFromTouch();
tv.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN , tv.getWidth(), tv.getHeight(), 0));
tv.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP , tv.getWidth(), tv.getHeight(), 0));
}
});
}
}
当我知道哪个EditText应该得到焦点时(我知道调用我的适配器getView时)我将这个特定的EditText设置为ListView的标签.然后ListView自行布局,我的帖子线程排队.它运行并请求焦点,但是因为在我的情况下还不够,我还生成了两个简单模拟点击的MotionEvent.显然,这足以使软键盘出现.
这背后的原因在这里的答案中解释:
Android Actionbar Tabs and Keyboard Focus