如何在Android中完成后将onclick事件传递给方法以解除对话框

时间:2015-08-29 04:49:40

标签: android alertdialog

尝试创建一个通用函数来执行自定义对话框,遇到从自身中解除对话框的问题,这是我在活动中的代码。我正在尝试完成对话但不是活动。我也无法引用alertDialog来从调用类中执行.dismiss()。有任何想法吗?是的,我每次想要显示此弹出窗口时都可以在技术上拥有showPopupMessage,并且可以轻松地在其中进行onclick。我正在尝试创建一个通用的应用程序,我只是在方法之外设置onclicklistener并将其传入。

 case R.id.deleteText:
                            final Context context = this;
                            View.OnClickListener rightEvent = new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    deleteCard(btCardToken);
                                    finish();
                                }
                            };
                            showPopupMessage(context,"Company", "Are you sure you wish to delete this card?", "CANCEL", "YES", rightEvent);
                            break;

然后我的代码在我的泛型类中,

public void showPopupMessage(Context context, String header, String message, String leftButtonText, String rightButtonText, View.OnClickListener rightEvent)
{
    LayoutInflater inflator = LayoutInflater.from(context);
    View promptsView = inflator.inflate(R.layout.edit_text_prompt, null);

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);

    // set prompts.xml to alertdialog builder
    alertDialogBuilder.setView(promptsView);

    final StyledTextView alertHeader = (StyledTextView)promptsView.findViewById(R.id.alertHeader);
    final StyledTextView alertText = (StyledTextView)promptsView.findViewById(R.id.alertText);
    final StyledTextView rightButton = (StyledTextView)promptsView.findViewById(R.id.rightButton);
    final StyledTextView leftButton = (StyledTextView)promptsView.findViewById(R.id.leftButton);
    final EditText inputEditTextBox = (EditText)promptsView.findViewById(R.id.inputEditTextBox);

    alertHeader.setText(header);
    alertText.setText(message);
    inputEditTextBox.setVisibility(View.GONE);
    leftButton.setText(leftButtonText);
    rightButton.setText(rightButtonText);

    // set dialog message
    alertDialogBuilder.setCancelable(true);

    // create alert dialog
    final AlertDialog alertDialog = alertDialogBuilder.create();
    leftButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            alertDialog.dismiss();
        }
    });
    rightButton.setOnClickListener(rightEvent);
    alertDialog.show();
}

4 个答案:

答案 0 :(得分:1)

您应该通过扩展DialogFragment并在AlertDialog回调方法中创建onCreateDialog()来创建对话框。要提供对话框单击,请为每种类型的单击事件定义一个带有方法的接口。然后在主机组件中实现该接口,该接口将从对话框接收操作事件。

您的对话框可能如下所示:

public class MyDialog extends DialogFragment {

    // Use this instance of the interface to deliver action events
    MyDialogListener mListener;

    // Override the Fragment.onAttach() method to instantiate the NoticeDialogListener
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        // Verify that the host activity implements the callback interface
        try {
            // Instantiate the MyDialogListener so we can send events to the host
            mListener = (MyDialogListener) activity;
        } catch (ClassCastException e) {
            // The activity doesn't implement the interface, throw exception
            throw new ClassCastException(activity.toString() + " must implement MyDialogListener");
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Build the dialog and set up the button click handlers
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();
        builder.setView(inflater.inflate(R.layout.edit_text_prompt, null))
        // Inflate and set the layout for the dialog
        // Pass null as the parent view because its going in the dialog layout
        final StyledTextView alertHeader = (StyledTextView) promptsView.findViewById(R.id.alertHeader);
        final StyledTextView alertText = (StyledTextView) promptsView.findViewById(R.id.alertText);
        final StyledTextView rightButton = (StyledTextView) promptsView.findViewById(R.id.rightButton);
        final StyledTextView leftButton = (StyledTextView) promptsView.findViewById(R.id.leftButton);
        final EditText inputEditTextBox = (EditText) promptsView.findViewById(R.id.inputEditTextBox);

        alertHeader.setText(header);
        alertText.setText(message);
        inputEditTextBox.setVisibility(View.GONE);
        leftButton.setText(leftButtonText);
        rightButton.setText(rightButtonText);

        // set dialog message
        builder.setCancelable(true);
        leftButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.onLeftClicked(MyDialog.this);
            }
        });
        rightButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.onRightClicked(MyDialog.this);
            }
        });
        return builder.create();
    }

    /* The activity that creates an instance of this dialog fragment must
     * implement this interface in order to receive event callbacks.
     * Each method passes the DialogFragment in case the host needs to query it. */
    public interface MyDialogListener {
        public void onLeftClicked(DialogFragment dialog);
        public void onRightClicked(DialogFragment dialog);
    }
}

在您的活动中,调用对话框并响应按钮点击,如此

public class MainActivity extends FragmentActivity
                          implements MyDialog.MyDialogListener{
    ...

    public void showMyDialog() {
        // Create an instance of the dialog fragment and show it
        DialogFragment dialog = new MyDialog();
        dialog.show(getSupportFragmentManager(), "MyDialog");
    }

    // The dialog fragment receives a reference to this Activity through the
    // Fragment.onAttach() callback, which it uses to call the following methods
    // defined by the MyDialog.MyDialogListener interface
    @Override
    public void OnLeftClicked(DialogFragment dialog) {
        // User touched the dialog's left button
        ...
    }

    @Override
    public void onRightClicked(DialogFragment dialog) {
        // User touched the dialog's right button
        ...
    }
}

有关详细信息,请查看官方文档:http://developer.android.com/guide/topics/ui/dialogs.html

答案 1 :(得分:0)

http://developer.android.com/guide/topics/ui/dialogs.html

您需要修改许多内容,Dialog.show()并不意味着您的程序会手动调用。您应该使用DialogFragment和FragmentManager来管理对话框显示,隐藏等。

如果您不遵循DialogFragment路线,您将在以后遇到很多麻烦,直到您意识到必须使用它。

答案 2 :(得分:0)

对YES使用AlertDialog setPositiveButton()方法,对NO使用setNegativeButton()方法并处理其点击次数

答案 3 :(得分:0)

在显示对话框不起作用后设置onClickListener吗?

  alertDialog.show();

  leftButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        alertDialog.dismiss();
    }
});
rightButton.setOnClickListener(rightEvent);