【问题标题】:TimePickerDialog runs twiceTimePickerDialog 运行两次
【发布时间】:2014-12-30 13:05:44
【问题描述】:

Android Studio:将单个 textView 和单个 TimePickerDialog 作为嵌套的本地类。
当点击 TimePickerDialog 并且 onTimeSet 运行 TWICE 时。

点击一次,它应该只运行一次。 (即“吐司”不应该显示 PartyTime Set:0 然后紧接着 PartyTime Set:1 )
为什么两次?...我怎样才能让它只运行一次? p>

public class MainActivity extends ActionBarActivity {
    int n_Count;
    SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
    Calendar calPartyTime = Calendar.getInstance();
    int ct_hourParty = calPartyTime.get(Calendar.HOUR_OF_DAY); 
    int ct_minuteParty = calPartyTime.get(Calendar.MINUTE);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TimePickerDialog timePickerParty = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {

                calPartyTime.set(Calendar.HOUR_OF_DAY, selectedHour);
                calPartyTime.set(Calendar.MINUTE, selectedMinute);

                String date = dateFormat.format(calPartyTime.getTime());

                TextView tv_PartyTime = (TextView) findViewById((R.id.partyTimeTextView));
                tv_PartyTime.setText(date);

                Toast.makeText(getApplicationContext(), "Party Time Set!:" + n_Count , Toast.LENGTH_SHORT).show();
                n_Count++;
            }
        }, ct_hourParty, ct_minuteParty, true);

        timePickerParty.setTitle("Set Party UTC (HH:MM)");
        timePickerParty.show();
    }
}

【问题讨论】:

    标签: android nested local inner-classes android-timepicker


    【解决方案1】:

    您可以添加一些我使用过的逻辑(使用布尔变量并检查其值 -

    public static class DatePickerFragment extends DialogFragment implements OnDateSetListener {
    
    boolean mFirst = true;
    
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);
    
        // Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(getActivity(), this, year, month, day);
    }
    
    public void onDateSet(DatePicker view, int year, int month, int day) {
        if (mFirst) {
            mFirst = false;
            // Do something with the date chosen by the user
    
            currentYear = year;
            currentMonth = month;
            currentDay = day;
    
            DialogFragment newFragment2 = new TimePickerFragment();
            newFragment2.show(getFragmentManager(), "timePicker");
        }
    }
    }
    

    希望这会有所帮助:)

    【讨论】:

    • 添加的逻辑确实有帮助......谢谢!......虽然我想首先了解“为什么”额外迭代“onTimeSet”。
    • 这基本上是一个已知的错误 - code.google.com/p/android/issues/detail?id=34833
    • 再次感谢您...看起来时间会解决这些问题,所以现在,需要添加逻辑。
    猜你喜欢
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-25
    • 2014-11-25
    • 2021-12-27
    相关资源
    最近更新 更多