ONTAP监听器实现 [英] OnTap listener implementation
问题描述
我有一个疑问是在一个特定的按钮或图像视图或视图实现自来水监听器?因为这是我唯一的冲浪网站显示了整体布局,我想可以在视图上的窃听进行我的行动。请帮忙。谢谢你。
I have a doubt that is on tap listener implemented on a particular button, or image view or view? because the sites which I surfed only shows for the whole layout and I want my action to be performed on the tapping of a view. please help. Thank you.
推荐答案
任意视图可以设置与 onClickListener()
这是视图类的一部分。做到这一点最简单的方法是,当你设置引用您在的onCreate()
方法视图。下面是一个像看一个例子:
Any view can be setup with an onClickListener()
which is part of the view class. The easiest way to do it is when you setup the references to your view in the onCreate()
method. Here is an example for a image view:
ImageView iv = (ImageView) findViewByID(R.id.example);
iv.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Do what you need to do on click
....
}
});
更新:枪王
UPDATE: DOUBLE TAP
下面是实现基本枪王检测上的图像查看示例活动:
Here is a sample activity which implements basic double tap detection on an image view:
import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
public class DoubleTapActivity extends Activity {
//Set the double tap delay in milliseconds
protected static final long DOUBLE_CLICK_MAX_DELAY = 1000L;
private ImageView iView;
private long thisTime = 0;
private long prevTime = 0;
private boolean firstTap = true;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
iView = (ImageView)findViewById(R.id.iView);
iView.setOnTouchListener( new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(firstTap){
thisTime = SystemClock.uptimeMillis();
firstTap = false;
}
else
{
prevTime = thisTime;
thisTime = SystemClock.uptimeMillis();
//Check that thisTime is greater than prevTime
//just incase system clock reset to zero
if(thisTime > prevTime){
//Check if times are within our max delay
if((thisTime - prevTime) <= DOUBLE_CLICK_MAX_DELAY){
//We have detected a double tap!
Toast.makeText(DoubleTapActivity.this, "DOUBLE TAP DETECTED!!!", Toast.LENGTH_LONG).show();
//PUT YOUR LOGIC HERE!!!!
}
else
{
//Otherwise Reset firstTap
firstTap = true;
}
}
else
{
firstTap = true;
}
}
return false;
}
});
}
}
这篇关于ONTAP监听器实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!