当按钮保持按下状态时,Android TextVIew滚动

时间:2012-11-02 12:54:05

标签: android

我希望在textview处于按下状态时滚动button。一旦用户释放button,我就想停止滚动。我在按钮上使用setOnTouchListener。但我无法做到这一点。任何人都可以帮助我并分享一些代码来做到这一点。

arrow_up.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    lastDown = System.currentTimeMillis();
                    System.out.println(System.currentTimeMillis());
                flag = true;
                method();
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    System.out.println(System.currentTimeMillis() - lastDown);
                    flag = false;
                    method();
                }
                return false;
            }
        });

public void method() {
        while(flag == true){

            try {
                Thread.sleep(500);
                System.out.println("Flag === " + flag);
                tv.scrollBy(0, -20);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        }

1 个答案:

答案 0 :(得分:1)

您可以通过为按钮创建OnFocusChangeListener来实现。

mButton.setOnFocusChangeListener(new View.OnFocusChangeListener() 
        { 
            @Override 
            public void onFocusChange(View v, boolean hasFocus) 
            { 
                if (hasFocus) 
                { 
                    //code
                }
                else
                {
                    //code
                }
            } 
        });