gpt4 book ai didi

android - TimePicker 分钟间隔

转载 作者:行者123 更新时间:2023-12-05 00:16:40 28 4
gpt4 key购买 nike

我已经构建了一个带有日期选择器和时间选择器的微调器格式的 View 。一切似乎都运行良好,只是我不知道如何将分钟设置为 5 分钟间隔?

我看到了如何使用 DialogFragment 执行此操作的示例,并使用 DialogFragments 通过日期选择器或时间选择器启动单独的 Activity ,但我正在尝试同时创建一个 Activity 。

我需要修改我当前的代码以更改分钟间隔,或者在我的代码中合并一个 DialogFragment,它与嵌入在我的 View 中的时间选择器一起工作。

我找不到任何关于如何执行其中任何一个的示例。任何建议,将不胜感激。

public class MainActivity extends AppCompatActivity {

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

DatePicker simpleDatePicker = (DatePicker)findViewById(R.id.date_picker); // initiate a date picker

//Above API 26
// ????
simpleDatePicker.setOnDateChangedListener(new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// Do something
}
});

TimePicker simpleTimePicker = (TimePicker)findViewById(R.id.time_picker); // initiate a time picker

simpleTimePicker.setIs24HourView(true);// set to 24 hour

simpleTimePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
// Do somthing
}
});
}

public void cancelBTN(View view) {
}

public void saveBTN(View view) {
}
}


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<RelativeLayout
android:id="@+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>

<Button
android:id="@+id/cancelBTN"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:background="@color/gray"
android:onClick="cancelBTN"
android:text="Cancel"
android:textColor="@color/white" />

<TextView
android:id="@+id/Title"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="@color/gray"
android:gravity="center"
android:text="Select Date - Time"
android:textColor="@color/white"
android:textSize="20dp" />

<Button
android:id="@+id/saveBTN"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:layout_alignParentRight="true"
android:background="@color/gray"
android:onClick="saveBTN"
android:text="Save"
android:textColor="@color/white" />

<DatePicker
android:calendarViewShown="false"
android:datePickerMode="spinner"
android:id="@+id/date_picker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:layout_below="@+id/saveBTN"
/>

<TimePicker
android:timePickerMode="spinner"
android:id="@+id/time_picker"
android:layout_marginTop="15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:layout_below="@+id/date_picker"

/>


</RelativeLayout>


</RelativeLayout>

最佳答案

我找到了一种不用 DialogFragment 的方法。我添加了一个新函数并在初始化 TimePicker 后调用它。有关详细信息,请参阅下面的代码。

public class MainActivity extends AppCompatActivity {

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

DatePicker simpleDatePicker = (DatePicker)findViewById(R.id.date_picker); // initiate a date picker

//Above API 26
// ????
simpleDatePicker.setOnDateChangedListener(new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// Do something
}
});

TimePicker simpleTimePicker = (TimePicker)findViewById(R.id.time_picker); // initiate a time picker

simpleTimePicker.setIs24HourView(true);// set to 24 hour

setTimePickerInterval(simpleTimePicker);

simpleTimePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
// Do somthing
}
});
}

private void setTimePickerInterval(TimePicker timePicker) {
try {
int TIME_PICKER_INTERVAL = 5;
NumberPicker minutePicker = (NumberPicker) timePicker.findViewById(Resources.getSystem().getIdentifier(
"minute", "id", "android"));
minutePicker.setMinValue(0);
minutePicker.setMaxValue((60 / TIME_PICKER_INTERVAL) - 1);
List<String> displayedValues = new ArrayList<String>();
for (int i = 0; i < 60; i += TIME_PICKER_INTERVAL) {
displayedValues.add(String.format("%02d", i));
}
minutePicker.setDisplayedValues(displayedValues.toArray(new String[0]));
} catch (Exception e) {

}
}

public void cancelBTN(View view) {
}

public void saveBTN(View view) {
}
}

关于android - TimePicker 分钟间隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54784149/

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