1

我有 8 个按钮 我希望应用程序通过滚动屏幕而不是触摸来检测按钮?喜欢 Jelly bean 中的辅助功能吗?我不确定如何将该功能称为?手势?运动感?

这可能吗?我应该寻找什么?

谢谢!

4

1 回答 1

1

默认情况下,Android 中的 UI 元素可以通过滚动球、箭头键等来聚焦... View 有一个 View.setOnFocusChangedListener 回调。您还可以以编程方式将焦点设置在元素上。

我很快写了一些示例代码,但没有测试。

float initialX = 0;
float initialY = 0;
int currentFocusedChild = 0;
List<View> children;

public void walkElements() {
    final LinearLayout mainLayout = (LinearLayout) findViewById(R.id.main_layout);
    children = mainLayout.getFocusables(View.FOCUS_FORWARD);
    mainLayout.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch(event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    initialX = event.getX();
                    initialY = event.getY();                        
                    break;
                case MotionEvent.ACTION_MOVE:
                    float diffX = event.getX() - initialX;
                    float diffY = event.getY() - initialY;

                    if(diffY > 0) {
                        if (currentFocusedChild < children.size() - 1) {
                            currentFocusedChild++;
                        }
                    } else {
                        if (currentFocusedChild > 0) {
                            currentFocusedChild--;
                        }
                    }
                    children.get(currentFocusedChild).setSelected(true);

                    //Sleep for a period of time so the selection is slow enough for the user.
                    Thread.sleep(300);
                    break;
                case MotionEvent.ACTION_UP:
                    children.get(currentFocusedChild).performClick();
                    break;
            }
            return false;
        }
    });

}
于 2012-09-06T04:15:54.900 回答