3

我正在尝试获取用户输入对话框的值,使用推荐的 DialogFragment 类,对话框构造并运行良好,但我无法将 EditText 参数的值返回给父类,没有得到空指针异常.

我的 DialogHost 类,它构造、返回父级并将其链接到其按钮。

package jo.app.co;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;

public class DialogHost extends DialogFragment {


public interface NoticeDialogListener {
    public void onDialogPositiveClick(DialogFragment dialog);
    public void onDialogNegativeClick(DialogFragment dialog);
}

NoticeDialogListener mListener;

@override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    try {

        mListener = (NoticeDialogListener) activity;

    } catch (ClassCastException e) {      

        throw new ClassCastException(activity.toString());
    }
}

@override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    LayoutInflater inflater = getActivity().getLayoutInflater();

    builder.setView(inflater.inflate(R.layout.dialog_add, null))

           .setPositiveButton("Save", new DialogInterface.OnClickListener() {

               @override
               public void onClick(DialogInterface dialog, int id) {        
                   mListener.onDialogPositiveClick(DialogHost.this);                       

               }
           })
           .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   DialogHost.this.getDialog().cancel();                       
               }
           });      
    return builder.create();
}

}

这是我的 MainActivity 类显示对话框

package jo.app.co;

import android.app.DialogFragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.widget.EditText;

public class MainActivity extends FragmentActivity implements      DialogHost.NoticeDialogListener {


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

}

public void showNoticeDialog() {

    DialogFragment dialog = new DialogHost();
    dialog.show(getFragmentManager(), "DialogHost");
}


@override
public void onDialogPositiveClick(DialogFragment dialog) {

    EditText myText = (EditText) findViewById(R.id.item_added);
    try {
        Log.d ("IN TRY", myText.getText().toString());          
    }
    catch (Exception e) {
        Log.e ("IN CATCH", e.toString());
    }           

}

@override
public void onDialogNegativeClick(DialogFragment dialog) {        
    Log.d ("INMAIN", "REACHED NEG");
}


 }

这是我的对话框的布局供参考。

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

    <EditText
       android:id="@+id/item_added"
       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_add_item" />

</LinearLayout>
4

1 回答 1

2

您引用编辑文本的方式似乎有问题。您需要从您膨胀的视图中获取它请尝试以下代码,该代码在您的主要活动本身中添加所有功能。如果您愿意,可以使用单独的课程来适应您的情况:

public void showNoticeDialog() {

public String inputvalue;

LayoutInflater inflater = LayoutInflater.from(this);
final View textenter = inflater.inflate(R.layout.dialog_add, null)
final EditText userinput = (EditText) textenter.findViewById(R.id.item_added); 
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(textenter)
          .setTitle("Your title");
builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {

               @override
               public void onClick(DialogInterface dialog, int id) {        
                   inputvalue =  userinput.getText().toString();                  
                  // Do something with value in inputvalue
               }
           })
           .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                 dialog.cancel();

               }
           });      
    AlertDialog dialog = builder.create();
          builder.show();
         }

对于 alert dialog ,只有当您计划在该对话框中有多个视图(如超过 1 个 edittext)时,您才需要扩充 xml。如果您打算像示例中那样只有 1 个 edittext,则不需要 xml,您可以直接定义一个 edittext 并将其设置为警报对话框的视图。

final EditText userinput = new EditText(context);
            builder.setView(userinput);
于 2013-07-06T10:31:52.063 回答