简体   繁体   中英

Android: Touch Listener - method call

I've got a textView and i want when i hold my finger on the textView, it should call the method repetitive.

I only know these listenener: onClick, onLongClick, onTouch ... so i don't really know how to do that.

Here is my example:

package de.tiendonam.touchz;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

public class GameActivity extends Activity {

TextView gameBackground;
int points = 0;

 @Override
 protected void onCreate(Bundle savedInstanceState)
 {
     super.onCreate(savedInstanceState);

     //fullscreen
     getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
     getActionBar().hide();
     getWindow().setFlags(
             WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
     setContentView(R.layout.activity_game);

     gameBackground = (TextView) findViewById(R.id.gameBackground);
     gameBackground.setText("Points: 0");

     gameBackground.setOnTouchListener(new OnTouchListener(){

        @Override
        public boolean onTouch(View v, MotionEvent e) {

            int action = e.getAction();

            if(action == MotionEvent.ACTION_DOWN)
            {
                points++;
            }
            else if(action == MotionEvent.ACTION_UP)
            {
                points = 0;
            }
            gameBackground.setText("Points: "+points);

            return true;
        }

     });
 }
 }

( sorry for my bad English, i'm not native english speaker )

You will have to use a CountdownTimer . Start it when on MotionEvent.ACTION_DOWN and stop it on MotionEvent.ACTION_UP . Use the onTick to increment your points.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM