您好,我是Android编程的初学者,我在将数据从MainActivity发送到我的dialogBox时遇到问题。我不得不问这里,因为我试了很长时间没有成功。
在MainActivity.class中,我使用onOptionsItemSelected方法从DialogBoxSettings.class创建对话框。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
String title = (String) item.getTitle(); // I set my title
Bundle savedInstanceState = new Bundle();
savedInstanceState.putString("dialogBoxSettingsTitle", title);
DialogBoxSettings newFragment = new DialogBoxSettings();
newFragment.setArguments(savedInstanceState);
Toast.makeText(getApplicationContext(), title, Toast.LENGTH_LONG).show();
newFragment.show(getFragmentManager(), "dialogBoxSettings");
return super.onOptionsItemSelected(item);
}
在onCreateDialog方法的DialogBoxSettings中我创建了我的对话框
public class DialogBoxSettings extends DialogFragment {
private static final String TAG = DialogBoxSettings.class.getSimpleName();
EditText mEditTextDialogBoxService;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.dialog_box_settings, null);
builder.setView(view);
//builder.setMessage(title);
mEditTextDialogBoxService = (EditText) view.findViewById(R.id.editTextDialogBoxService);
builder.setPositiveButton(getResources().getText(R.string.dialogBoxSettingsPositiveButton), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Log.d(TAG, "POSITIVE button");
}
});
builder.setNegativeButton(getResources().getText(R.string.dialogBoxSettingsNegativeButton), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Log.d(TAG, "NEGATIVE button");
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
除了我不知道如何将标题传递给onCreateDialog方法之外,所有这些都能正常工作。我现在需要使用Bundle,但我现在真的尝试了一切并且失败了,所以如果有人帮助我,我将非常感激。到目前为止,我已经尝试过这样的事情,但我还没有成功Pass item[] to DialogFragment
谢谢!!!!
答案 0 :(得分:0)
在您的对话框片段中:
public class DialogBoxSettings extends DialogFragment {
private static final String TAG = DialogBoxSettings.class.getSimpleName();
EditText mEditTextDialogBoxService;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
***final String title = getArguments().getString("dialogBoxSettingsTitle");***
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.dialog_box_settings, null);
builder.setView(view);
***builder.setTitle(title);***
//builder.setMessage(title);
mEditTextDialogBoxService = (EditText) view.findViewById(R.id.editTextDialogBoxService);
builder.setPositiveButton(getResources().getText(R.string.dialogBoxSettingsPositiveButton), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Log.d(TAG, "POSITIVE button");
}
});
builder.setNegativeButton(getResources().getText(R.string.dialogBoxSettingsNegativeButton), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Log.d(TAG, "NEGATIVE button");
}
});
// Create the AlertDialog object and return it
return builder.create();
}