2011-05-03 2 views
0

contentview가 사용자 정의 뷰 그룹 인 사용자 정의 대화 상자 (대화 상자 확장)가 있습니다. 뷰 그룹에는 몇 개의 edittext 자식이 있지만 뷰 그룹의 dispatchDraw 및 onTouch 메서드에서 직접 드로잉 및 단추를 클릭 할 것입니다 (가능한 많은 뷰를 부 풀리지 않으려 고합니다). 구체적으로이보기에는 대화 상자의 닫기 버튼으로 설정할 수있는 버튼 자식이 없습니다.보기 그룹의 onTouch 메서드 내에서 대화 상자 을 닫으려고하지만 뒤로 키를 눌러 시뮬레이션하는 것 이상으로이 작업을 수행하는 방법을 알 수 없습니다.비 xml 콘텐츠 뷰에서 대화 닫기 Discomiss

활동 코드 :

public class My_Activity extends Activity { 
    ... 
    public void onCreate(Bundle savedInstanceState) { 
     ... 
     //if there's no Class_That_Im_Editing in the database, prompt the user to make a new one by adding information to the editviews in this custom dialog and clicking the area where I draw the ok button 
     my_dialog = new Custom_Dialog(this, R.style.CustomDlg, new Class_That_Im_Editing()); 
    } 
} 

대화 코드 :

public class Custom_Dialog extends Dialog { 
    ... 
     public void onCreate(Bundle savedInstanceState) { 
      ... 
      setContentView(new Custom_ViewGroup(context, Class_That_Im_Editing)); 
     } 
} 

뷰 그룹 코드 :

public class Custom_ViewGroup extends ViewGroup implements OnTouchListener { 
    //this class has some edittext children but _no_ buttons 
    ... 
    public boolean onTouch(View view, MotionEvent event) { 
     if (logic checking if the user has clicked the button area) { 
      //??? what do I put here to dismiss the dialog 
     } 
    } 
} 

나는 dismissDialog (int) 메소드를 사용하고 생각할 수있는 유일한 방법 이는 onCreateDialog 및 onPrepareDialog 이벤트 처리기를 재정의 (override)하는 것을 의미합니다. 하지만 view의 onTouch 메서드 내에서 dismissDialog를 호출하려면 어떻게해야합니까?

어쩌면 나는 어떤 종류의 청취자를 설치해야합니까? 그렇다면이 작업을 수행하는 데 필요한 뼈대 코드는 무엇입니까?

답변

1

그래서 문제는 대화가 존재하는 범위에 있지 않을 때 dismiss() 대화를 말하고있었습니다. 내 솔루션은 다음과 같습니다.

대화 상자와 동일한 범위 (여기서는 내 주요 활동 내)에 OnTouchListener를 만듭니다. 그런 다음 대화 상자를 초기화 할 때 전달합니다. 그러면 대화 상자를보기 그룹에 전달해야합니다.

그것은 다음과 같이 보일 것입니다 :

활동 코드 :

public class My_Activity extends Activity { 
    public Custom_Dialog my_dialog; 
    ... 
    public void onCreate(Bundle savedInstanceState) { 
     OnTouchListener otl_custom_dialog = new OnTouchListener() { 
      public boolean onTouch(View view, MotionEvent event) { 
       if (logic checking if the user has clicked the button area) { 
        //notice I can still access any _public_ variable within the viewgroup class 
        //by using my_dialog.my_custom_viewgroup.public_variable 
        ... 
        //I can now call dismiss() from within this scope 
        my_dialog.dismiss(); 
       } 
       ... 
      } 
     } 
     ... 
     //if there's no Class_That_Im_Editing in the database, prompt the user to make a new one by adding information to the editviews in this custom dialog and clicking the area where I draw the ok button 
     my_dialog = new Custom_Dialog(this, R.style.CustomDlg, new Class_That_Im_Editing(), otl_custom_dialog); 
     my_dialog.show(); 
    } 
} 

대화 코드 :

public class Custom_Dialog extends Dialog { 
    Custom_ViewGroup my_custom_viewgroup; 
    OnTouchListener otl_custom_dialog; 
    ... 
     public void onCreate(Bundle savedInstanceState) { 
      ... 
      setContentView(new Custom_ViewGroup(context, class_that_im_editing, otl_custom_dialog)); 
     } 
} 

뷰 그룹 코드 :이 방법을 테스트 한

public class Custom_ViewGroup extends ViewGroup implements OnTouchListener { 
    public Custom_ViewGroup(Context context, Class_That_Im_Editing class_that_im_editing, OnTouchListener otl_custom_dialog) { 
     ... 
     this.setOnTouchListener(otl_custom_dialog); 
    } 
} 

그리고 그것 잘 작동합니다. 희망이 밖으로 다른 사람을 도와주세요!

관련 문제