Android OnTouchListner不起作用 - SurfaceView

时间:2016-04-20 16:39:19

标签: java android android-studio ontouchlistener

我想在我的代码中添加一个OnTouchListener,我看了一个关于它的教程,程序员实现了OnTouchListner。因为我想测试Android Studio只想实现View.OnTouchListner(也许程序员使用旧版本的Android Studio)。此OnTouchListner不起作用。我希望你能帮助我。 (为了糟糕的英语而烦恼)

public class SurfaceViewExample extends Activity implements View.OnTouchListener{
    OurView v;
    public boolean starten = true;

    @Override
    protected void onCreate(Bundle savedInstance){
        super.onCreate(savedInstance);
        v = new OurView(this);
        setContentView(v);
    }

    @Override
    protected void onPause() {
         super.onPause();
         v.pause();
    }

@Override
protected void onResume() {
    super.onResume();
    v.resume();
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    Toast.makeText(getApplicationContext(), "Sucessful!", Toast.LENGTH_LONG).show();

    System.out.println("Methode erflogreich ausgeführt");
    switch(event.getAction()){
        case MotionEvent.ACTION_DOWN:
            Toast.makeText(getApplicationContext(), "Sucessful (Action_Down)!", Toast.LENGTH_LONG).show();
            break;
        case MotionEvent.ACTION_UP:
            break;
        case  MotionEvent.ACTION_MOVE:
            break;


    }
    return true;
}

public class OurView extends SurfaceView implements Runnable{
    int r = 255;
    int g = 255;
    long starttime  = System.currentTimeMillis();
    int b = 255;
    LinearLayout layout;
    TextView textView;
    Thread t = null;
    SurfaceHolder holder;
    boolean isitok = false;

    public OurView(Context context) {
        super(context);
        holder = getHolder();
        Timer t = new Timer();
        Random zt = new Random();
        t.schedule(new z(), 1000, zt.nextInt(5000)+2500);
    }

    public void run() {
        while(isitok){
            if(!holder.getSurface().isValid()){
                continue;
            }
            Canvas canvas = holder.lockCanvas();
            Rect myRect = new Rect();
            try{
                layout.removeView(textView);
            }catch(Exception e){

            }

            myRect.set(0, 0, canvas.getWidth(), (int) (canvas.getHeight() * 0.8));
            Paint color = new Paint();
            Paint bcolor = new Paint();
          //  System.out.println("Zufallwerte -->  r=" + r + " g=" + g + " b= " + b + "<---------");
            color.setColor(Color.rgb(r, g, b));
            color.setStyle(Paint.Style.FILL);
            bcolor.setColor(Color.WHITE);
            bcolor.setStyle(Paint.Style.FILL);
            canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), bcolor);
            canvas.drawRect(myRect, color);

            Paint paint = new Paint();
            paint.setColor(Color.BLACK);

            TextPaint textPaint = new TextPaint();
            textPaint.setColor(Color.BLACK);
            textPaint.setTextAlign(Paint.Align.CENTER);
            float textHeight = textPaint.descent() - textPaint.ascent();
            float textOffset = (textHeight / 2) - textPaint.descent();

            RectF bounds = new RectF(0, 0, getWidth(), getHeight());
            textPaint.setTextSize(60);
            canvas.drawText("Stoppuhr: "+ stoppuhr()+"ms", bounds.centerX(), (int)(canvas.getHeight()*0.9), textPaint);
            holder.unlockCanvasAndPost(canvas);
        }
    }
    public void pause(){
        isitok = false;
        while(true){
            try{
                t.join();
            }catch(Exception e){

            }
            break;
        }
        t = null;
    }
    public void resume(){
        isitok = true;
        t = new Thread(this);
        t.start();
    }

    public long stoppuhr(){
        if(starten){
            starttime = System.currentTimeMillis();
        }

        return (System.currentTimeMillis()-starttime);
    }
    final Handler myHandler = new Handler();
    Runnable ru = new Runnable() {
        @Override
        public void run() {
            Random myrand = new Random();
            r = myrand.nextInt(255);
            g = myrand.nextInt(255);
            b = myrand.nextInt(255);
            starten = !starten;
            //stoppuhr();
        }
    };




    class z extends TimerTask {

        @Override
        public void run() {
            myHandler.post(ru);
        }
    }



}

1 个答案:

答案 0 :(得分:0)

正如@ jk2praj所指出,你需要注册onTouchListner,如:

v = new OurView(this);
setContentView(v);
v.setOnTouchListener(this);