프로그래밍/android2012. 6. 9. 10:57

마치 리모컨의 채널 버튼을 누르면 계속 채널이 바뀌는 것처럼 버튼을 누르고 있으면 계속 어떤 일을 하는게 필요했다.


   
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="버튼" />

</LinearLayout>



   
public class HomeActivity extends Activity
{
	private Button _btn1;
	private boolean _isBtnDown;
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		_btn1 = (Button) findViewById(R.id.btn1);
		_btn1.setOnTouchListener(onBtnTouchListener);
	}
	
	private void onBtnDown()
	{
		TouchThread kThread = new TouchThread();
		kThread.start();
	}
	
	private Handler touchHandler = new Handler()
	{
		public void handleMessage(android.os.Message msg)
		{
			Log.w("HomeActivity.touchHandler | handleMessage()", " : " + msg);
		}
	};
	
	private class TouchThread extends Thread
	{
		@Override
		public void run()
		{
			super.run();
			
			while (_isBtnDown)
			{
				touchHandler.sendEmptyMessage(9876);
				
				try
				{
					Thread.sleep(200);
				} catch (InterruptedException e)
				{
					e.printStackTrace();
				}
			}
		}
	}
	
	/********************
	 * Listener
	 ********************/
	private OnTouchListener onBtnTouchListener = new OnTouchListener()
	{
		@Override
		public boolean onTouch(View v, MotionEvent $e)
		{
			switch ($e.getAction())
			{
				case MotionEvent.ACTION_DOWN:
					_isBtnDown = true;
					onBtnDown();
					break;
				
				case MotionEvent.ACTION_UP:
					_isBtnDown = false;
					break;
				
				default:
					break;
			}
			return false;
		}
	};
}










Posted by 쎄미

댓글을 달아 주세요