关于android:当在activity中使用touch Listener时,我们如何从监听器中获取信息给activity? | 珊瑚贝

When using touchListener in activity how can we get information back to the activity from the listener?


我有一个活动,其中有一个 ImageView 和一个 SeekBar。我为 ImageView 注册了一个带有 imageview1.setOnTouchListener(mylistener) 的侦听器(mylistner 是一个类)。我在侦听器的 onTouch 方法中获取运动事件。我希望程序在我触摸图像视图时隐藏 SeekBar。我看过的所有教程都展示了侦听器如何从源中获取事件。但我正在寻找的是将数据发送回源,以便它可以隐藏 SeekBar。

主要问题是在活动的触摸事件处理中,我们如何将结果从侦听器类返回给调用类(源)?有没有更好的方法来做我想做的事?

我应该再做一个需要 SeekBar 的活动吗?如果是怎么办?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class A extends Activity {
//…..
final SeekBar sb=(SeekBar) findViewById(R.id.seekBar1);
Mylistener mylistener;
mylisetner=new MyListener(getApplicationContext());

ImageView.setOnTouchListener(mylistener);
//…..

}

public class MyListener implements View.OnTouchListener {
//…..

public boolean onTouch(View v, MotionEvent event) {
final int action = event.getAction() & MotionEvent.ACTION_MASK;
switch (action) {
        case MotionEvent.ACTION_DOWN:          
       // here how can you get access to the seek bar? or how you inform it to hide?
}
//….

}


根据我从您的问题中了解到的情况,您想在触摸 imageview 时隐藏搜索栏.. 对吗?

在你的 ontouchlistener 中尝试以下代码……

1
2
3
4
5
6
7
public boolean onTouch(View v, MotionEvent event) {  
 if (event.getAction() == MotionEvent.ACTION_UP){
     seekbar.setVisibility(View.VISIBLE);
 } else if (event.getAction() == MotionEvent.ACTION_DOWN) {
     seekbar.setVisibility(View.GONE);
 }
}

其中 seekbar 是你的 SEEKBAR :),在 oncreate 中保存它的引用…..

  • 如果 onTouch 在同一个活动类中处理,则此代码将起作用,但如果您创建一个实现 View.OnTouchListener 的新类,然后添加 onTouch,您将遇到这个问题:seekbar 是在活动类中定义的,与侦听器类并且不能定义为公共的,因此您将无法访问它们。
  • MyListener 在内部类中对吗?如果您将 seekbar 定义为活动的类变量,它将在 MyListener …
  • MyLIsener 不是内部类。
  • 嗯,不把它做成一个有什么特殊要求吗?
  • 我有一个扩展视图的自定义视图,它需要 5 个类来处理缩放功能。如果我在里面定义所有这些类,我将失去代码的可读性。你不觉得吗?
  • 当我触摸自定义视图时,搜索栏应该隐藏!
  • 将自定义事件侦听器定义为:stackoverflow.com/questions/8292712/…
  • 谢谢你成功了。我做了一个事件,它在活动中调用监听器,其中 seekbar 可以变得不可见/可见。


来源:https://www.codenong.com/14644531/

微信公众号
手机浏览(小程序)
0
分享到:
没有账号? 忘记密码?