您的位置:首页 > 其它

ListView里面有EditTextView,解决弹出键盘后EditView不能获取焦点的问题

2014-12-11 13:53 731 查看
虽然ListView里面动态生成EditTextView不是Android UI Friendly, 但是需求就是这样,那就硬着头皮去做了

遇到的问题是点击EditText,键盘弹出后,输入光标会消失,需要再点击一次才能获取光标,这个大概是因为listview不能很好的处理EditText作为item的情况,弹出键盘后应该view是重新生成的。

折腾了一天,解决方案如下:

布局文件(nothing special):

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

    <FrameLayout
        android:id="@+id/booking1_frameLayout"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:background="@drawable/header" >

        <TextView
            style="@style/title_text_style"
            android:text="@string/booking2" />
    </FrameLayout>

    <ListView
        android:id="@+id/book2_listview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
         android:layout_below="@id/booking1_frameLayout"
        android:divider="@null"
        android:paddingLeft="10dp"
        android:paddingRight="10dp" />

</RelativeLayout>


关键代码:

在listView的adapter的getView方法里:

//点击时记录下现在点击的是哪个EditText,也就是在编辑哪一个EditText
viewHolderChild.name.setOnTouchListener(new OnTouchListener() {

				@Override
				public boolean onTouch(View v, MotionEvent event) {
					if (event.getAction() == MotionEvent.ACTION_UP) {
						touchedPosition = position;
					}
					return false;
				}
			});
			
			if (touchedPosition == position) {
				// 如果当前的行下标和点击事件中保存的index一致,手动为EditText设置焦点。
				viewHolderChild.name.requestFocus();

			}else {
				viewHolderChild.name.clearFocus(); 
			}


主要是对listview的机制还不很了解。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐