如何在alertDialogs中设置文本和getText

时间:2016-04-03 10:04:09

标签: android alertdialog

我使用AlertDialog作为数据条目,布局包含一些EditText字段。我可以引用这些字段,但似乎无法setText或getText:

这是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>

以及我尝试访问它们的代码下面

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

        }
        });

我查看了其他示例,但我无法发现我的实现与其他似乎有效的实现之间的区别。我正在实施的事情的顺序有什么问题吗?

1 个答案:

答案 0 :(得分:1)

首先创建这样的popupview(实际上你已经这样做了)

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

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

不要直接设置视图(builder.setView(R.layout.activitydetails_popup_add_activity);这是问题。在popupview和this之间没有任何关系。你正在尝试从popupview中设置文本和getText,但你的构建器使用直接视图。)在创建之前;

希望它有效