我在从对话框获取数据到调用活动时遇到问题。一直遵循Android文档,但没有运气。我只想捕获某人在我的对话框的编辑文本框中输入的字符串。这是我的设置。Android将参数从对话框传递到调用活动
public class CrossoverMainActivity extends Activity implements CrossoverSaveDialog.NoticeDialogListener {
final CrossoverSaveDialog dialog_save = new CrossoverSaveDialog();
@Override
public void onDialogPositiveClick(DialogFragment dialog) {
// User touched the dialog's positive button
}
@Override
public void onDialogNegativeClick(DialogFragment dialog) {
// User touched the dialog's negative button
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
if (id == R.id.action_save) {
dialog_save.show(getFragmentManager(), "Dialog_save");
return true;
}
if (id == R.id.action_delete) {
CrossoverDeleteDialog dialog_delete = new CrossoverDeleteDialog();
//Fragment fragment = new Fragment();
dialog_delete.show(getFragmentManager(), "Dialog_delete");
return true;
}
return super.onOptionsItemSelected(item);
}
}
然后,在另一个java文件,我有我的对话框:
public class CrossoverSaveDialog extends DialogFragment {
public interface NoticeDialogListener {
void onDialogPositiveClick(DialogFragment dialog);
void onDialogNegativeClick(DialogFragment dialog);
}
// Use this instance of the interface to deliver action events
NoticeDialogListener mListener;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View dialogSave = inflater.inflate(R.layout.crossover_save_dialog, null);
builder.setView(dialogSave);
builder.setPositiveButton(R.string.dialog_save, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mListener.onDialogPositiveClick(CrossoverSaveDialog.this);
}
})
.setNegativeButton(R.string.dialog_cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
mListener.onDialogNegativeClick(CrossoverSaveDialog.this);
}
});
return builder.create();
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// Verify that the host activity implements the callback interface
try {
// Instantiate the NoticeDialogListener so we can send events to the host
mListener = (NoticeDialogListener) activity;
} catch (ClassCastException e) {
// The activity doesn't implement the interface, throw exception
throw new ClassCastException(activity.toString()
+ " must implement NoticeDialogListener");
}
}
}
这里是我的两个问题:
我得到的问题是,编译器抱怨说我没有实现我的界面的方法......我似乎是这样做的。有什么我在这里失踪?
我必须这样做第二个对话框,它不同于我在这里显示的对话框。但是,我会以同样的方式去做。我只是查看回调方法的对话框来确定哪个对话框正在使用?这样我就可以处理每个对话的逻辑。或者,我是否应该在每个对话框中使用一个独立的接口,并使用它们的每一个独特方法,并且只实现其中的每一个?