我有 2 个 Edittext,当我触摸它时会弹出一个 timepickerdialog:
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initTimePicker();
}
private void initTimePicker()
{
Calendar mcurrentTime = Calendar.getInstance();
final int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
final int minute = mcurrentTime.get(Calendar.MINUTE);
final EditText deplacementD = (EditText) findViewById(R.id.editText1);
final EditText deplacementF = (EditText) findViewById(R.id.editText2);
deplacementD.setInputType(InputType.TYPE_NULL);
deplacementF.setInputType(InputType.TYPE_NULL);
// Attachement du timepicker
deplacementD.setOnTouchListener(getTimePicker(hour, minute, "Start"));
deplacementF.setOnTouchListener(getTimePicker(hour, minute, "End"));
}
private OnTouchListener getTimePicker(final int heure, final int minute, final String titre)
{
return new OnTouchListener() {
private boolean validated = false;
@Override
public boolean onTouch(View v, MotionEvent event) {
final EditText editTouched = (EditText) v;
Log.d("Timepicker", "On Touch event");
if (event.getAction() == MotionEvent.ACTION_UP) {
Log.d("Timepicker", "Motion event UP");
TimePickerDialog mTimePicker;
Log.d("Timepicker", "New timepickerdialog instance");
mTimePicker = new TimePickerDialog(editTouched.getContext(),
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute)
{
Log.d("Timepicker", "OnTimeSet called");
if (validated)
editTouched.setText(String
.format("%02d:%02d", hourOfDay, minute));
}
}, 00, 00, true);// Yes 24 hour time
Log.d("Timepicker", "Timepicker title set");
mTimePicker.setTitle(titre);
mTimePicker.setButton(DialogInterface.BUTTON_NEGATIVE, "CANCEL",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which)
{
Log.d("Timepicker", "Cancel Clicked");
validated = false;
}
});
mTimePicker.setButton(DialogInterface.BUTTON_POSITIVE,
getString(android.R.string.ok),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which)
{
Log.d("Timepicker", "Ok called");
validated = true;
}
});
Log.d("Timepicker", "Timepicker show()");
mTimePicker.show();
Log.d("Timepicker", "out of getTimePicker return true");
return true;
}
Log.d("Timepicker", "out of getTimePicker return false");
return false;
}
};
}
}
在库存的 android 设备 (nexus s,4,5) 上一切正常。当我单击肯定按钮时,会出现时间选择器对话框并设置编辑文本。
相反,对于三星设备(S4 4.4.2 和 Note II 4.3),该对话框出现但从onTimeSet
未被调用。不过logcat中没有错误。
这是放入 getTimepicker() 的日志消息的结果:
在 nexus 设备上:
05-26 09:45:51.850: D/Timepicker(3219): Attaching timepicker
05-26 09:45:51.850: D/Timepicker(3219): Inside getTimePicker()
05-26 09:45:58.007: D/Timepicker(3219): On Touch event
05-26 09:45:58.007: D/Timepicker(3219): out of getTimePicker return false
05-26 09:45:58.027: D/Timepicker(3219): On Touch event
05-26 09:45:58.027: D/Timepicker(3219): out of getTimePicker return false
05-26 09:45:58.077: D/Timepicker(3219): On Touch event
05-26 09:45:58.077: D/Timepicker(3219): Motion event UP
05-26 09:45:58.077: D/Timepicker(3219): New timepickerdialog instance
05-26 09:45:58.167: D/Timepicker(3219): Timepicker title set
05-26 09:45:58.177: D/Timepicker(3219): Timepicker show()
05-26 09:45:58.187: D/Timepicker(3219): out of getTimePicker return true
05-26 09:46:05.745: D/Timepicker(3219): Ok called
05-26 09:46:05.745: D/Timepicker(3219): OnTimeSet called
OnTimeSet()
在我单击“确定”按钮后立即调用。
相反,在三星设备上:
05-26 09:47:59.500: D/Timepicker(12897): Attaching timepicker
05-26 09:47:59.500: D/Timepicker(12897): Inside getTimePicker()
05-26 09:48:01.105: D/Timepicker(12897): On Touch event
05-26 09:48:01.105: D/Timepicker(12897): out of getTimePicker return false
05-26 09:48:01.125: D/Timepicker(12897): On Touch event
05-26 09:48:01.125: D/Timepicker(12897): out of getTimePicker return false
05-26 09:48:01.160: D/Timepicker(12897): On Touch event
05-26 09:48:01.160: D/Timepicker(12897): out of getTimePicker return false
05-26 09:48:01.190: D/Timepicker(12897): On Touch event
05-26 09:48:01.190: D/Timepicker(12897): Motion event UP
05-26 09:48:01.190: D/Timepicker(12897): New timepickerdialog instance
05-26 09:48:01.230: D/Timepicker(12897): Timepicker title set
05-26 09:48:01.230: D/Timepicker(12897): Timepicker show()
05-26 09:48:01.245: D/Timepicker(12897): out of getTimePicker return true
05-26 09:48:04.700: D/Timepicker(12897): Ok called
永远不会在 timeset 上调用
我应该怎么做才能使我的代码与三星设备兼容?