출처 :

http://blog.rochdev.com/2011/05/update-ui-when-holding-button.html#!/2011/05/update-ui-when-holding-button.html

 

I got this question recently so thought I would share one of the solutions.

What we want is simply to update the user interface when holding down a button.

The code should be pretty self-explanatory.

The first thing we do is to create a custom button and override events.

Then in the Activity we register a OnLongClickListener for the button where we add a runnable to the message queue.

When running this code you will see a button which will display a ticking counter (1,2,3 ..) when the button is held down.

screenshot

package com.rochdev.sample.longpress;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.TextView;

public class SampleLongpress extends Activity {

    private MyButton mMyButton;
    private TextView mTextView;

    private Handler mHandler;
    private boolean mDown;
    
    int mCounter = 0;
    
    public SampleLongpress () {
        mHandler = new Handler();
    }

    private final Runnable mRunnable = new Runnable() {
        public void run() {
            if (mDown) {
                if (mTextView != null) {
                    mTextView.setText(Integer.toString(mCounter));
                    mCounter++;
                }
                mHandler.postDelayed(this, 1000);
            }
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mMyButton = (MyButton) findViewById(R.id.my_button);
        mMyButton.setOnLongClickListener(new OnLongClickListener() {            
            @Override
            public boolean onLongClick(View v) {
                mDown = true;
                mHandler.post(mRunnable);
                return true;
            }
        });
        mMyButton.setSampleLongpress(this);

        mTextView = (TextView) findViewById(R.id.down_up_tv);
    }

    public void cancelLongPress() {
        mDown = false;
        if (mTextView != null) {
            mTextView.setText("");
            mCounter = 0;
        }
    }
}


 

package com.rochdev.sample.longpress;

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.Button;

public class MyButton extends Button {    
    
    private SampleLongpress sampleLongpress;

    public MyButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public MyButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    public MyButton(Context context) {
        super(context);
    }
    
    public void setSampleLongpress(SampleLongpress sl) {
        sampleLongpress = sl;
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        cancelLongpressIfRequired(event);
        return super.onTouchEvent(event);
    }
    
    @Override
    public boolean onTrackballEvent(MotionEvent event) {
        cancelLongpressIfRequired(event);
        return super.onTrackballEvent(event);
    }
    
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_DPAD_CENTER)
                || (keyCode == KeyEvent.KEYCODE_ENTER)) {
            cancelLongpress();
        }
        return super.onKeyUp(keyCode, event);
    }
    
    private void cancelLongpressIfRequired(MotionEvent event) {
        if ((event.getAction() == MotionEvent.ACTION_CANCEL)
                || (event.getAction() == MotionEvent.ACTION_UP)) {
            cancelLongpress();
        }
    }

    private void cancelLongpress() {        
        if (sampleLongpress != null) {
            sampleLongpress.cancelLongPress();
        }
    }    
}


 

<com.rochdev.sample.longpress.MyButton android:id="@+id/my_button"
        android:layout_width="wrap_content" android:layout_weight="1"
        android:layout_height="wrap_content" android:text="Longpress"
        android:textStyle="bold" />

'Android > Tip&Tech' 카테고리의 다른 글

위젯 관련 ppt  (0) 2013.07.22
안드로이드 기기 드라이버 설치  (0) 2013.07.10
font 크기 참조하자  (0) 2013.07.03
[펌]App Widget 개발에 필요한 것들  (1) 2013.06.28
dialog style에서 actionbar 사용하기  (0) 2013.06.27

+ Recent posts