在xml中可以设置组件的焦点,有这些属性:
android:selectAllOnFocus="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:nextFocusLeft=""
android:nextFocusRight=""
android:nextFocusUp=""
android:nextFocusDown="@id/textView3"
后面四个表示,按上下左右键时,把焦点交给id为*****的组件。
在代码中可以设置焦点的相关方法和监听:
tv.setFocusable(true);
tv.setFocusableInTouchMode(true);
tv.requestFocus();
tv.requestFocus(direction);
tv.requestFocusFromTouch();
tv.clearFocus();
tv.isFocusable();
tv.addFocusables(views, direction);
tv.isFocusableInTouchMode();
tv.isFocused();
tv.isFocusable();
tv.focusSearch(0);
tv.findFocus();
tv.getFocusables(0);
tv.getFocusedRect(r);
tv.getNextFocusLeftId();
tv.getNextFocusRightId();
tv.getNextFocusDownId();
tv.getNextFocusUpId();
tv.getOnFocusChangeListener();
tv.hasFocus();
tv.hasFocusable();
tv.hasWindowFocus();
tv.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
}
});
关于PopuWindow利用键盘来消失的方法:
在网上找到一个很好的案例。
PopupWindow在设置了焦点之后,那么这个弹出窗口就是当前操作的窗口,如果想要在此时响应按键事件,就需要在PopupWindow上注册键盘事件来响应。但是PopupWindow好像只有一个OnDismissListener,怎么来注册键盘事件监听呢?
可以利用PopupWindow里的最外层View,对该子View监听键盘事件setOnKeyListener即可。
popupwindow.setBackgroundDrawable(new BitmapDrawable());
popupwindow.setFocusable(true);
popupwindow.setFocusableInTouchMode(true);
childrenView.setOnKeyListener(new OnKeyListener()
{
@Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK)
{
Log.v("keyCode", "/" + keyCode);
if(popupwindow != null) {
popupwindow.dismiss();
popupwindow = null;
}
}
return false;
}
});
Gravity是设置内容对它本身的位置的
layout_gravity是设置它本身在什么位置的