在TextView和ImageView上按下按钮保持错误

时间:2018-07-17 11:32:13

标签: android button textview imageview ontouch

我有一个ButtonTextView(number)和一个ImageView。每次我按下按钮,数字都会增加。当我按住按钮时,它将增加很多。 我想同时使用ImageView显示特定编号的图像,同时使按钮INVISIBLE可见一段时间以停止增量。

问题:如果我一次按一下按钮,则下面的代码运行良好,但是当我按住按钮时,图像显示的时间很短,并继续显示数字。而且尽管该按钮设置为不可见5秒钟,但数字仍会增加(因为该按钮仍保持按住状态)。

主要班级

public class MainActivity extends AppCompatActivity {
Button button;
TextView textView;
ImageView imageView;
int i=0;

@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    imageView=findViewById(R.id.imageView);
    textView=findViewById(R.id.textView);
    button = findViewById(R.id.button);
    button.setOnTouchListener(new RepeatListener(400, 100, new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            setText();
            setImage();
        }
    }));
}

public void setText(){
    imageView.setVisibility(View.INVISIBLE);
    textView.setText(""+i);
    i++;
}
public void setImage(){
    if(i==10){
        imageView.setImageResource(R.drawable.kitten);
        imageView.setVisibility(View.VISIBLE);
        buttonInvi();
    }
}
public void buttonInvi(){
    button.setVisibility(View.INVISIBLE);
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            button.setVisibility(View.VISIBLE);
        }
    }, 5000); // where 1000 is equal to 1 sec (1 * 1000)
}
}

RepeatListener CLASS

public class RepeatListener implements OnTouchListener {

private Handler handler = new Handler();

private int initialInterval;
private final int normalInterval;
private final OnClickListener clickListener;
private View touchedView;

private Runnable handlerRunnable = new Runnable() {
    @Override
    public void run() {
        if(touchedView.isEnabled()) {
            handler.postDelayed(this, normalInterval);
            clickListener.onClick(touchedView);
        } else {
            // if the view was disabled by the clickListener, remove the callback
            handler.removeCallbacks(handlerRunnable);
            touchedView.setPressed(false);
            touchedView = null;
        }
    }
};
public RepeatListener(int initialInterval, int normalInterval,
                      OnClickListener clickListener) {
    if (clickListener == null)
        throw new IllegalArgumentException("null runnable");
    if (initialInterval < 0 || normalInterval < 0)
        throw new IllegalArgumentException("negative interval");

    this.initialInterval = initialInterval;
    this.normalInterval = normalInterval;
    this.clickListener = clickListener;
}

public boolean onTouch(View view, MotionEvent motionEvent) {
    switch (motionEvent.getAction()) {
        case MotionEvent.ACTION_DOWN:
            handler.removeCallbacks(handlerRunnable);
            handler.postDelayed(handlerRunnable, initialInterval);
            touchedView = view;
            touchedView.setPressed(true);
            clickListener.onClick(view);
            return true;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            handler.removeCallbacks(handlerRunnable);
            touchedView.setPressed(false);
            touchedView = null;
            return true;
    }

    return false;
}
}

3 个答案:

答案 0 :(得分:0)

如何在增加计数器之前检查能见度

colc   cold
------------
x      z
jc     me

答案 1 :(得分:0)

尝试此行

button.setVisibility(View.GONE);

代替

button.setVisibility(View.INVISIBLE);

答案 2 :(得分:0)

只要达到特定数字并设置listener to null并显示imageview,然后设置您的button invisible,然后在5秒钟后再次设置您的监听器并创建button visible