Android 如果按下按钮5秒钟,则开始新的活动
如果按下按钮5秒钟,我想开始一个新的活动。我使用的是触控式监听器 我使用的代码如下所示:Android 如果按下按钮5秒钟,则开始新的活动,android,eclipse,Android,Eclipse,如果按下按钮5秒钟,我想开始一个新的活动。我使用的是触控式监听器 我使用的代码如下所示: Button btn = (Button) findViewById(R.id.button1); btn.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) {
Button btn = (Button) findViewById(R.id.button1);
btn.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getActionMasked();
if (action == MotionEvent.ACTION_DOWN) {
} else if (action == MotionEvent.ACTION_UP
|| action == MotionEvent.ACTION_CANCEL) {
}
// TODO Auto-generated method stub
return false;
}
});
在这两者之间我如何使用handler,谢谢你试试这个
您可以使用Touch Listener来执行此操作
尝试:
Handler handel=新的Handler();
b、 setOnTouchListener(新视图。OnTouchListener(){
其中,b是要在其上进行长时间单击的视图(在您的情况下,它应该是按钮)
和Runnable run如下所示
Runnable run = new Runnable() {
@Override
public void run() {
// Your code to run on long click
Intent i=new Intent(this, NewActivity.class);
startActivity(i);
}
};
这会让你达到目的。安排一项任务,然后检查按钮是否仍然按下。
你应该这样做-
//class member
private long firstTime=0,secondTime=0;
现在在方法中
Button btn = (Button) findViewById(R.id.button1);
btn.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getActionMasked();
if (action == MotionEvent.ACTION_DOWN) {
firstTime = System.currentTimeMillis();
} else if (action == MotionEvent.ACTION_UP
|| action == MotionEvent.ACTION_CANCEL) {
secondTime = System.currentTimeMillis();
if(secondTime-firstTime>=5000){ // at least 5000 ms touch down time
// launch your target activity from here
}else{ //ignore it}
firstTime=0; //reseting the value for the next time
secondTime=0;//reseting the value for the next time
}
// TODO Auto-generated method stub
return false;
}
});
为什么需要这样做,只需使用SimpleGetureDetector并在长按时启动活动(override onLongPress()方法)这与要求不符。根据问题,用户自己需要持续按下按钮5秒。但您的解决方案是——在用户按钮单击5秒后启动活动。这里唯一的问题是,您可以在启动操作之前退出活动。在这种情况下,活动将开始格式化代码,并解释它是如何工作的,为什么工作
Button btn = (Button) findViewById(R.id.button1);
btn.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getActionMasked();
if (action == MotionEvent.ACTION_DOWN) {
firstTime = System.currentTimeMillis();
} else if (action == MotionEvent.ACTION_UP
|| action == MotionEvent.ACTION_CANCEL) {
secondTime = System.currentTimeMillis();
if(secondTime-firstTime>=5000){ // at least 5000 ms touch down time
// launch your target activity from here
}else{ //ignore it}
firstTime=0; //reseting the value for the next time
secondTime=0;//reseting the value for the next time
}
// TODO Auto-generated method stub
return false;
}
});
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getActionMasked();
if (action == MotionEvent.ACTION_DOWN) {
prev=System.currentTimeMillis();
} else if (action == MotionEvent.ACTION_UP
|| action == MotionEvent.ACTION_CANCEL) {
curr=System.currentTimeMillis();
if(curr-prev>=5000){
//do your actions here,prev,curr are fields in a class
}
}
// TODO Auto-generated method stub
return false;
}`