如何防止在点击自动打开的微调项目后打开TimePickerDialog?

时间:2017-02-15 18:41:48

标签: android spinner android-spinner android-calendar timepickerdialog

我有spinner只有一个名为Pick a time..的项目。点击此项后,TimePickerDialog应该会打开。

这是我的代码:

AdapterView.OnItemSelectedListener endTimeListener = new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> adapterView, View arg1,
                                       int i, long arg3) {

                final TextView endTimeSpinnerTV = (TextView) adapterView.getChildAt(0);

                switch (i) {
                    default:
                        final Calendar c = Calendar.getInstance();
                        mHourEnd = c.get(Calendar.HOUR_OF_DAY);
                        mMinuteEnd = c.get(Calendar.MINUTE);

                        TimePickerDialog timePickerDialog = new TimePickerDialog(PostSportRequest.this,
                                new TimePickerDialog.OnTimeSetListener() {

                                    @Override
                                    public void onTimeSet(TimePicker view, int hourOfDay,
                                                          int minute) {

                                        Calendar date = Calendar.getInstance();
                                        date.set(Calendar.HOUR_OF_DAY, hourOfDay);
                                        date.set(Calendar.MINUTE, minute);
                                        date.set(Calendar.AM_PM, date.get(Calendar.AM_PM));

                                        showTimeEnd(hourOfDay, minute);

                                        userAvailableTillTimeInSF = new SimpleDateFormat("HH:mm:ss").format(date.getTime());

                                        endTimeString = new SimpleDateFormat("hh:mm a").format(date.getTime());

                                        endTimeSpinnerTV.setText(userAvailableTillTimeAMPM);

                                    }
                                }, mHourEnd, mMinuteEnd, false);
                        timePickerDialog.show();
                        break;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        };

SpinnerOnSameSelection endTimeSpinner.setOnItemSelectedEvenIfUnchangedListener(endTimeListener);

此处SpinnerOnSameSelection课程:

public class SpinnerOnSameSelection extends Spinner {
    OnItemSelectedListener listener;
    private AdapterView<?> lastParent;
    private View lastView;
    private long lastId;

    public SpinnerOnSameSelection(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        initlistner();
    }

    @Override
    public void setSelection(int position) {
        if (position == getSelectedItemPosition() && listener != null) {
            listener.onItemSelected(lastParent, lastView, position, lastId);
        } else {
            super.setSelection(position);
        }

    }

    private void initlistner() {
        // TODO Auto-generated method stub
        super.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
                // TODO Auto-generated method stub
                lastParent = parent;
                lastView = view;
                lastId = id;
                if (listener != null) {
                    listener.onItemSelected(parent, view, position, id);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // TODO Auto-generated method stub
                if (listener != null) {
                    listener.onNothingSelected(parent);
                }
            }
        });

    }

    public void setOnItemSelectedEvenIfUnchangedListener(
            OnItemSelectedListener listener) {
        this.listener = listener;
    }
}

以及showTimeEnd(int hour, int min)

public void showTimeEnd(int hour, int min) {
        if (hour == 0) {
            hour += 12;
            format = "AM";
        } else if (hour == 12) {
            format = "PM";
        } else if (hour > 12) {
            hour -= 12;
            format = "PM";
        } else {
            format = "AM";
        }

        userAvailableTillTimeAMPM = hour + ":" + min + " " + format;

}

问题是当我打开活动时,TimePickerDialog会自动打开。

我如何阻止这种情况发生?

2 个答案:

答案 0 :(得分:1)

我猜你是在值之前设置监听器。所以听众被解雇了:

 endTimeSpinner.setOnItemSelectedEvenIfUnchangedListener(endTimeListener);
 endTimeSpinner.setSelection(1);

您必须将其反转。首先设置选择,然后设置监听器

 endTimeSpinner.setSelection(1);
 endTimeSpinner.setOnItemSelectedEvenIfUnchangedListener(endTimeListener);

编辑:

我非常确定如果将TextView与clickListener一起使用,它会更好。

但是如果你真的需要Spinner,如果位置为0,请不要调用你的监听器:

private void initlistner() {
    // TODO Auto-generated method stub
    super.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                                   int position, long id) {
            // TODO Auto-generated method stub
            lastParent = parent;
            lastView = view;
            lastId = id;
            if (listener != null && position > 0) {
                listener.onItemSelected(parent, view, position, id);
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            // TODO Auto-generated method stub
            if (listener != null) {
                listener.onNothingSelected(parent);
            }
        }
    });

}

答案 1 :(得分:0)

解决问题的方法是设置你的监听器:

 endTimeSpinner.post(new Runnable() {
        @Override
        public void run() {
            endTimeSpinner.setOnItemSelectedListener(endTimeListener);
        }
    })