从scrollview打开childview [英] Opening childview back from scrollview

查看:76
本文介绍了从scrollview打开childview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有子视图的父级ScrollView.当用户按下后退"按钮时,我希望子视图处理该事件.我尝试了几件事,但似乎都没有用.按下后退"按钮将终止活动.

I have a parent ScrollView with a child view. When the user presses the back button, I want the child view to handle the event. I have tried a couple of things but none of them seem to work. Pressing the back button kills the activity.

public class GameScrollView extends ScrollView{

     public GameScrollView(Context context) {
          super(context);
     }
     @Override
     public boolean onInterceptTouchEvent (MotionEvent ev){
          return false;

     }
     @Override
     public boolean onKeyDown (int keyCode, KeyEvent event){
          return false;

     }
}

在子视图中,我有以下代码

in the child view I have the following code

public class GameView extends View implements OnTouchListener, onKeyListener{

     public boolean onKey(View v, int keyCode, KeyEvent event){
          if(keyCode == KeyEvent.KEYCODE_BACK){
                    //do stuff
          }
          invalidate();
          return true;        
     }
}

ScrollView中,我还尝试覆盖dispatchKeyEvent方法以返回false,但这也不起作用.我在这里做什么错了?

In the ScrollView I have also tried overriding the dispatchKeyEventmethod to return false, but that did not work either. What am I doing wrong here?

谢谢!

推荐答案

我知道了.之所以不起作用,是因为子视图没有焦点.

I figured it out. The reason this was not working was because the child view did not have focus.

在子视图的构造函数中设置requestFocus()属性可解决此问题.

Setting the requestFocus() property in the constructor of the child view fixed the problem.

这篇关于从scrollview打开childview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆