无法将DialogFragment中的String对象传递给MainActivity

时间:2015-10-29 16:22:33

标签: java android dialogfragment

我创建了DialogFragment [已AlertDialog实施OnCreateDialog(Bundle)]。

DialogFragment要求用户通过EditText框输入项目名称(String),我正在尝试将其传递回MainActivity

MainActivity中,我使用Toast来检查String是否确实已传入。由于没有传递,此检查失败。但是,如果我在DialogFragment中对字符串进行硬编码,则可以正常工作。这让我怀疑我尝试找到EditText对象的方式存在问题,但我不确定我的错误是什么。

MainActivity.java

public class MainActivity extends AppCompatActivity  implements NewProjectDialog.NewProjectDialogListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //initialize the two on screen buttons
    //onclick listeners are attached to activity_main.xml
    Button newProject = (Button) findViewById(R.id.new_project);
    Button browseProjects = (Button) findViewById(R.id.browse_projects);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);

    return true;
}

@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;
    }

    return super.onOptionsItemSelected(item);
}

public void newProject(View view) {
    //Open up the DialogFragment that prompts user for the title
    DialogFragment newFragment = new NewProjectDialog();
    newFragment.show(getFragmentManager(), "New Project Dialog");
}

@Override
public void onDialogOK(String projectTitle) {
    //Toast.makeText(MainActivity.this, projectTitle, Toast.LENGTH_SHORT).show();

}

@Override
public void onDialogCancel() {
    //user pressed cancel in NewProjectDialog
    Toast.makeText(MainActivity.this, "Cancelled", Toast.LENGTH_SHORT).show();
}

public void browseProjects(View view){
    Toast.makeText(MainActivity.this, "Browse Projects", Toast.LENGTH_SHORT).show();
}

NewProjectDialog.java

public class NewProjectDialog extends DialogFragment{

/* The activity that creates an instance of this dialog fragment must
 * implement this interface in order to receive event callbacks.
 * Each method passes the DialogFragment in case the host needs to query it. */
public interface NewProjectDialogListener {
    public void onDialogOK(String projectTitle);
    public void onDialogCancel();
}

// Use this instance of the interface to deliver action events
NewProjectDialogListener mListener;

// Override the Fragment.onAttach() method to instantiate the NoticeDialogListener
@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 = (NewProjectDialogListener) activity;
    } catch (ClassCastException e) {
        // The activity doesn't implement the interface, throw exception
        throw new ClassCastException(activity.toString()
                + " must implement NoticeDialogListener");
    }
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder newProjectDialog = new AlertDialog.Builder(getActivity());

   //prevent dialog from closing
    setCancelable(false);

   //set dialog title
    newProjectDialog.setTitle(R.string.new_project_title);

    //inflate view so that findViewbyId on the next line works
    View view = View.inflate(getActivity(),R.layout.new_project_dialog, null);
    //Link tempEdit object to the text-edit box so we can retrieve data from it below upon button click
    final EditText tempEdit = (EditText)view.findViewById(R.id.project_title);

    //set the view
    newProjectDialog.setView(R.layout.new_project_dialog);

    //set OK button
    newProjectDialog.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            //Toast.makeText(getActivity(), tempEdit.getText().toString(), Toast.LENGTH_SHORT).show();
            mListener.onDialogOK(tempEdit.getText().toString());
        }
    });

    //set cancel button
    newProjectDialog.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            mListener.onDialogCancel();
        }
    });

    // Create the AlertDialog object and return it
    return newProjectDialog.create();
}

1 个答案:

答案 0 :(得分:1)

你是对的,问题是你从另一个角度看EditText。因为你的布局被夸大了两次。

尝试使用setView(View)而不是layout resource id