目前本人在做Android机顶盒开发,在做登录界面的时候发现了一个问题,填写了账号密码后点击登录,正常情况下本应该等待网络服务器的验证然后给出应答,但是在验证过程中点击遥控器的按键,发现停留在登录按钮的焦点会转移~~~
然而这种问题肯定是不允许存在的,在等待验证的过程中用户进行了其他的操做,岂不是打乱或者终端了网络验证过程,然后我把原本可以获取焦点的其他编辑框EditText此时设为不可获取焦点:(xxxxxxEditText.setFocusable(false);),但是还是会出现焦点飘走的情况,实在找不出焦点去哪里了(去到了根View?)于是写了一个获取当前焦点的线程代码,如下:
使用findFocus()方法来获取到当前拥有焦点的控件
Runnable run2 = new Runnable() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(3000);
View rootview = AuthenticateActivity.this.getWindow().getDecorView();
View currentView= rootview.findFocus();
//TAG为当前Activity名称
Log.i(TAG, "当前焦点所在View:"+currentView.toString());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
new Thread(run2).start();
此代码是开启了一个子线程持续获取当前焦点所在View,每隔3秒钟打印一次。
打印如下:
11-06 15:37:22.123 1414-1452/com.gzgd.launcher I/launcher: [AuthenticateActivity]当前焦点所在View:android.widget.TextView{22d7b15f VFED.... .F...... 0,22-84,79}
11-06 15:37:27.123 1414-1452/com.gzgd.launcher I/launcher: [AuthenticateActivity]当前焦点所在View:android.widget.ImageButton{21f0047b VFED..C. .F...... 375,0-632,62 #7f070057 app:id/confirm}
11-06 15:37:32.123 1414-1452/com.gzgd.launcher I/launcher: [AuthenticateActivity]当前焦点所在View:android.widget.TextView{22d7b15f VFED.... .F...... 0,22-84,79}
11-06 15:37:37.123 1414-1452/com.gzgd.launcher I/launcher: [AuthenticateActivity]当前焦点所在View:android.widget.TextView{22d7b15f VFED.... .F...... 0,22-84,79}