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中设置控件获得焦点,布布扣,bubuko.com
原文地址:http://www.cnblogs.com/code4app/p/3810244.html