2011-08-16 7 views
4

있는 대화 상자 창을 통해 클릭 감지가 나는 대화가 :안드로이드

내가 이상하고 대화 상자의 라인 외부 접촉을 감지 할 수 있도록하려는
final Dialog dialog = new Dialog(context); 
dialog.setContentView(R.layout.location_dialog); 
dialog.setTitle("My dialog"); 
dialog.setMessage("My dialog's content"); 
dialog.setCancelable(true); 
dialog.setCanceledOnTouchOutside(true); 
dialog.show(); 

. 난 쉽게 빌드 - 인 방법

dialog.setCanceledOnTouchOutside(true); 

와 함께 대화 상자 영역 외부의 어떤 접촉을 감지 할 수 있습니다하지만 어떻게이 지역 내부의 접촉을 감지 할 수 있습니까? detect touches only in the area in red.

+0

은 당신이 그쪽으로 빨간색 상자를 만들기위한 XML로 무엇을보기를 만들었습니다 ..? – ngesh

+0

실제 응용 프로그램에서는이 빨간색 영역이 없습니다. 간단한 대화 상자입니다. 그림에서 빨간색 상자를 쉽게 표시 할 수있게했습니다. –

+0

그때는 어려운 일입니다. 당신은 다른 사람이 아닌 view에 대해서만 touchListeners를 가질 수 있습니다 .. – ngesh

답변

7

대화의 확장을 만들고 필요한 방법 오버라이드 (override) : dispatchTouchEvent 또는 onTouchEvent를 (문서에서이 그것을받을 할 전망이없는 당신의 윈도우 경계의 외부에서 발생 터치 이벤트를 처리하는 데 가장 유용합니다.)

업데이트 :

@Override 
public boolean dispatchTouchEvent(MotionEvent ev) { 
    Rect dialogBounds = new Rect(); 
    getWindow().getDecorView().getHitRect(dialogBounds); 

    if (dialogBounds.contains((int) ev.getX(), (int) ev.getY())) { 
     Log.d("test", "inside"); 
    } else { 
     Log.d("test", "outside"); 
    } 
    return super.dispatchTouchEvent(ev); 
} 
+0

예를 들어 코드를 입력 해주세요. '@Override가됩니다. public boolean dispatchTouchEvent (MotionEvent ev) {; // do something}'function 함수를 사용합니까? –

+0

답변 됨 updated ... – Flavio