android - 如果 subview 获得焦点,则 OnKeyListener 或 OnKeydown 不起作用

标签 android android-scrollview trackball

我想听一个 ScrollView 看它是否在滚动。我使用了 OnTouchListener,效果很好。但是当我想使用 OnKeyListener 或重写 OnKeydown 方法为轨迹球添加兼容性时,它就无法工作。看起来像焦点的子按钮导致了问题。

是否有解决此问题的解决方案或变通方法?感谢您的帮助。

这里有一些演示代码可以重现我的问题:

public class TestActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ScrollView scrollView = (ScrollView) findViewById(R.id.scroll_view);
    LinearLayout mainLayout = (LinearLayout) findViewById(R.id.main_layout);

    LinearLayout.LayoutParams wrapParams = new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.FILL_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    for (int i = 0; i < 100; i++) {
        MyItem item = (MyItem) LayoutInflater.from(this).inflate(R.layout.item, null);
        item.setParent(scrollView);
        item.setBackgroundColor(Color.WHITE);
        item.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        mainLayout.addView(item, wrapParams);
    }

    scrollView.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // can go into here
        }

    });

    scrollView.setOnKeyListener(new OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN || keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
                // never go in, unless no child button get focus
            }
            return false;
        }
    });
}

}

主.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <com.fannyxie.MyScroller 
        android:id="@+id/scroll_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:descendantFocusability="beforeDescendants"
        android:clickable="true"
        android:focusable="true">
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical">
            <LinearLayout android:id="@+id/main_layout"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical">
            </LinearLayout>
        </LinearLayout>
    </com.fannyxie.MyScroller>
</LinearLayout>

项目.xml:

<?xml version="1.0" encoding="utf-8"?>
<com.fannyxie.MyItem xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <TextView 
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="TITLE"/>
    <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:id="@+id/myButton" android:text="Button"></Button>
</com.fannyxie.MyItem>

MyScroller.java

public class MyScroller extends ScrollView {

    public MyScroller(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //not go into here...
        Log.i("MyScroller", "onKeyDown");
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onTrackballEvent(MotionEvent event) {
        //not go into here...
        Log.i("MyScroller", "onTrackballEvent");
        return super.onTrackballEvent(event);
    }

    @Override
    protected boolean onRequestFocusInDescendants(int direction,
            Rect previouslyFocusedRect) {
        //some times go into here, when no button get the focus when entering first time
        Log.i("MyScroller", "request focus in descendants");
        return super.onRequestFocusInDescendants(direction, previouslyFocusedRect);
    }



}

我的元素.java

public class MyItem extends LinearLayout {

    private Button myButton;
    public MyItem(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected boolean onRequestFocusInDescendants(int direction,
            Rect previouslyFocusedRect) {
        // never go into here
        Log.i("MyItem", "request focus in descendants");
        return super.onRequestFocusInDescendants(direction, previouslyFocusedRect);
    }

}

最佳答案

谢谢@Jeffrey。但我找到了更好的方法。只需覆盖 ScrollView 中的 dispatchkeyevent 方法并在那里处理轨迹球/键盘事件。它运行良好。

public class MyScroller extends ScrollView {

public MyScroller(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if ((KeyEvent.KEYCODE_DPAD_UP == event.getKeyCode() || KeyEvent.KEYCODE_DPAD_DOWN == event.getKeyCode())) {
            //handle key events here
    }
    return super.dispatchKeyEvent(event);
}

}

关于android - 如果 subview 获得焦点,则 OnKeyListener 或 OnKeydown 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11437681/

相关文章:

java - Activity to Fragment 错误,无法获取共享首选项的值

c++ - 在 visual studio 2010 上使用 opencv 进行球跟踪会出现未处理的异常

android - 按下轨迹球的 'on' 按钮时调用哪个 'Select' 方法?

android - 给出 Intent 返回的结果代码或数据是什么

android - 我的 Intent 将 putExtra 放入两个具有不同值的不同名称中。但是我在另一项 Activity 中获得相同的值(value)?

android - FrameLayout 中的 ScrollView 的问题

安卓 : ImageVIew onTouch Drag is intercepted by ScrollView ontouch Event

java - 如何在没有动画的情况下滚动到 ScrollView 中的某个位置?

java - 常规实现中如何改变轨迹球的中心?

java - @context httpServletRequest 对 jersey 中 post 方法的请求为空