我很难触发我的主要View的onKey事件。我不确定我做错了什么,我已经正确实现了onClick事件,但似乎无法弄清楚onKey事件。
以下是相关代码:
public class MyActivity extends Activity {
private RelativeLayout main;
private ApplicationToolbar toolbar;
public void onCreate(Bundle savedInstanceState) {
...
this.main = (RelativeLayout) this.findViewById(R.id.main);
this.toolbar = new ApplicationToolbar(this);
// toolbar is added to main later on in the code...
this.main.setOnClickListener(mClickListener);
this.main.setOnKeyListener(mKeyListener);
}
private OnClickListener mClickListener = new OnClickListener() {
public void onClick(View v) {
toolbar.setVisibility(View.VISIBLE); // Works correctly.
}
};
private OnKeyListener mKeyListener = new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
toolbar.setBackgroundColor(0xFF0000FF); // Does not work.
return true;
}
};
}
事实上,无论我在mKeyListener中放入什么代码,它都不会执行,这让我相信事件本身永远不会被触发,即使我在我的物理键盘上按下一堆键(Motorola Droid,Android) 2.1)。
答案 0 :(得分:0)
您可以尝试覆盖onKeyDown(int keyCode, KeyEvent event)
检查: