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

How to prevent TimePickerDialog supposed to be opened after clicking a spinner item from opening automatically?

我有一个 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 class:

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;

}

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

我怎样才能防止这种情况发生?

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

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

你必须反转它。先设置selection再设置listener

 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);
            }
        }
    });

}

解决它的一种方法是像这样设置你的监听器:

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