【问题标题】:Android: Touch Events not WorkingAndroid:触摸事件不起作用
【发布时间】:2014-10-21 14:31:12
【问题描述】:

这是我的主要活动代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Display display = getWindowManager().getDefaultDisplay();        

    //Setting the FileBrowser FrameLayout Settings.
    FrameLayout fileBrowserLayout =  (FrameLayout) findViewById(R.id.flFBrowser);
    FrameLayout previewLayout = (FrameLayout) findViewById(R.id.flPreview);
    TextView instructions = (TextView)findViewById(R.id.tvInstructions);

    fileBrowserLayout.getLayoutParams().width = display.getWidth()*WidthFileBrowser/100;
    previewLayout.getLayoutParams().width = display.getWidth()*WidthPreview/100;
    previewLayout.getLayoutParams().height = display.getHeight()*PreviewHeight/100;
    instructions.getLayoutParams().width = display.getWidth()*WidthPreview/100;
    instructions.getLayoutParams().height = display.getHeight()*InstructionsHeight/100;
    instructions.setText(Instructions);
    instructions.setTextSize(InstructionTextSize);
    instructions.setTextColor(InstructionTextColor);
    instructions.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD);
    instructions.setGravity(Gravity.CENTER_HORIZONTAL);

    //Attempting to set the Fragment        
    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();       
    FileBrowser fb = new FileBrowser();
    preview = new Preview();
    ft.add(R.id.flFBrowser,fb);
    ft.add(R.id.flPreview, preview);
    ft.commit();

}

这是我的预览片段代码

public class Preview extends Fragment implements ViewFactory,OnTouchListener{

@Override
public boolean onTouch(View v, MotionEvent event) {
    System.err.println("Something touched me!");
    return true;
}
}

onActivityStarted 上有更多初始化代码,但它与初始化图像切换器有关。

但是我不知道为什么我的触摸事件没有注册。

有人可以帮帮我吗?

【问题讨论】:

  • 试试Log.e("main", "something touched me! aiiee!"); 而不是println()
  • 我不知道为什么,但我还是尝试了,但什么也没得到。到底有什么区别?
  • System.___.println 没有出现在 Android 中。 Log.e 出现在 logcat 中。
  • 不正确。我看就好了。它出现在 Eclipse LogCat 控制台的橙色 w 部分。还是谢谢

标签: android touch


【解决方案1】:

问题是您实际上并没有注册在您的Preview 类中实现的onTouchListener。我不知道也不认为(基于 API)您可以为 Fragments 注册一个触摸侦听器,但您可以为您拥有的任何视图注册。

尝试为您的其中一个 FrameLayouts 注册 onTouchListener。您可以在初始化预览对象后执行此操作:

preview = new Preview();
previewLayout.setOnTouchListener(preview);

这应该可以解决问题。

【讨论】:

    【解决方案2】:

    就我而言,我的孩子正在拦截触摸事件。我想将识别器附加到父布局,因此,我需要拦截触摸事件 (http://developer.android.com/training/gestures/viewgroup.html#intercept)。我必须继承布局并覆盖 onInterceptTouchEvent() 以将事件传递给我可能附加到布局的任何触摸侦听器。我在片段中实现了监听器和检测器,监听器将事件转发给检测器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-06
      • 2012-06-17
      • 2020-09-04
      • 1970-01-01
      相关资源
      最近更新 更多