问题是,“继续输入”部分有效,但“单击项目”部分无效,因为ListView的OnItemClickListener从未被调用过,我无法弄清楚原因。
这是我的activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ddd"
android:focusableInTouchMode="true"
tools:context="com.example.popuptest3.MainActivity" >
<EditText
android:id="@+id/edtPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone" />
</RelativeLayout>
我的 popup_window.xml
:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff" >
<ListView
android:id="@+id/lstItems"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="60dp" />
<Button
android:id="@+id/btnDismiss"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Dismiss" />
</RelativeLayout>
My list_item.xml
:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp" >
<TextView
android:id="@+id/txtItem"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="Item Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
在我的
MainActivity
类中,我有以下的准备代码:private PopupWindow popup = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prepareControls();
}
private void prepareControls() {
final EditText edtPhone = (EditText)findViewById(R.id.edtPhone);
edtPhone.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus)
openPopup(edtPhone);
else
closePopup();
}
});
}
也就是说,当edtPhone
获得焦点时,我打开PopupWindow
,当edtPhone
失去焦点时,我关闭PopupWindow
。然后我有:
private void openPopup(View parent) {
closePopup();
// Inflate the view of the PopupWindow.
LayoutInflater layoutInflater = LayoutInflater.from(this);
View view = layoutInflater.inflate(R.layout.popup_window,
new LinearLayout(this), false);
final Activity activity = this;
// Prepare the ListView.
ListView lstItems = (ListView)view.findViewById(R.id.lstItems);
lstItems.setAdapter(new ListAdapter(this));
lstItems.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
hideKeyboardAndClearFocus(activity);
}
});
// Prepare the Button.
Button btnDismiss = (Button)view.findViewById(R.id.btnDismiss);
btnDismiss.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
hideKeyboardAndClearFocus(activity);
}
});
// Create and show the PopupWindow.
popup = new PopupWindow(view, 400, 300, false);
popup.showAtLocation(parent, Gravity.LEFT | Gravity.TOP, 0, 60);
}
btnDismiss
按钮不是我的计划的一部分,我在这里添加它仅用于演示目的。最后我有:
private void closePopup() {
if (popup != null) {
popup.dismiss();
popup = null;
}
}
private static void hideKeyboardAndClearFocus(Activity activity) {
InputMethodManager manager = (InputMethodManager)activity.
getSystemService(Activity.INPUT_METHOD_SERVICE);
manager.hideSoftInputFromWindow(
activity.getCurrentFocus().getWindowToken(), 0);
activity.getWindow().getDecorView().clearFocus();
}
我认为这不需要解释,ListAdapter
类相当标准,因此我省略了它。
现在,当我点击 edtPhone
时,它会获得焦点,软键盘会弹出,PopupWindow
会打开。我可以在 edtPhone
中输入一些内容,当我点击 btnDismiss
时,edtPhone
失去焦点,软键盘关闭,PopupWindow
关闭,所有操作都正常。但是,如果我点击 lstItems
上的一个项目而不是 btnDismiss
,则什么也不会发生。使用 Log
我可以看到 OnItemClickListener
没有被调用。有趣的是,ListView
可以滚动,但无法点击。
我知道这篇文章,但那个解决方案对我的目的没有用。如果我更改这一行:
popup = new PopupWindow(view, 400, 300, false);
to the following:
popup = new PopupWindow(view, 400, 300, true);
PopupWindow
变得可聚焦,ListView
可点击,很好,但是我就不能在 edtPhone
中继续输入了。实际上,如果在该活动中还有其他控件,它们也都无法访问,就像 PopupWindow
是“模态”的一样。
我尝试了 android:focusable="false"
,但仍没有成功。而且我不需要自动完成,所以 AutoCompleteTextView
不是我需要的。
那么为什么非可聚焦的 PopupWindow
上的 OnItemClickListener
没有被调用呢?我做错了什么吗?