会挽雕弓如满月,西北望,射天狼。这篇文章主要讲述Android Wheel日期和时间选择器获取值相关的知识,希望能为你提供帮助。
【Android Wheel日期和时间选择器获取值】大家好我在应用程序中使用Wheel lib,我想在点击按钮时在Toast中显示日期和时间。我该怎么做??
这是示例的代码,我只是在车轮中显示日期和时间,但我想在Toast中显示日期和时间。
public class Time2Activity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.time2_layout);
final WheelView hours = (WheelView) findViewById(R.id.hour);
NumericWheelAdapter hourAdapter = new NumericWheelAdapter(this, 0, 23);
hourAdapter.setItemResource(R.layout.wheel_text_item);
hourAdapter.setItemTextResource(R.id.text);
hours.setViewAdapter(hourAdapter);
final WheelView mins = (WheelView) findViewById(R.id.mins);
NumericWheelAdapter minAdapter = new NumericWheelAdapter(this, 0, 59, "%02d");
minAdapter.setItemResource(R.layout.wheel_text_item);
minAdapter.setItemTextResource(R.id.text);
mins.setViewAdapter(minAdapter);
mins.setCyclic(true);
final WheelView ampm = (WheelView) findViewById(R.id.ampm);
ArrayWheelAdapter<
String>
ampmAdapter =
new ArrayWheelAdapter<
String>
(this, new String[] {"AM", "PM"});
ampmAdapter.setItemResource(R.layout.wheel_text_item);
ampmAdapter.setItemTextResource(R.id.text);
ampm.setViewAdapter(ampmAdapter);
// set current time
Calendar calendar = Calendar.getInstance(Locale.UK);
hours.setCurrentItem(calendar.get(Calendar.HOUR));
mins.setCurrentItem(calendar.get(Calendar.MINUTE));
ampm.setCurrentItem(calendar.get(Calendar.AM_PM));
final WheelView day = (WheelView) findViewById(R.id.day);
day.setViewAdapter(new DayArrayAdapter(this, calendar));
}/**
* Day adapter
*
*/
private class DayArrayAdapter extends AbstractWheelTextAdapter {
// Count of days to be shown
private final int daysCount = 20;
// Calendar
Calendar calendar;
/**
* Constructor
*/
protected DayArrayAdapter(Context context, Calendar calendar) {
super(context, R.layout.time2_day, NO_RESOURCE);
this.calendar = calendar;
setItemTextResource(R.id.time2_monthday);
}@Override
public View getItem(int index, View cachedView, ViewGroup parent) {
int day = -daysCount/2 + index;
Calendar newCalendar = (Calendar) calendar.clone();
newCalendar.roll(Calendar.DAY_OF_YEAR, day);
View view = super.getItem(index, cachedView, parent);
TextView weekday = (TextView) view.findViewById(R.id.time2_weekday);
if (day == 0) {
weekday.setText("");
} else {
DateFormat format = new SimpleDateFormat("EEE");
weekday.setText(format.format(newCalendar.getTime()));
}TextView monthday = (TextView) view.findViewById(R.id.time2_monthday);
if (day == 0) {
monthday.setText("Today");
monthday.setTextColor(0xFF0000F0);
} else {
DateFormat format = new SimpleDateFormat("MMM d");
monthday.setText(format.format(newCalendar.getTime()));
monthday.setTextColor(0xFF111111);
}return view;
}@Override
public int getItemsCount() {
return daysCount + 1;
}@Override
protected CharSequence getItemText(int index) {
return "";
}
}
}
答案单击按钮。
public void onClickButtonSelect(View v){
int AMPM = ampm.getCurrentItem();
String AMOrPM;
if (AMPM == 0){
AMOrPM = "AM";
}else{
AMOrPM = "PM";
}
String hoursString = Integer.toString(hours.getCurrentItem());
String minutesString = Integer.toString(mins.getCurrentItem());
int day = -10/2 + day1.getCurrentItem();
Calendar newCalendar = (Calendar) calendar1.clone();
newCalendar.roll(Calendar.DAY_OF_YEAR, day);
DateFormat format = new SimpleDateFormat("MMM d");
Toast.makeText(getActivity(), format.format(newCalendar.getTime()).toString() + "-" + hoursString + "-" + minutesString + "-" + AMOrPM, 6000).show();
}
推荐阅读
- 在Android中加载大文本
- Android - 按下按钮时将textview添加到布局
- 如何在Android中绘制弯曲文本
- 如何在Android中设置NestedScrollView的最大高度()
- Android(String List中的变量总是返回0)
- 如何使用lambda表达式/缩短onclicklisteners android studio
- Android Textview无法显示特殊字符()
- 当系统范围的字体缩放时,Android TextView不会包装内容
- android长textview封面imageview