假设我想每10秒执行一次操作,并不一定需要更新视图.
问题是:使用带有timertask的计时器是否更好(我的意思是更有效率和更有效),如下所示:
final Handler handler = new Handler();
TimerTask timertask = new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
public void run() {
<some task>
}
});
}
};
timer = new Timer();
timer.schedule(timertask, 0, 15000);
}
Run Code Online (Sandbox Code Playgroud)
或者只是一个带有postdelayed的处理程序
final Handler handler = new Handler();
final Runnable r = new Runnable()
{
public void run()
{
<some task>
}
};
handler.postDelayed(r, 15000);
Run Code Online (Sandbox Code Playgroud)
如果您能解释何时使用哪种方法以及为什么其中一种方法比另一种方法更有效(如果它实际上是这样),我将不胜感激.
我想检测一个doubletap视图,例如a button,然后知道它是哪个视图.我已经看到了这个类似的问题,但他们说这是一个重复的问题似乎没有回答我的问题.
所有我能找到是一个添加GestureDetector到活动中,并添加OnDoubleTapListener到它.但只有在我点击屏幕的背景/布局时才会触发.当我(双击)a时,它不会被触发button.
这是我里面的代码onCreate:
gd = new GestureDetector(this, this);
gd.setOnDoubleTapListener(new OnDoubleTapListener()
{
@Override
public boolean onDoubleTap(MotionEvent e)
{
Log.d("OnDoubleTapListener", "onDoubleTap");
return false;
}
@Override
public boolean onDoubleTapEvent(MotionEvent e)
{
Log.d("OnDoubleTapListener", "onDoubleTapEvent");
//if the second tap hadn't been released and it's being moved
if(e.getAction() == MotionEvent.ACTION_MOVE)
{
}
else if(e.getAction() == MotionEvent.ACTION_UP)//user released the screen
{
}
return false;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent …Run Code Online (Sandbox Code Playgroud) 如何在Android中实现双击按钮?我应该使用OnDoubleTapListener吗?
我正在做一个项目,其中我想在单点触摸上显示特定消息,并使用android在双触摸上显示另一条消息.我可以实现它.
我的示例代码如下
if(firstTap){
thisTime = SystemClock.u`enter code here`ptimeMillis();
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(AddLocation.this, "DOUBLE TAP DETECTED!!!", Toast.LENGTH_LONG).show();
//PUT YOUR LOGIC HERE!!!!
}else{
//Otherwise Reset firstTap
firstTap = true;
}
}else{
firstTap = true;
}
}
return false;
Run Code Online (Sandbox Code Playgroud) 我有一个很好的工作TouchImageView,我想知道它是如何工作的代码:我只想做的是能够捏缩放,或使用双击放大我选择的任何imageview,当我缩放我返回图像的原始大小.TouchImageView.java:
public class TouchImageView extends ImageView {
Matrix matrix = new Matrix();
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;
PointF last = new PointF();
PointF start = new PointF();
float minScale = 1f;
float maxScale = 3f;
float[] m;
float redundantXSpace, redundantYSpace;
float width, height;
static final int CLICK = 3;
float saveScale = 1f;
float right, bottom, origWidth, origHeight, bmWidth, bmHeight;
ScaleGestureDetector mScaleDetector;
Context …Run Code Online (Sandbox Code Playgroud) 我有一个imageview.我需要在双击和单击时以不同的速度开始动画.双击速度快,单拍速度恢复正常.
如何在imageview的onclick上实现手势?
我在我的应用程序中使用tabhost.我使用下面的代码来添加意图:
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
Resources res = getResources();
intent = new Intent().setClass(this, AActivity.class);
spec = tabHost.newTabSpec("Files").setIndicator("NAS Files", res.getDrawable(R.drawable.ic)).setContent(intent);
tabHost.addTab(spec);
Run Code Online (Sandbox Code Playgroud)
在AActivity中,我想在单击按钮时隐藏选项卡(TabWidget).然后单击两次以显示选项卡.我该怎么做?
在我的应用程序中,我有一个按钮.单击和双击按钮后将执行单独的操作.我怎样才能做到这一点?谢谢
如何检测android中的双击?我实现了OnDoubleTapListener并写了这个:
public boolean onDoubleTapEvent(MotionEvent e) {
// TODO Auto-generated method stub
if(e.getAction() == 1){
Toast.makeText(getApplicationContext(),"Double Tap", Toast.LENGTH_SHORT).show();
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
但它没有用.这有什么不对?
我有一个Fragment实现RecyclerView.OnItemTouchListener.如何仅将单击和长按动作事件从RecyclerView传递到GestureDetectorCompat.这就是我的意思是我只想处理点击和长按,其他事件应该由RecyclerView处理,因为它会正常发生.我怎么设置它?
public class MyFragment extends Fragment implements RecyclerView.OnItemTouchListener,
GestureDetector.OnGestureListener {
protected RecyclerView recyclerView;
protected RecyclerView.Adapter adapter;
protected LinearLayoutManager layoutManager;
private GestureDetectorCompat detector;
public MyFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.myfrag, container, false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerview);
layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
recyclerView.addOnItemTouchListener(this);
adapter = new MyAdapter(myData));
recyclerView.setAdapter(adapter);
return rootView;
}
@Override
public boolean onDown(MotionEvent e) {
return false;
}
@Override
public …Run Code Online (Sandbox Code Playgroud) 我想使用 GestureDetector 的 tap 方法检测 libgdx 中的双击。手势监听器类。过去两天我在网上搜索,但找不到如何执行此操作的示例。我知道该方法有一个“计数”变量,但我不知道如何使用它。提前致谢。