gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:26:21 25 4
gpt4 key购买 nike

我正在使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com