1

我有 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 上调用

我应该怎么做才能使我的代码与三星设备兼容?

4

2 回答 2

1

我有一个类似的问题:

onTimeSet 是从我的 nexus VDM 调用的,但不是从三星 GS3 调用的。两者都有android 4.3。 这个答案帮助我解决了这个问题。

于 2014-05-26T15:13:42.503 回答
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);

结果如下:

日志猫

希望对您有所帮助。

于 2014-05-26T08:25:40.323 回答