2
在我的MainActivity
中,我生成DialogAlert
,其中有2 EditText
s。 我想在MainActivity
中恢復我的EditText
,當我點擊DialogAlert
中的正面按鈕。Android - 從AlertDialog傳遞參數到AlertDialog'host
這裏我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
「位置共享」。
我想從MainActivity
我PARAMS的方法onDialogPositiveClick()
恢復名在我DialogAlert
謝謝,它的作品很好! – ErWaN