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) { } |
根据我从您的问题中了解到的情况,您想在触摸 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 中保存它的引用…..
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/268977.html