1

我正在尝试向DialogFragment用户显示他的姓名,我需要将此字符串返回到我的主要活动中。

我按照 android 指南创建了一个自定义对话框片段:http: //developer.android.com/guide/topics/ui/dialogs.html#CustomLayout

在我的代码中,为了响应用户点击肯定按钮,我可以在我的主要活动中调用一个函数:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Get the layout inflater
    LayoutInflater inflater = getActivity().getLayoutInflater();

    builder.setView(inflater.inflate(R.layout.textdialog, null)).setPositiveButton("OK", new DialogInterface.OnClickListener(){
               @Override
               public void onClick(DialogInterface dialog, int id) {
                   ((mainActivity)getActivity()).doPositiveClick();
               }
           }).setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   ((mainActivity)getActivity()).doNegativeClick();
               }
           });      
    return builder.create();
}

其中 R.layout.textdialog 是:

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

<EditText
        android:id="@+id/textdraw"
        android:inputType="textEmailAddress"
        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="introduce text" />

</LinearLayout>

我的问题是,如何在我的对话片段中获取用户编写的文本并将其发送回我的主要活动?

谢谢!

4

1 回答 1

4

好吧,我自己解决了(但不确定这是否是更好的方法)

我在我的onCreateDialog.

  1. 将视图保存在变量中:

    View v= inflater.inflate(R.layout.textdialog, null);

  2. 将编辑文本保存在变量中:

    final EditText ed=(EditText)v.findViewById(R.id.textdraw);

  3. 当用户单击肯定按钮时,发回 edittext 变量中的值:

    getActivity()).doPositiveClick(ed.getText().toString());

完整代码在这里:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Get the layout inflater
    LayoutInflater inflater = getActivity().getLayoutInflater();
    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    View v= inflater.inflate(R.layout.textdialog, null);
    // reference to the edittext
    final EditText ed= (EditText)v.findViewById(R.id.textdraw);


    builder.setView(v).setPositiveButton("OK", new DialogInterface.OnClickListener(){
               @Override
               public void onClick(DialogInterface dialog, int id) {
                   ((mainActivity)getActivity()).doPositiveClick(ed.getText().toString());
               }
           }).setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   ((mainActivity)getActivity()).doNegativeClick();
               }
           });      
    return builder.create();
}

和主要活动中的代码:

public void doPositiveClick(String ed){
    Toast.makeText(this, "Hi, "+ed, Toast.LENGTH_SHORT).show();
}
于 2013-01-03T11:03:59.723 回答