我正在尝试创建一个按钮,当它被按住时,它会为其添加1或任何数字,直到该按钮被释放。我的目标是创建一个屏幕上的光标键控制器来移动按钮/位图。下面的代码只在按下时才加1。如何让它连续计数?继续计数按钮
感谢您帮助
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;
}
}
为此,我得到错误“处理程序不能被解除”,如果我把Handler处理程序=新的处理程序();在两个处理程序行前面,那么即使当我放手时,它也会添加任何错误。我该如何解决这个问题? – Srcko7 2012-02-20 20:18:56
对不起,Handler handler = new Handler();不知何故被切碎。 – 2012-02-20 22:09:34
谢谢你:) – Srcko7 2012-02-21 09:04:13