从alertDialog获取文本

时间:2013-03-05 16:55:06

标签: java android

我在alertDialog中有一个textView,我想在用户点击Ok按钮时获取此文本...

然而我不知道如何实现它? (在谷歌Android开发人员,它没有解释)

这是我的forgot_password_popup.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@null" >

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_weight="0.76"
        android:background="#FFFFBB33"
        android:contentDescription="@string/app_name"
        android:scaleType="center"
        android:src="@drawable/logo" />

    <EditText
        android:id="@+id/emailText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="4dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:layout_marginTop="16dp"
        android:hint="Enter Email Address"
        android:inputType="textEmailAddress" />

</LinearLayout>

这是我更新的课程:

public class LoginDialogFragment extends DialogFragment {

    private EditText emailText;
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();
        final View v = inflater.inflate(R.layout.forgot_password_popup, null);
          emailText = (EditText) v.findViewById(R.id.emailText);


        // Inflate and set the layout for the dialog
        // Pass null as the parent view because its going in the dialog layout
        builder.setView(inflater.inflate(R.layout.forgot_password_popup, null))
        // Add action buttons
               .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int id) {

                       try {
                           String email=emailText.getText().toString();
                        ParseUser.requestPasswordReset(email);
                    } catch (ParseException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                   }
               })
               .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {

                       LoginDialogFragment.this.getDialog().cancel();
                   }
               });      
        return builder.create();
    }
}

这是logcat(因为你可以看到它写了无效的电子邮件,因为电子邮件是空的):

03-05 17:42:27.791: W/System.err(751): com.parse.ParseException: invalid email 
03-05 17:42:27.791: W/System.err(751):  at com.parse.ParseCommand$3.then(ParseCommand.java:352)
03-05 17:42:27.801: W/System.err(751):  at com.parse.Task$10.run(Task.java:442)
03-05 17:42:27.801: W/System.err(751):  at com.parse.Task$1.execute(Task.java:67)
03-05 17:42:27.801: W/System.err(751):  at com.parse.Task.completeImmediately(Task.java:439)
03-05 17:42:27.801: W/System.err(751):  at com.parse.Task.continueWith(Task.java:316)
03-05 17:42:27.801: W/System.err(751):  at com.parse.Task.continueWith(Task.java:327)
03-05 17:42:27.801: W/System.err(751):  at com.parse.Task$8.then(Task.java:377)
03-05 17:42:27.801: W/System.err(751):  at com.parse.Task$8.then(Task.java:1)
03-05 17:42:27.801: W/System.err(751):  at com.parse.Task$11.run(Task.java:474)
03-05 17:42:27.801: W/System.err(751):  at com.parse.Task$1.execute(Task.java:67)
03-05 17:42:27.801: W/System.err(751):  at com.parse.Task.completeAfterTask(Task.java:471)
03-05 17:42:27.801: W/System.err(751):  at com.parse.Task.access$9(Task.java:467)
03-05 17:42:27.801: W/System.err(751):  at com.parse.Task$7.then(Task.java:343)
03-05 17:42:27.811: W/System.err(751):  at com.parse.Task$7.then(Task.java:1)
03-05 17:42:27.811: W/System.err(751):  at com.parse.Task.runContinuations(Task.java:502)
03-05 17:42:27.811: W/System.err(751):  at com.parse.Task.access$5(Task.java:498)
03-05 17:42:27.821: W/System.err(751):  at com.parse.Task$TaskCompletionSource.trySetResult(Task.java:557)
03-05 17:42:27.821: W/System.err(751):  at com.parse.Task$TaskCompletionSource.setResult(Task.java:591)
03-05 17:42:27.821: W/System.err(751):  at com.parse.Task$11$1.then(Task.java:485)
03-05 17:42:27.821: W/System.err(751):  at com.parse.Task$11$1.then(Task.java:1)
03-05 17:42:27.832: W/System.err(751):  at com.parse.Task$10.run(Task.java:442)
03-05 17:42:27.832: W/System.err(751):  at com.parse.Task$1.execute(Task.java:67)
03-05 17:42:27.832: W/System.err(751):  at com.parse.Task.completeImmediately(Task.java:439)
03-05 17:42:27.832: W/System.err(751):  at com.parse.Task.access$8(Task.java:435)
03-05 17:42:27.832: W/System.err(751):  at com.parse.Task$6.then(Task.java:309)
03-05 17:42:27.832: W/System.err(751):  at com.parse.Task$6.then(Task.java:1)
03-05 17:42:27.832: W/System.err(751):  at com.parse.Task.runContinuations(Task.java:502)
03-05 17:42:27.841: W/System.err(751):  at com.parse.Task.access$5(Task.java:498)
03-05 17:42:27.841: W/System.err(751):  at com.parse.Task$TaskCompletionSource.trySetResult(Task.java:557)
03-05 17:42:27.841: W/System.err(751):  at com.parse.Task$TaskCompletionSource.setResult(Task.java:591)
03-05 17:42:27.841: W/System.err(751):  at com.parse.Task$11$1.then(Task.java:485)
03-05 17:42:27.841: W/System.err(751):  at com.parse.Task$11$1.then(Task.java:1)
03-05 17:42:27.841: W/System.err(751):  at com.parse.Task$10.run(Task.java:442)
03-05 17:42:27.841: W/System.err(751):  at com.parse.Task$1.execute(Task.java:67)
03-05 17:42:27.841: W/System.err(751):  at com.parse.Task.completeImmediately(Task.java:439)
03-05 17:42:27.851: W/System.err(751):  at com.parse.Task.continueWith(Task.java:316)
03-05 17:42:27.851: W/System.err(751):  at com.parse.Task.continueWith(Task.java:327)
03-05 17:42:27.851: W/System.err(751):  at com.parse.Task$11.run(Task.java:478)
03-05 17:42:27.861: W/System.err(751):  at com.parse.Task$1.execute(Task.java:67)
03-05 17:42:27.861: W/System.err(751):  at com.parse.Task.completeAfterTask(Task.java:471)
03-05 17:42:27.871: W/System.err(751):  at com.parse.Task.access$9(Task.java:467)
03-05 17:42:27.871: W/System.err(751):  at com.parse.Task$7.then(Task.java:343)
03-05 17:42:27.871: W/System.err(751):  at com.parse.Task$7.then(Task.java:1)
03-05 17:42:27.871: W/System.err(751):  at com.parse.Task.runContinuations(Task.java:502)
03-05 17:42:27.871: W/System.err(751):  at com.parse.Task.access$5(Task.java:498)
03-05 17:42:27.882: W/System.err(751):  at com.parse.Task$TaskCompletionSource.trySetResult(Task.java:557)
03-05 17:42:27.882: W/System.err(751):  at com.parse.Task$TaskCompletionSource.setResult(Task.java:591)
03-05 17:42:27.882: W/System.err(751):  at com.parse.Task$3.run(Task.java:225)
03-05 17:42:27.882: W/System.err(751):  at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:390)
03-05 17:42:27.882: W/System.err(751):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
03-05 17:42:27.891: W/System.err(751):  at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:153)
03-05 17:42:27.891: W/System.err(751):  at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:267)
03-05 17:42:27.891: W/System.err(751):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
03-05 17:42:27.901: W/System.err(751):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
03-05 17:42:27.901: W/System.err(751):  at java.lang.Thread.run(Thread.java:856)

1 个答案:

答案 0 :(得分:0)

public class LoginDialogFragment extends DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {


        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.forgot_password_popup, null);
        final EditText emailText = (EditText) view.findViewById(R.id.emailText);

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setView(view)

               .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int id) {
                       ParseUser.requestPasswordReset(emailText.getText().toString());
                   }
               })

               .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       //LoginDialogFragment.this.getDialog().cancel();
                   }
               });      

        return builder.create();
    }
}