我有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)一切都很完美。当我点击肯定按钮时,会出现timepickerdialog并设置edittext。
相反,对于Samsung设备(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
永远不会调用on timeset
如何使我的代码符合三星设备?
答案 0 :(得分:1)
我遇到了类似的问题:
onTimeSet是从我的nexus VDM调用的,但不是从三星GS3调用的。两者都有android 4.3。 This answer helped me to work through the problem.
答案 1 :(得分:0)
好吧,过了一会儿我开始工作了,我有一个三星Galaxy S2,onTimeSet
被调用了。我做了以下什么。
您已将getTimePicker
声明为类成员并在同一位置初始化,我已声明Listener
,但在onCreate
初始化它:
private OnTouchListener myListener;
然后在onCreate
内:
final EditText myEditText = (EditText) findViewById(R.id.ed1);
final EditText myEditText2 = (EditText) findViewById(R.id.ed2);
myListener = new OnTouchListener() {
private Boolean validated;
@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("TITLE");
mTimePicker.setButton(DialogInterface.BUTTON_NEGATIVE, "ANULAR",
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;
}
};
myEditText.setOnTouchListener(myListener);
myEditText2.setOnTouchListener(myListener);
结果如下:
希望对你有所帮助。