android中,要使控件获得焦点,需要先setFocus,再requestFocus。
以Button为例:
btn.setFocusable(true);
btn.setFocusableInTouchMode(true);
btn.requestFocus();
btn.requestFocusFromTouch();
//获得失去焦点的监听器
btn.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if (hasFocus) {
btn_box.setBackgroundResource(R.drawable.book_green);
}else {
btn_box.setBackgroundResource(R.drawable.book);
}
}
});
android中设置控件获得焦点
时间: 2024-10-15 01:58:41