我遇到了一个非常奇怪的行为,使我拔头发。 EditText对象保留空字符串,无论在其中输入什么内容,我都不知道为什么。我尝试搜索类似的条目,但是
我的搜索结果与我所经历的没有任何相似之处。
这是我的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tvTitleDateField"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_width="wrap_content"
android:text="@string/date"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="@+id/appointmentDate"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/appointmentDate"/>
<EditText
android:id="@+id/appointmentDate"
android:layout_width="325dp"
android:layout_height="47dp"
android:layout_marginStart="24dp"
android:layout_marginTop="8dp"
android:ems="10"
android:autofillHints="@string/hint_tap_to_set_date"
android:hint="@string/hint_tap_to_set_date"
android:inputType="text"
app:layout_constraintStart_toEndOf="@+id/tvTitleDateField"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:id="@+id/tvTitleTimeField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="@string/time"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="@+id/appointmentTime"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/appointmentTime"/>
<EditText
android:id="@+id/appointmentTime"
android:layout_width="311dp"
android:layout_height="42dp"
android:layout_marginStart="24dp"
android:layout_marginTop="24dp"
android:autofillHints="@string/hint_tap_to_set_time"
android:ems="10"
android:hint="@string/hint_tap_to_set_time"
android:inputType="text"
app:layout_constraintStart_toEndOf="@+id/tvTitleTimeField"
app:layout_constraintTop_toBottomOf="@+id/appointmentDate"/>
<TextView
android:id="@+id/tvTitleNotificationField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="@string/notification"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="@+id/appointmentNotification"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/appointmentNotification"/>
<EditText
android:id="@+id/appointmentNotification"
android:layout_width="256dp"
android:layout_height="42dp"
android:layout_marginStart="8dp"
android:layout_marginTop="24dp"
android:autofillHints="@string/hint_tap_to_enter_notification"
android:ems="10"
android:hint="@string/hint_tap_to_enter_notification"
android:inputType="number"
app:layout_constraintStart_toEndOf="@+id/tvTitleNotificationField"
app:layout_constraintTop_toBottomOf="@+id/appointmentTime"/>
<TextView
android:id="@+id/tvNotes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:textSize="24sp"
android:text="@string/notes"
app:layout_constraintBottom_toBottomOf="@+id/appointmentNotes"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/appointmentNotes"/>
<EditText
android:id="@+id/appointmentNotes"
android:layout_width="301dp"
android:layout_height="44dp"
android:layout_marginStart="24dp"
android:layout_marginTop="24dp"
android:autofillHints="@string/notes"
android:ems="10"
android:gravity="start|top"
android:hint="@string/notes"
android:inputType="textMultiLine"
app:layout_constraintStart_toEndOf="@+id/tvNotes"
app:layout_constraintTop_toBottomOf="@+id/appointmentEvent"/>
<TextView
android:id="@+id/tvEvent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="@string/event"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="@+id/appointmentEvent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/appointmentEvent"/>
<EditText
android:id="@+id/appointmentEvent"
android:layout_width="311dp"
android:layout_height="41dp"
android:layout_marginStart="24dp"
android:layout_marginTop="24dp"
android:autofillHints="@string/enter_event"
android:ems="10"
android:hint="@string/enter_event"
android:inputType="textMultiLine"
app:layout_constraintStart_toEndOf="@+id/tvEvent"
app:layout_constraintTop_toBottomOf="@+id/appointmentNotification"/>
</androidx.constraintlayout.widget.ConstraintLayout>
以下是片段代码的相关部分:
public class ScheduleAppointmentFragment extends Fragment implements FragmentNameProvider{
private static EditText date;
private static EditText time;
private static EditText notification;
private static EditText event;
private static EditText notes;
.
.
.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "Entered: onCreate");
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
sMedicalInformationViewModel = ViewModelProviders.of(this).get(MedicalInformationViewModel.class);
if(getArguments() != null){
petId = getArguments().getInt(PET_ID_KEY);
name = getArguments().getString(PET_NAME_KEY);
appointmentId = getArguments().getString(APPOINTMENT_ID_KEY);
appointmentDate = getArguments().getString(APPOINTMENT_DATE_KEY);
appointmentTime = getArguments().getString(APPOINTMENT_TIME_KEY);
appointmentNotification = getArguments().getString(APPOINTMENT_NOTIFICATION_KEY);
appointmentEvent = getArguments().getString(APPOINTMENT_EVENT_KEY);
appointmentNotes = getArguments().getString(APPOINTMENT_NOTES_KEY);
Log.d(TAG, "appointmentEvent is: " + appointmentEvent);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(TAG, "Entered: onCreateView");
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_schedule_appointment, container, false);
Log.d(TAG, "container is: " + container);
date = view.findViewById(R.id.appointmentDate);
Log.d(TAG, "date at creation is: " + date);
date.setText("This is a test");
date.setVisibility(View.VISIBLE);
Log.d(TAG, "date is initially: " + date.getText());
date.setClickable(true);
date.setOnClickListener(new View.OnClickListener(){
public void onClick(View view) {
Log.d(TAG, "Date EditText field clicked");
CalendarDialogFragment calendarFragment = new CalendarDialogFragment();
//FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
calendarFragment.show(getFragmentManager(), "setFragment");
}
});
.
. (the other EditText entities go here)
.
}
public void saveUserInput(){
Log.d(TAG, "Entered: saveUserInput");
Log.d(TAG, "date is: " + date);
Log.d(TAG, "date now is set to: " + date.getText());
String dateInput = date.getText().toString();
Log.d(TAG, "date at saveUserInput is: " + date);
Log.d(TAG, "dateInput is: " + dateInput);
String timeInput = time.getText().toString();
String notificationInput = notification.getText().toString();
String eventInput = event.getText().toString();
String notesInput = notes.getText().toString();
ContentValues values = new ContentValues();
values.put(APPOINTMENT_DATE, dateInput);
values.put(APPOINTMENT_TIME, timeInput);
values.put(APPOINTMENT_NOTIFICATION, notificationInput);
values.put(APPOINTMENT_EVENT, eventInput);
values.put(APPOINTMENT_NOTES, notesInput);
Log.d(TAG, "appointment values are: " + values);
}
}
date.setText("This is a test");
没有可见效果,提示文本仍保留在屏幕上。但是,date.setText("This is a test");
将日期变量的文本设置为LogCat显示:D/ScheduleAppointmentFragment: date is initially: This is a test
来自onCreateView。
xxx.getText().toString()
加载为LogCat中显示的“”(空字符串):约会值是:约会Notification =约会事件= appointmentTime = appointmentDate =这是一个测试约会Notes = (日期已加载到onCreateView中)。
android:inputType="text"
进行测试。
关于此片段为何不显示设置值并且不保存输入值的任何建议?