我在用户输入表单上有一个EditText
行。单击EditText以使用setOnTouchListener
启动DatePickerDialog工作正常。我在EditText上设置处理setonFocusChangeListener
的{{1}}无效。我在这里缺少什么?
(不确定这是否有帮助,但在早期版本中我使用了OnFocusChange
并且DatePickerFragment
工作正常......所以问题可能是使用onFocusChange
而不是片段?我用DatePickerDialog
启动片段的两行是:
onFocusChange()
Activity.java
DatePickerFragment newFragment = new DatePickerFragment(); // and
newFragment.show(getSupportFragmentManager(), "datePicker");)
activity.xml
public class Activity extends AppCompatActivity implements OnFocusChangeListener {
...
private DatePickerDialog zDatePickerDialog
private int mYear;
private int mMonth;
private int mDay;
Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
zListenerEditText = (ListenerEditText) findViewById(id.ZEditText);
Calendar calendar = Calendar.getInstance();
mYear = calendar.get(Calendar.YEAR);
mMonth = calendar.get(Calendar.MONTH);
mDay = calendar.get(Calendar.DAY_OF_MONTH);
mDatePickerDialog = new DatePickerDialog(this,this,mYear,mMonth,mDay);
// This is working fine to handle the a click to launch the DatePickerDialog
zListenerEditText.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// launch the DatePickerDialog.
mDatePickerDialog.show();
return true;
}
});
// This does not work to launch the DatePickerDialog
zListenerEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus ) {
// launch the DatePickerDialog.
mDatePickerDialog.show();
...
});
}
}
在活动文件中编辑1:
...
<com.widget.ListenerEditText
android:id="@+id/ZEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none"
android:textColor="#FFFFFF" />
答案 0 :(得分:2)
从此处public class Activity extends AppCompatActivity implements OnFocusChangeListener
保持这样
public class Activity extends AppCompatActivity {
zListenerEditText = (ListenerEditText) findViewById(id.ZEditText);
Calendar calendar = Calendar.getInstance();
mYear = calendar.get(Calendar.YEAR);
mMonth = calendar.get(Calendar.MONTH);
mDay = calendar.get(Calendar.DAY_OF_MONTH);
mDatePickerDialog = new DatePickerDialog(this,this,mYear,mMonth,mDay);
// This is working fine to handle the a click to launch the DatePickerDialog
// This does not work to launch the DatePickerDialog
zListenerEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus ) {
// launch the DatePickerDialog.
mDatePickerDialog.show();
...
});
}
答案 1 :(得分:1)
这是因为你的onFocusChange事件永远不会被触发,因为当你返回true时你的onTouch事件已经消耗了它。你不能同时拥有这两个事件。如果你只是想在焦点上显示一个对话框,那么我认为只有onFocusChange才能完成这项工作。
编辑:1
我怎么能错过这个?正如@mrinmoy所提到的,你应该从public class Activity extends AppCompatActivity implements OnFocusChangeListener
中删除implementsFocusChange。
问题是你正在听两次焦点改变。当您implements OnFocusChangeListener
所有与您的活动相关的全局onFocusChange()
方法中的所有重点更改都会被覆盖时,您已覆盖。因此,在这种情况下,您在edittext上设置的onFocusChange永远不会被调用。
因此,如果你想为多个edittext处理focusChange,那么实现onFocusChange是好的,否则在单个edittext上设置它会更好。
如果你想在多个editexts上处理onFocusChange,那么这里是样本。
从单个editexts中删除onFocusChange侦听器。
处理您的全局onFocusChange()
方法,如下所示。
public class MainActivity extends AppCompatActivity implements View.OnFocusChangeListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
....
}
@Override
public void onFocusChange(View v, boolean hasFocus) {
switch (v.getId()) {
case R.id.editText1:
// Handle onFocusChange for editText1
break;
case R.id.editText2:
// Handle onFocusChange for editText2
break;
case R.id.editText3:
// Handle onFocusChange for editText3
break;
case R.id.editText4:
// Handle onFocusChange for editText4
break;
default:
// Handle default scenario.
break;
}
}
}
答案 2 :(得分:0)
尝试这个,它适合我
inputDOB.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
if (b) {
datePicker();
}
}
});
和 datePicker 方法是
public void datePicker() {
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
datePickerDialog = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
inputDOB.setText(formatDate(year,monthOfYear+1,dayOfMonth));
datePickerDialog.dismiss();
hideSoftKeyboard(SignUpActivity.this);
}
}, mYear, mMonth, mDay);
datePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis());
datePickerDialog.show();
}