当我们点击android mobile的音量减小按钮时,如何将列表项的焦点移动到下方项目。这个代码但是无法关闭焦点。对这个或任何其他替代选择方法的任何帮助。 提前谢谢。
super.onCreate(savedInstanceState);
// 2. create array adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, // standard row layout
// provided by android
listItemArray);
// 3. Call setListAdapter
setListAdapter(adapter);
listView = getListView();
listView.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_DOWN:
ListView list =(ListView)v;
for (int j=0; j < list.getChildCount(); j++){
if(list.getChildAt(j).getBackground().equals(R.color.gray)){
list.getChildAt(j).setBackgroundResource(R.color.TRANSPARENT);
list.getChildAt(j++).setBackgroundResource(R.color.gray);
}else
list.getChildAt(0).setBackgroundResource(R.color.gray);
}
break;
}
return false;
}
});
答案 0 :(得分:0)
我很难想象onKeyListener
如何ListView
检测音量键按下。请尝试覆盖dispatchKeyEvent()
中的Activity
:
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_VOLUME_UP:
Toast.makeText(this, "volume up", Toast.LENGTH_SHORT).show();
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
Toast.makeText(this, "volume down", Toast.LENGTH_SHORT).show();
return true;
default:
return super.dispatchKeyEvent(event);
}
}