Android 继续计数按钮
我正在尝试创建一个按钮,当按住该按钮时,将向其添加1或任何数字,直到该按钮被释放。我的目标是创建一个屏幕上的光标键控制器,以便在按钮/位图周围移动。按下时,以下代码仅添加1。如何使其连续计数 谢谢你的帮助Android 继续计数按钮,android,button,ontouchlistener,Android,Button,Ontouchlistener,我正在尝试创建一个按钮,当按住该按钮时,将向其添加1或任何数字,直到该按钮被释放。我的目标是创建一个屏幕上的光标键控制器,以便在按钮/位图周围移动。按下时,以下代码仅添加1。如何使其连续计数 谢谢你的帮助 package s.apps.kontroler; import android.app.Activity; import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; impor
package s.apps.kontroler;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.TextView;
public class Test extends Activity implements OnTouchListener {
Button up;
TextView text;
int countup;
boolean isDown = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
up = (Button) findViewById(R.id.button1);
text = (TextView) findViewById(R.id.textView1);
up.setOnTouchListener(this);
}
public boolean onTouch(View arg0, MotionEvent event) {
// TODO Auto-generated method stub
int i = event.getAction();
if (i == MotionEvent.ACTION_DOWN) {
isDown = true;
if (isDown) {
countup++;
text.setText("Count is: " + countup);
}
else if (i == MotionEvent.ACTION_UP) {
isDown = false;
}
}
return true;
}
}
那么
Calendar c = Calendar.getInstance();
int i = c.get(Calendar.SECOND);
While(//button is held )
{if(c.get(Calender.SECOND)>i)
i++;//addto variable}
这应该做到:
private static final int INTERVAL=500;
private Handler handler= new Handler();
private Runnable incrementRunnable = new Runnable() {
@Override
public void run() {
countup++;
text.setText("Count is: " + countup);
handler.postDelayed(this, INTERVAL);
}
}
public boolean onTouch(View arg0, MotionEvent event) {
int i = event.getAction();
if (i == MotionEvent.ACTION_DOWN) {
incrementRunnable.run();
}
else if (i == MotionEvent.ACTION_UP) {
handler.removeCallbacks(incrementRunnable);
}
return true;
}
为此,如果我将handler=newhandler(),则会出现错误“handler不能解析”;在这两个处理程序行前面,我没有得到任何错误,即使在我放手的时候,它也会添加错误。如何解决此问题?对不起,Handler=new Handler();不知怎么被砍了。嘿,苏瑞不知道你是怎么设置的,我对Android和Java还是新手:)