android基礎-Toast提示框、日曆檢視(CalendarView)元件、日期、時間選擇器DatePicker和TimerPicker等
阿新 • • 發佈:2019-01-30
1. Toast提示框
// 建立一個Toast提示資訊
Toast toast = Toast.makeText(MainActivity.this
, "簡單的提示資訊"
, Toast.LENGTH_SHORT);
toast.show();
2. 日曆檢視(CalendarView)元件
例項:選擇您的生日
public class MainActivity extends AppCompatActivity {
CalendarView cv;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cv = (CalendarView)findViewById(R.id.calendarView);
// 為CalendarView元件的日期改變事件新增事件監聽器
cv.setOnDateChangeListener(new CalendarView.OnDateChangeListener()
{
@Override
public void onSelectedDayChange(CalendarView view, int year,
int month, int dayOfMonth)
{
// 使用Toast顯示使用者選擇的日期
Toast.makeText(MainActivity.this ,
"你生日是" + year + "年" + month + "月"
+ dayOfMonth + "日",
Toast.LENGTH_SHORT).show();
}
});
}
}
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="選擇您的生日:"/>
<!-- 設定以星期二作為每週第一天
設定該元件總共顯示4個星期
並對該元件的日期時間進行了定製 -->
<CalendarView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:firstDayOfWeek="3"
android:shownWeekCount="4"
android:selectedWeekBackgroundColor="#aff"
android:focusedMonthDateColor="#f00"
android:weekSeparatorLineColor="#ff0"
android:unfocusedMonthDateColor="#f9f"
android:id="@+id/calendarView" />
</LinearLayout>
3. 日期、時間選擇器DatePicker和TimerPicker
例項:使用者選擇日期和時間
// 定義5個記錄當前時間的變數
private int year;
private int month;
private int day;
private int hour;
private int minute;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DatePicker datePicker = (DatePicker)
findViewById(R.id.datePicker);
TimePicker timePicker = (TimePicker)
findViewById(R.id.timePicker);
// 獲取當前的年、月、日、小時、分鐘
Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
hour = c.get(Calendar.HOUR);
minute = c.get(Calendar.MINUTE);
// 初始化DatePicker元件,初始化時指定監聽器
datePicker.init(year, month, day, new OnDateChangedListener()
{
@Override
public void onDateChanged(DatePicker arg0, int year
, int month, int day)
{
MainActivity.this.year = year;
MainActivity.this.month = month;
MainActivity.this.day = day;
// 顯示當前日期、時間
showDate(year, month, day, hour, minute);
}
});
timePicker.setEnabled(true);
// 為TimePicker指定監聽器
timePicker.setOnTimeChangedListener(new OnTimeChangedListener()
{
@Override
public void onTimeChanged(TimePicker view
, int hourOfDay, int minute)
{
MainActivity.this.hour = hourOfDay;
MainActivity.this.minute = minute;
// 顯示當前日期、時間
showDate(year, month, day, hour, minute);
}
});
}
// 定義在EditText中顯示當前日期、時間的方法
private void showDate(int year, int month
, int day, int hour, int minute)
{
EditText show = (EditText) findViewById(R.id.show);
show.setText("您的購買日期為:" + year + "年"
+ (month + 1) + "月" + day + "日 "
+ hour + "時" + minute + "分");
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="選擇購買本書的具體時間"/>
<!-- 定義一個DatePicker元件 -->
<DatePicker android:id="@+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_gravity="center_horizontal"
android:startYear="2000"
android:endYear="2016"
android:calendarViewShown="true"
android:spinnersShown="true"/>
<!-- 定義一個TimePicker元件 -->
<TimePicker android:id="@+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_gravity="center_horizontal"/>
<!-- 顯示使用者輸入日期、時間的控制元件 -->
<EditText android:id="@+id/show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:editable="false"
android:cursorVisible="false"/>
</LinearLayout>