你说的是对话框吗?您不需要为此创建新的活动。创建一个自定义对话框并使用 onDialogPositiveClick() 侦听器将参数传回。
首先,您的 MainActivity 应该实现 TablesDialogFragment.NoticeDialogListener。
然后,在您的对话框类中:
public class TablesDialogFragment extends DialogFragment implements OnLongClickListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final ArrayList<String> mSelectedItems = new ArrayList<String>();
String title = getArguments().getString("title");
final String[] tables = getArguments().getStringArray("key");
ContextThemeWrapper ctw = new ContextThemeWrapper( getActivity(), R.style.AlertDialogCustom);
AlertDialog.Builder builder = new AlertDialog.Builder(ctw);
if(tables.length !=0){
builder.setTitle(title)
.setMultiChoiceItems(tables, null,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
if (isChecked) {
// If the user checked the item, add it to the selected items
mSelectedItems.add(tables[which]);
} else if (!isChecked) {
// Else, if the item is already in the array, remove it
mSelectedItems.remove(tables[which]);
}
}
})
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// User clicked OK, so save the mSelectedItems results somewhere
// or return them to the component that opened the dialog
String[] selected = mSelectedItems.toArray(new String[mSelectedItems.size()]);
//Here I pass the String[] to the MainActivity
mListener.onDialogPositiveClick(TablesDialogFragment.this, selected);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
//Collapse SearchView
MainActivity.getSearchMenuItem().collapseActionView();
}
});
} else{
builder.setTitle("Sorry to tell you this, but... :(");
builder.setMessage("Why don't you try something different?")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//Collapse SearchView
MainActivity.getSearchMenuItem().collapseActionView();
}
});
}
return builder.create();
}
然后,在您的 MainActivity 中。
private String[] selectedItems;
public void callDialog(){
TablesDialogFragment newFragment = new TablesDialogFragment();
Bundle args = new Bundle();
String[] array = {"hey", "you"};
args.putString("title", "Title of The Dialog");
args.putStringArray("key", array);
newFragment.setArguments(args);
newFragment.show(getSupportFragmentManager(), "Table");
}
@Override
public void onDialogPositiveClick(DialogFragment dialog, String[] selected) {
// TODO Auto-generated method stub
selectedItems = selected;
//Do Whatever you want with the arguments in your MainActivity
}