我有一个EditText,当它获得焦点或被点击时会启动一个DatePickerDialog片段。它工作正常。但是,在设备旋转后,OnFocus侦听器不再启动片段。焦点移动到EditText行,但光标只是闪烁,对话框不会启动。有任何想法吗?我是否需要在onResume中添加一些额外的代码?
活动文件:
fEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus && (fEditText.getText().length() == 0) && (savedInstanceState == null)) {
DatePickerFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
}
});
布局文件:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:focusableInTouchMode="true"
tools:context=".Activity">
<android.support.design.widget.TextInputLayout
android:id="@+id/DueDate_text_input_layout"
android:layout_width="0dp"
android:layout_height="wrap_content" >
<com.example...EditText
android:id="@+id/FEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="date"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFF" >
</com.example...EditText>
</android.support.design.widget.TextInputLayout>
...
答案 0 :(得分:1)
在onResume()
内onCreate()
而不是Activity
onCreateView
或fragment
{/ 1}}下面尝试
@Override
public void onResume() {
super.onResume();
fEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus && (fEditText.getText().length() == 0) && (savedInstanceState == null)) {
DatePickerFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
}
});
}