【问题标题】:Android: Touch Listener - method callAndroid:Touch Listener - 方法调用
【发布时间】:2014-11-12 00:34:25
【问题描述】:

我有一个 textView,我希望当我将手指放在 textView 上时,它应该重复调用该方法。

我只知道这些监听器:onClick、onLongClick、onTouch ...所以我真的不知道该怎么做。

这是我的例子:

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;
        }

     });
 }
 }

(对不起我的英语不好,我不是以英语为母语的人)

【问题讨论】:

  • 你试过onLongClickListener吗?

标签: android


【解决方案1】:

您必须使用CountdownTimer。在MotionEvent.ACTION_DOWN 上启动它并在MotionEvent.ACTION_UP 上停止它。使用onTick 增加您的积分。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-26
    • 2015-07-19
    • 2017-07-22
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-05
    相关资源
    最近更新 更多