简体   繁体   English

不为PreferenceActivity调用onTouchEvent-为什么?

[英]onTouchEvent not called for PreferenceActivity - Why?

I have this very simple code implemented within my app: 我在我的应用程序中实现了以下非常简单的代码:

public class EditPreferences extends PreferenceActivity {

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.v(TAG, "onCreate");   
  }


  @Override
  public boolean onTouchEvent(MotionEvent me) {
    Log.v(TAG, "onTouchEvent");   
    return false;
  } 

}

When the preferences screen/activity shows up, I expect to see the "onTouchEvent" log messages when I touch anything on that screen. 当显示首选项屏幕/活动时,当我触摸该屏幕上的任何内容时,我希望看到“ onTouchEvent”日志消息。

But I don't get any messages. 但是我没有收到任何消息。 Which tells me that onTouchEvent isn't even called. 这告诉我甚至没有调用onTouchEvent。

(I can see the "onCreate" message, though. Of course.) (不过,我可以看到“ onCreate”消息。当然。)

Why isn't PreferenceActivity's onTouchEvent() called? 为什么不调用PreferenceActivity的onTouchEvent()?

Is it possible that it is being intercepted earlier by some other component in the app? 是否有可能被应用程序中的其他组件更早地拦截?

Or am I missing something in the implementation steps? 还是我在实施步骤中遗漏了一些东西?

Thats because, you are actually touching a ListView which is by default implemented by PreferenceActivity, you need to call onTouch of your ListView instead, check following code : 那是因为,您实际上正在触摸默认情况下由PreferenceActivity实现的ListView,因此需要调用ListView的onTouch,请检查以下代码:

getListView().setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        Toast.makeText(getApplicationContext(), "touched", Toast.LENGTH_SHORT).show();
         return false;
    }
});

in your onCreate method. 在您的onCreate方法中。

GOOD LUCK. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM