前几天碰到一个需求,要求当按下back键时先隐藏ListView,然后才是收键盘(焦点在EditText中)。
于是我在Acitivity的dispatchKeyEvent中添加了对相应KeyCode的处理,但是发现实际效果却是ListView和键盘一起收掉了。调试发现在进这个函数之前,键盘就已经收掉了。于是上网查阅相关资料,得知要重写Layout的根节点。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/refresh_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
这是一个简单的示例Layout。我们需要写一个新的Layout继承LinearLayout
public class EventLinearLayout extends LinearLayout {
@Override
public boolean dispatchKeyEventPreIme(KeyEvent event) {
return super.dispatchKeyEventPreIme(event);
}
}
然后重写dispatchKeyEventPreIme函数。然后记得修改你的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<com.lily.EventLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/refresh_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</<com.lily.EventLinearLayout>
这样就OK了。