0

contentview がカスタム ビューグループであるカスタム ダイアログ (Dialog を拡張) があります。ビューグループには edittext の子がいくつかありますが、ビューグループの dispatchDraw および onTouch メソッドでボタンの描画とクリックを自分で処理しています (できるだけ多くのビューを膨張させないようにしています)。具体的には、このビューには、ダイアログの閉じるボタンとして設定できるボタンの子がありません。viewgroup の onTouch メソッド内からダイアログを閉じたいのですが、戻るキーを押すことをシミュレートする以外に、これを行う方法がわかりません。

活動コード:

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
        }
    }
}

私が考えることができる他の唯一のアプローチは、onCreateDialog および onPrepareDialog イベント ハンドラーをオーバーライドすることを意味する、dismissDialog(int) メソッドを使用することです。しかし、ビューの onTouch メソッド内から DismissDialog を呼び出すにはどうすればよいでしょうか?

たぶん、ある種のリスナーを設定する必要がありますか?もしそうなら、これを行うためのスケルトンコードは何でしょうか?

4

1 に答える 1

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);
    }
}

この方法をテストしましたが、問題なく動作します。これが他の誰かに役立つことを願っています!

于 2011-05-03T21:56:53.140 に答える