来自DialogFragment中的EditText的getText

时间:2014-11-20 10:34:15

标签: android android-edittext android-dialogfragment

我有像这样的DialogFragment

enter image description here

通过单击" plus"来调用Dialog。 MainActivity中的按钮最初为空。当我点击"继续"时,我创建一个按钮,到目前为止它的工作原理。我还希望我在DialogFragment的EditText中插入的字符串显示在我刚刚创建的按钮上。我的问题是我无法检索我在EditText中输入的字符串。

以下是代码:

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

}

界面

public interface IProjectDialFrag {

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

}

和XML布局供参考:

DialogFragment布局

<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布局

<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>

我哪里错了?

3 个答案:

答案 0 :(得分:13)

只需在对话框中调用findViewById

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

注意:需要在onCreateDialog之后调用。

答案 1 :(得分:2)

试试这个,

 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);

                }
            });

答案 2 :(得分:0)

您不应该在onDialogPositiveClick方法中绑定(findViewById()ButterKnife.bind())您的观点,它们将无法正常工作。

始终考虑使用onResume()onStart()片段生命周期回调进行视图绑定。例如,在您的情况下,代码将如下所示:

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

您的editText是一个类变量(EditText editText;