简体   繁体   English

Android:Touch Listener-方法调用

[英]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. 我有一个textView,我想将手指放在textView上时,它应该将方法重复。

I only know these listenener: onClick, onLongClick, onTouch ... so i don't really know how to do that. 我只知道这些监听器:onClick,onLongClick,onTouch ...所以我真的不知道该怎么做。

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 . 您将必须使用CountdownTimer Start it when on MotionEvent.ACTION_DOWN and stop it on MotionEvent.ACTION_UP . 在启动时,它MotionEvent.ACTION_DOWN和停止它MotionEvent.ACTION_UP Use the onTick to increment your points. 使用onTick来增加您的积分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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