Tag Archives: onTouch

[Android] 위젯 컨트롤들의 터치 이벤트 막기 (Prevent touch event)

안드로이드에서 위젯(Widget)이라고 함은 화면을 구성하는 일반적인 컨트롤 구성요소들을 말합니다.

대표적으로 TextView, Button, ImageView등이 있겠습니다. 하지만 RatingBar등과 같이 자체적인 터치 및 드래그 이벤트를 갖는 위젯들도 있습니다. 이러한 위젯들의 사용자 인터렉션을 원치 않는다면 터치 이벤트를 막아야 합니다.

하지만 setEnable()로 막을경우 색감이 어두워지는 효과를 가지게 되고 setOnClickListener()를 이용하여 값을 원상복귀 시키는 방법도 효과는 없습니다.

다음과 같은 방법을 추천합니다.

main.xml
[code]<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout
xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”>
    
<TextView  
   android:layout_width=”fill_parent”
   android:layout_height=”wrap_content”
   android:text=”@string/hello”/>
<RatingBar
android:id=”@+id/rating”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:numStars=”5″
android:stepSize=”1″
android:rating=”3″/>
</LinearLayout>[/code]

MainActivity.java
[code]    @Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
        
    RatingBar ratingBar = (RatingBar) findViewById(R.id.rating);
    ratingBar.setOnTouchListener(new OnTouchListener()
    {
@Override
public boolean onTouch(View view, MotionEvent event)
{
// 터치 이벤트 제거
return true;
};
    });
}[/code]
위에서 알 수 있듯이 setOnTouchListener를 구현하여 onTouch에서 true를 반환하면 터치 이벤트가 막히게 됩니다.

1350786332.zip