如何在Android中使用DialogFragment进行文本输入?

时间:2013-07-06 08:46:25

标签: java android input dialogfragment

我正在尝试获取用户输入对话框的值,使用推荐的DialogFragment类,Dialog构造并运行正常,但我无法将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类,显示了Dialog

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>

1 个答案:

答案 0 :(得分:1)

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

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

对于警告对话框,只有在计划在该对话框中有多个视图(如超过1个edittext)时,才需要对xml进行充气。如果您计划在示例中只有1个edittext,则不需要xml,您可以直接定义edittext并将其设置为警报对话框的视图。

final EditText userinput = new EditText(context);
            builder.setView(userinput);