getText from a EditText in a DialogFragment

2020-03-19 02:03发布

问题:

I have a DialogFragment like this one

The Dialog is called by clicking on the "plus" button in the MainActivity below which is initially empty. When I click on "continue", I create a button and so far it works. I also wish that the string I insert in the EditText in DialogFragment is shown on the button I've just created. My problem is that I can not retrieve the string I entered in the EditText.

Here is the code:

MainActivity

public class MainActivity extends FragmentActivity implements IProjectDialFrag {

    private ProjectDialogFragment projectDialFrag = new ProjectDialogFragment();

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        switch (id) {
            case R.id.action_settings:
                //TODO
                return true;
            case R.id.filters:
                Intent intent1 = new Intent(MainActivity.this, FiltersActivity.class);
                startActivity(intent1);
                return true;
            case R.id.action_new:
                return true;
            case R.id.add_button:
                Intent intent2 = new Intent(MainActivity.this, ButtonsActivity.class);
                startActivity(intent2);
                return true;
            case R.id.add_project:
                projectDialFrag.show(getFragmentManager(), "projectDialog");
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }

    }

    @Override
    public void onDialogNegativeClick(DialogFragment dialog) {
        return;
    }

    @Override
    public void onDialogPositiveClick(DialogFragment dialog) { 
        LayoutInflater inflater = LayoutInflater.from(this);
        View view = inflater.inflate(R.layout.project_dialog_fragment, null);

        EditText editText = (EditText) view.findViewById(R.id.project_name);
        String projectName = editText.getText().toString();
        Button projectButton = new Button(this);
        projectButton.setText(projectName);
        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.main_layout);
        linearLayout.addView(projectButton);

    }

}

DialogFrament

public class ProjectDialogFragment extends DialogFragment {

    private IProjectDialFrag iProjDialFrag;

    @SuppressLint("InflateParams")
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
         AlertDialog.Builder createProjectAlert = new AlertDialog.Builder(getActivity());

         createProjectAlert.setTitle("Create Project");

         LayoutInflater inflater = getActivity().getLayoutInflater();

         createProjectAlert.setView(inflater.inflate(R.layout.project_dialog_fragment, null))

            .setPositiveButton(R.string.conti_nue, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int id) {
                    iProjDialFrag.onDialogPositiveClick(ProjectDialogFragment.this);
                }
            })
            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int id) {
                    iProjDialFrag.onDialogNegativeClick(ProjectDialogFragment.this);

                }
            });

         return createProjectAlert.create();

    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        iProjDialFrag = (IProjectDialFrag) activity;
    }

}

The Interface

public interface IProjectDialFrag {

    public void onDialogPositiveClick(DialogFragment dialog);
    public void onDialogNegativeClick(DialogFragment dialog);

}

and the XML layout for reference:

DialogFragment layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <EditText 
        android:id="@+id/project_name" 
        android:inputType="text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:layout_marginBottom="4dp"
        android:hint="@string/hint_project_alert" />

</LinearLayout>

MainActivity layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="horizontal"
    android:id="@+id/main_layout">

</LinearLayout>

Where am I wrong?

回答1:

Just call the findViewById on your dialog:

EditText editText = (EditText) getDialog().findViewById(R.id.project_name);
if (editText != null) {
    Log.e("", "Value is: " + editText.getText());
} else {
    Log.e("", "EditText not found!");
}

Note: needs to be called after onCreateDialog.



回答2:

Try like this,

 AlertDialog.Builder createProjectAlert = new AlertDialog.Builder(getActivity());

         createProjectAlert.setTitle("Create Project");

         LayoutInflater inflater = getActivity().getLayoutInflater();

         View view = mInflater.inflate(R.layout.project_dialog_fragment, null);
         EditText project_name = (EditText) view.findViewById(R.id.project_name);

         createProjectAlert.setView(inflater.inflate(R.layout.project_dialog_fragment, null))

            .setPositiveButton(R.string.conti_nue, new DialogInterface.OnClickListener() {

                @Override
                 public void onClick(DialogInterface dialog, int id) {
                    String projectName = editText.getText().toString();
                    iProjDialFrag.onDialogPositiveClick(ProjectDialogFragment.this);
                }
            })
            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int id) {
                    iProjDialFrag.onDialogNegativeClick(ProjectDialogFragment.this);

                }
            });


回答3:

You shouldn't bind (findViewById() or ButterKnife.bind()) your views in onDialogPositiveClick method, they will not work.

Always consider using either onResume() or onStart() fragment lifecycle callbacks for view binding. For example in your case, the code would look like this:

@Override
public void onResume() {
    super.onResume();
    editText = (EditText) getDialog().findViewById(R.id.project_name);
}

Where your editText is a class variable (EditText editText;)