gpt4 book ai didi

android - TimePickerDialog 运行两次

转载 作者:行者123 更新时间:2023-11-29 17:43:57 29 4
gpt4 key购买 nike

Android Studio: with single textView and single TimePickerDialog as a nested local class.
When the TimePickerDialog is tapped and onTimeSet runs TWICE.

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

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

最佳答案

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

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

希望这有帮助:)

关于android - TimePickerDialog 运行两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27706074/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com