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);
}
}
});
原文:http://www.cnblogs.com/code4app/p/3810244.html