2

在我的MainActivity中,我在里面生成了一个DialogAlert2EditText秒。当我单击. EditText_ _MainActivityDialogAlert

这是我的DialogAlert课:

public class ShareDialog extends DialogFragment{

    public interface ShareDialogListener {
        public void onDialogPositiveClick(DialogFragment dialog);
        public void onDialogNegativeClick(DialogFragment dialog);
    }

    ShareDialogListener mListener;
    private Handler mResponseHandler;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
           mListener = (ShareDialogListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement ShareDialogListener");
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        LayoutInflater inflater = getActivity().getLayoutInflater();

        View v = inflater.inflate(R.layout.dialog_share, null); 
        final EditText name_place =    (EditText)v.findViewById(R.id.sharePlaceName);
        final EditText description_place = (EditText)v.findViewById(R.id.shareDescription);
        builder.setView(v)
                .setTitle(R.string.shareTitle)
               .setPositiveButton(R.string.share, new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int id) {
                       String name = name_place.getText().toString();
                       String description = description_place.getText().toString();
                       mListener.onDialogPositiveClick(ShareDialog.this);

                   }
               })
               .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       mListener.onDialogNegativeClick(ShareDialog.this);
                       ShareDialog.this.getDialog().cancel();
                   }
               });      
        return builder.create();
}

}

我的MainActivity工具ShareDialog.ShareDialogListener

在我的MainActivity我有:

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

    public void onDialogPositiveClick(DialogFragment dialog) {
        Toast.makeText(this, "Position Shared", Toast.LENGTH_SHORT).show();

    }

    public void onDialogNegativeClick(DialogFragment dialog) {
        Toast.makeText(this, "Cancel Share", Toast.LENGTH_SHORT).show();

    }

当我单击 中的肯定按钮DialogAlert,我有一个Toast“共享位置”。

我想onDialogPositiveClick()MainActivity我在我的参数名称描述中创建的方法中恢复DialogAlert

4

1 回答 1

3

只需修改onDialogPositiveClick方法以提供参数:

public interface ShareDialogListener {
    public void onDialogPositiveClick(DialogFragment dialog, String name, String description);
    public void onDialogNegativeClick(DialogFragment dialog);
}

在您的警报中:

mListener.onDialogPositiveClick(ShareDialog.this, name_place.getText(), description_place.getText());

在您的活动中:

public void onDialogPositiveClick(DialogFragment dialog, String name, String description) {
    Toast.makeText(this, "Position Shared: " + name + ", " + description, Toast.LENGTH_SHORT).show();

}
于 2013-01-14T15:07:25.887 回答