读书人

listView焦点有关问题

发布时间: 2012-08-24 10:00:20 作者: rapoo

listView焦点问题

public void onItemSelected(AdapterView<?> listView, View view, int position, long id)?
{?
? ? if (position == 1)?
? ? {?
? ? ? ? // listView.setItemsCanFocus(true);?
?
? ? ? ? // Use afterDescendants, because I don't want the ListView to steal focus?
? ? ? ? listView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);?
? ? ? ? myEditText.requestFocus();?
? ? }?
? ? else?
? ? {?
? ? ? ? if (!listView.isFocused())?
? ? ? ? {?
? ? ? ? ? ? // listView.setItemsCanFocus(false);?
?
? ? ? ? ? ? // Use beforeDescendants so that the EditText doesn't re-take focus?
? ? ? ? ? ? listView.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);?
? ? ? ? ? ? listView.requestFocus();?
? ? ? ? }?
? ? }?
}?
?
public void onNothingSelected(AdapterView<?> listView)?
{?
? ? // This happens when you start scrolling, so we need to prevent it from staying?
? ? // in the afterDescendants mode if the EditText was focused ?
? ? listView.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);?
}?

有时候根据需要 <ListView?
? ? android:id="@android:id/list" ?
? ? android:layout_height="fill_parent" ?
? ? android:layout_width="fill_parent"?
? ? android:descendantFocusability="beforeDescendants"?
? ? />?
主要是 android:descendantFocusability属性的使用

读书人网 >移动开发

热点推荐