gpt4 book ai didi

android - Android 中用于重复事件的星期几和时间选择器

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:55:01 26 4
gpt4 key购买 nike

我想创建一个控件,允许用户在我的 Android Activity 中选择星期几(星期一)和一天中的某个时间(下午 1:00)。找不到关于此的任何好帖子?

最佳答案

好的,我想我明白了。我只是不喜欢这个解决方案,因为我在星期几使用的 Spinner 与时间选择器的“主题”不匹配,所以它看起来很糟糕。

编辑:我更改了微调器的布局,您可以在帖子底部看到它,现在看起来好多了。

DayTimePickerFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View thisDialog = inflater.inflate(R.layout.picker_day_time, container, false);

Button btnDateTimeOK = (Button)thisDialog.findViewById(R.id.btnDateTimeOK);
Button btnDateTimeCancel = (Button)thisDialog.findViewById(R.id.btnDateTimeCancel);
final Spinner spinSelectedDay = (Spinner)thisDialog.findViewById(R.id.spinSelectedDay);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.spinner_item_day_picker, getResources().getStringArray(R.array.days_of_week));
spinSelectedDay.setAdapter(adapter);
final TimePicker tpSelectedTime = (TimePicker)thisDialog.findViewById(R.id.tpSelectedTime);

btnDateTimeCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ActionBarActivity thisActivity = (ActionBarActivity) getActivity();
getDialog().dismiss();
}
});

btnDateTimeOK.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ActionBarActivity thisActivity = (ActionBarActivity) getActivity();
String strTime = null, strDay = null, strHour = null, strMinute = null, strAM_PM = null;

strDay = ((String)spinSelectedDay.getSelectedItem());
strMinute = tpSelectedTime.getCurrentMinute().toString();
if (tpSelectedTime.getCurrentMinute() < 10){
strMinute = "0"+strMinute;
}
if (tpSelectedTime.getCurrentHour() > 12){
strHour = (tpSelectedTime.getCurrentHour() - 12)+"";
strAM_PM = "PM";
}else if(tpSelectedTime.getCurrentHour() == 12){
strHour = tpSelectedTime.getCurrentHour().toString();
strAM_PM = "PM";
}else if(tpSelectedTime.getCurrentHour() < 12){
strHour = tpSelectedTime.getCurrentHour().toString();
strAM_PM = "AM";
}
if (strHour != null && strAM_PM != null){
strTime = strHour + ":" + strMinute + " " + strAM_PM;
}

etTarget.setText(strDay + " " + strTime);
getDialog().dismiss();
}
});

return thisDialog;
}

picker_day_time.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >

<Spinner android:id="@+id/spinSelectedDay" android:layout_width="fill_parent" android:layout_height="wrap_content">
</Spinner>

<TimePicker android:id="@+id/tpSelectedTime"
android:layout_width="wrap_content" android:layout_height="wrap_content" >
</TimePicker>

<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<Button android:id="@+id/btnDateTimeCancel" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1"
android:text="@string/button_text_cancel" android:onClick="DateTimeClickCancel" />

<Button android:id="@+id/btnDateTimeOK" android:layout_width="0dip" android:layout_height="wrap_content"
android:layout_weight="1" android:text="@string/button_text_ok" android:onClick="DateTimeClickOK" />
</LinearLayout>

</LinearLayout>

spinner_item_day_picker.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:text="Thursday"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:textSize="@dimen/text_size_x_large" android:padding="10dp" android:gravity="center"/>

关于android - Android 中用于重复事件的星期几和时间选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20746624/

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