[英]How to move down the selector in list view when click of volume down button in android
How to move focus of list item to down item when we clicked on volume down button of android mobile.Tried this code but unable to down the focus.any help on this or any other alternative approaches to down the selector. 当我们单击android mobile的音量降低按钮时,如何将列表项的焦点移到下降项。尝试了此代码,但无法降低focus.this或任何其他替代方法的选择器的任何帮助。 Thanks in advance. 提前致谢。
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;
}
});
I can hardly imagine how would onKeyListener
on a ListView
detect volume key press. 我很难想象ListView
上的onKeyListener
如何检测到音量按键。 Try to override dispatchKeyEvent()
in your Activity
instead: 尝试改写您的Activity
dispatchKeyEvent()
:
@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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.