0

AlertDialoglayoutInflater を使用してレイアウトからカスタムを作成します。このレイアウトで、ボタンを見つけて、OnClickListenerこのボタンに設定しました。問題は、メソッドonClickが存在せず、できないDialogInterfaceことですdialog.dissmiss。AlertDialog、またはおそらく上のポインターを取得するにはどうすればよいDialogInterfaceですか?

    AlertDialog.Builder builder;
    LayoutInflater inflater = (LayoutInflater) myApp.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.my_dialog, null);
    ((Button) layout.findViewById(R.id.downloadButton)).setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            //in this, i want to dissmiss dialog
            //some code
        }
    });

クラス属性のダイアログにポインターを保存したくありません-他の方法が存在する可能性がありますか? ありがとう :)

4

2 に答える 2

0

Dialog クラスを使うと以下のようにできます。

final Dialog dialog = new Dialog(MAinActivity.this);
dialog.setContentView(R.layout.login_popup);
Button dialogButtonOk = (Button) dialog.findViewById(R.id.dialogButtonOK);

dialogButtonOk.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                       dialog.dismiss();
}
}
于 2012-10-18T10:55:06.410 に答える
0

私はこれに取り組んでいたところです...あなたがしていることに関係のない重要なものを削除していないことを願っています.

public class updateDialog extends AlertDialog implements AlertDialog.OnClickListener {
    updateDialog(Context context, Object sent)
    {
        super(context,R.style.MyAlertDialogStyle);
        // Inflate your view
        View myView = getLayoutInflater().inflate(R.layout.update_sql,null);
        // Set it to be the view
        setView(myView);
        // Set up buttons
        setButton(BUTTON_NEGATIVE, context.getString(Cancel), this);
        setButton(BUTTON_NEUTRAL,context.getString(R.string.Propose), this);
        setButton(BUTTON_POSITIVE, context.getString(R.string.Restore),  this);
    }

    // Handle buttons
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
        switch (i)
        {
            case BUTTON_NEGATIVE:
                // Dismiss the dialog.
                dialogInterface.dismiss();
                break;
            case BUTTON_NEUTRAL:
                break;
            case BUTTON_POSITIVE:
                break;
        }
    }

ほとんどの場合、重要なのはビルダーを使用することではなく、自分でビルドすることです。

于 2016-10-29T11:58:19.010 に答える