Android 如何在alertDialogs中设置文本和获取文本

Android 如何在alertDialogs中设置文本和获取文本,android,android-alertdialog,Android,Android Alertdialog,我使用AlertDialog作为数据输入,布局包括几个EditText字段。我可以引用这些字段,但似乎无法设置文本或获取文本: 这是xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_

我使用AlertDialog作为数据输入,布局包括几个EditText字段。我可以引用这些字段,但似乎无法设置文本或获取文本:

这是xml

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

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Date (mm/dd/yyyy)"
    android:inputType="date"
    android:id="@+id/et_appointment_add_date" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Start Time"
    android:inputType="time"
    android:id="@+id/et_appointment_add_start_time" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="End time (optional)"
    android:inputType="time"
    android:id="@+id/et_appointment_add_end_time"/>

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Location (optional)"
    android:inputType="text"
    android:id="@+id/et_appointment_add_location" />
</LinearLayout>

我看了其他的例子,但我看不出我的实现和其他似乎有效的实现之间的区别。我正在实现的事情的顺序有什么问题吗?

首先像这样创建popupview(实际上是您做的)

然后将此视图设置为警报对话框生成器 像这样
builder.setView(popupview)

不要直接设置视图(
builder.setView(R.layout.activitydetails\u popup\u add\u activity);
这就是问题所在。popupview与此之间没有关系。您正在尝试从popupview设置文本和获取文本,但您的生成器使用的是直接视图。)然后再创建它


希望它能工作

您没有去其他任何地方的
最终视图弹出视图
。这可能就是问题所在。我建议你写你自己的对话,那样会容易些。我不懂你的意思。我创建了popupView,以便可以通过(EditText)popupView.findViewById()引用etditText字段。我也不明白你写我自己的对话是什么意思。您的意思是使用自定义的内容而不是alertDialog?您是xml布局文件吗?contais是作为ActivityDetails界面的一部分显示的编辑文本我在这里显示的xml不是活动的xml,而是我在这里膨胀的弹出窗口builder.setView(R.layout.ActivityDetails\u popup\u add\u activity);我是否需要以某种方式将其添加到ActivityDetails.xml中?太好了。工作起来很有魅力。所以,当我调用final View popupView=inflater.inflate(some.xml)时,我是否有效地创建了一个xml实例,使xml有点像类(或者更好,实际上只是模板)?耶。。明白了..这里popupview是view类的对象
CalendarView cv = (CalendarView)findViewById(R.id.cv_acitivtyDetails);
cv.setOnDateChangeListener(new OnDateChangeListener() {

@Override
public void onSelectedDayChange(CalendarView view, int year, int month,
        int dayOfMonth) {

        final String dateString = Integer.toString(month) + "/" + Integer.toString(dayOfMonth) + "/" + Integer.toString(year);

        CharSequence options[] = new CharSequence[] {"Show", "Add", "Delete"};

        AlertDialog.Builder builder = new AlertDialog.Builder(ActivityDetails.this);
        builder.setTitle("Appointments");
        builder.setItems(options, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int position) {

            switch (position) {

            case 0:

                AlertDialog.Builder builder = new AlertDialog.Builder(ActivityDetails.this);
                LayoutInflater inflater = ActivityDetails.this.getLayoutInflater();
                builder.setTitle("Add Activity Appointment");
                builder.setMessage(currentAct);
                builder.setView(R.layout.activitydetails_popup_add_activity);
                final View popupView = inflater.inflate(R.layout.activitydetails_popup_add_activity, null);

                // HERE I REFERENCE BOTH EDITTEXTS, THE ONE I WANT TO SET AND THE ONE I WANT TO GET
                final EditText etDate = (EditText)popupView.findViewById(R.id.et_appointment_add_date);
                final EditText etLoc = (EditText)popupView.findViewById(R.id.et_appointment_add_location);

                // HERE I TRY TO SET THE DEFAULT DATE WHEN THE WINDOW POPS UP, BUT IT DOES NOT SEEM TO DO ANYTHING
                etDate.setText(dateString);

                builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        // HERE I AM USING A TOAST TO CHECK IF I GET THE TEXT FROM THE EDITTEXT, BUT IT ALWAYS SEEMS TO RETURN AN EMPTY STRING
                        String location = "LOC:" + etLoc.getText().toString();
                        Toast.makeText(getApplicationContext(), location, Toast.LENGTH_SHORT).show();
                        dialog.dismiss();
                    }
                });

                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });

                AlertDialog b = builder.create();
                b.show();


        break;

        case 1:

        break;

        case 2:

        break;

        }

        }
        });
        builder.show();

        }
        });
final View popupView = inflater.inflate(R.layout.activitydetails_popup_add_activity, null);