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

标签 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_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();

        }
        });

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

最佳答案

首先像这样创建popupview(实际上你做到了)

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

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

不要直接设置 View (builder.setView(R.layout.activitydetails_popup_add_activity); 这就是问题所在。popupview 与此之间没有任何关系。您正在尝试从 popupview 设置文本和获取文本,但是您的构建器使用直接 View 。)在创建它之前;

希望它有用

关于android - 如何在 alertDialogs 中设置文本和获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36384051/

相关文章:

android - 从 native 代码执行 HTTP 请求

Android 深度链接两个单独的 Activity

android - AlertDialog 的自定义 ListView

android-alertdialog - Flutter 对话框以某种方式触发父小部件的导航器路由

java - 警告对话框消失

php - 如何使用PHP正确输出JSON数据

java - android HTTP 请求失败

java - Android:从 Mysql 填充操作栏微调器

android - 如何在首次安装时向数据库插入名称,显示带有xml的自定义对话框,

android - 无法添加窗口—— token null 不适用于服务中的应用程序