運作有問題或需要源碼請點贊關注收藏後評論區留言~~~
一、提醒對話框
AlertDialog名為提醒對話框,它是Android中最常用的對話框,可以完成常見的互動操作,例如提示,确認,選擇等功能,由于AlertDialog沒有公開的構造方法,是以必須借助建造起AlertDialog.Builder才能完成參數設定
通過AlertDialog.Builder設定完對話框參數,還需調用建造起的create方法才能生成對話框執行個體,最後調用對話框執行個體的show方法 在頁面上彈出提醒對話框 效果如下 點選不同的按鈕會彈出不同的對話框以及資訊
AlertDialogActivity類代碼如下
package com.example.chapter05;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class AlertDialogActivity extends AppCompatActivity implements View.OnClickListener {
private TextView tv_alert; // 聲明一個文本視圖對象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alert_dialog);
// 給btn_alert設定點選監聽器,一旦使用者點選按鈕,就觸發監聽器的onClick方法
findViewById(R.id.btn_alert).setOnClickListener(this);
// 從布局檔案中擷取名叫tv_alert的文本視圖
tv_alert = findViewById(R.id.tv_alert);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btn_alert) {
// 建立提醒對話框的建造器
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("尊敬的使用者"); // 設定對話框的标題文本
builder.setMessage("你真的要解除安裝我嗎?"); // 設定對話框的内容文本
// 設定對話框的肯定按鈕文本及其點選監聽器
builder.setPositiveButton("殘忍解除安裝", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
tv_alert.setText("雖然依依不舍,但是隻能離開了");
}
});
// 設定對話框的否定按鈕文本及其點選監聽器
builder.setNegativeButton("我再想想", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
tv_alert.setText("讓我再陪你三百六十五個日夜");
}
});
AlertDialog alert = builder.create(); // 根據建造器建構提醒對話框對象
alert.show(); // 顯示提醒對話框
}
}
}
activity_alert_dialogXML檔案代碼如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn_alert"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="彈出提醒對話框"
android:textColor="@color/black"
android:textSize="17sp" />
<TextView
android:id="@+id/tv_alert"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textColor="@color/black"
android:textSize="17sp" />
</LinearLayout>
日期對話框DatePickerDialog
系統提供了專門的提起選擇器DatePicker,供使用者選擇具體的年月日,不過DatePicker并非彈窗模式,而是在目前頁面占據一塊區域,并且不會自動關閉。按習慣來說,日期控件應該彈出對話框,選擇完日期就要自動關閉對話框,是以很少直接在界面上顯示DatePicker,而是利用已經封裝好的日期選擇對話框DatePickerDialog 效果如下
DatePickerActivity類代碼如下
package com.example.chapter05;
import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Calendar;
@SuppressLint("DefaultLocale")
// 該頁面類實作了接口OnDateSetListener,意味着要重寫日期監聽器的onDateSet方法
public class DatePickerActivity extends AppCompatActivity implements
View.OnClickListener, DatePickerDialog.OnDateSetListener {
private TextView tv_date; // 聲明一個文本視圖對象
private DatePicker dp_date; // 聲明一個日期選擇器對象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_date_picker);
tv_date = findViewById(R.id.tv_date);
// 從布局檔案中擷取名叫dp_date的日期選擇器
dp_date = findViewById(R.id.dp_date);
findViewById(R.id.btn_date).setOnClickListener(this);
findViewById(R.id.btn_ok).setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btn_date) {
// 擷取月曆的一個執行個體,裡面包含了目前的年月日
Calendar calendar = Calendar.getInstance();
// 建構一個日期對話框,該對話框已經內建了日期選擇器。
// DatePickerDialog的第二個構造參數指定了日期監聽器
DatePickerDialog dialog = new DatePickerDialog(this, this,
calendar.get(Calendar.YEAR), // 年份
calendar.get(Calendar.MONTH), // 月份
calendar.get(Calendar.DAY_OF_MONTH)); // 日子
dialog.show(); // 顯示日期對話框
} else if (v.getId() == R.id.btn_ok) {
// 擷取日期選擇器dp_date設定的年月份
String desc = String.format("您選擇的日期是%d年%d月%d日",
dp_date.getYear(), dp_date.getMonth() + 1, dp_date.getDayOfMonth());
tv_date.setText(desc);
}
}
// 一旦點選日期對話框上的确定按鈕,就會觸發監聽器的onDateSet方法
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// 擷取日期對話框設定的年月份
String desc = String.format("您選擇的日期是%d年%d月%d日",
year, monthOfYear + 1, dayOfMonth);
tv_date.setText(desc);
}
}
activity_date_pickerXML檔案代碼如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="請選擇日期"
android:textColor="@color/black"
android:textSize="17sp" />
<!-- datePickerMode取值spinner表示下拉框風格,取值calendar表示月曆風格 -->
<DatePicker
android:id="@+id/dp_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:calendarViewShown="false"
android:datePickerMode="spinner"
android:gravity="center"
android:spinnersShown="true" />
<Button
android:id="@+id/btn_ok"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="确 定"
android:textColor="@color/black"
android:textSize="17sp" />
<TextView
android:id="@+id/tv_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:textColor="@color/black"
android:textSize="17sp" />
</LinearLayout>
時間對話框TimePickerDialog
既然有了日期選擇器,還得有對應的時間選擇器,同樣實際開發中也很少直接使用TimePicker,而是用封裝好的時間選擇對話框TimePickerDialog, 同樣是在界面上内嵌 其中小時與分鐘按照鐘表風格展示 效果如下
TimePickerActivity類代碼如下
package com.example.chapter05;
import android.annotation.SuppressLint;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.TimePicker;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Calendar;
@SuppressLint("DefaultLocale")
// 該頁面類實作了接口OnTimeSetListener,意味着要重寫時間監聽器的onTimeSet方法
public class TimePickerActivity extends AppCompatActivity implements
View.OnClickListener, TimePickerDialog.OnTimeSetListener {
private TextView tv_time; // 聲明一個文本視圖對象
private TimePicker tp_time; // 聲明一個時間選擇器對象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_time_picker);
tv_time = findViewById(R.id.tv_time);
// 從布局檔案中擷取名叫tp_time的時間選擇器
tp_time = findViewById(R.id.tp_time);
findViewById(R.id.btn_time).setOnClickListener(this);
findViewById(R.id.btn_ok).setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btn_time) {
// 擷取月曆的一個執行個體,裡面包含了目前的時分秒
Calendar calendar = Calendar.getInstance();
// 建構一個時間對話框,該對話框已經內建了時間選擇器。
// TimePickerDialog的第二個構造參數指定了時間監聽器
TimePickerDialog dialog = new TimePickerDialog(this, this,
calendar.get(Calendar.HOUR_OF_DAY), // 小時
calendar.get(Calendar.MINUTE), // 分鐘
true); // true表示24小時制,false表示12小時制
dialog.show(); // 顯示時間對話框
} else if (v.getId() == R.id.btn_ok) {
// 擷取時間選擇器tp_time設定的小時和分鐘
String desc = String.format("您選擇的時間是%d時%d分",
tp_time.getCurrentHour(), tp_time.getCurrentMinute());
tv_time.setText(desc);
}
}
// 一旦點選時間對話框上的确定按鈕,就會觸發監聽器的onTimeSet方法
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// 擷取時間對話框設定的小時和分鐘
String desc = String.format("您選擇的時間是%d時%d分", hourOfDay, minute);
tv_time.setText(desc);
}
}
activity_time_pickerXML檔案代碼如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="請選擇時間"
android:textColor="@color/black"
android:textSize="17sp" />
<!-- timePickerMode取值spinner表示下拉框風格,取值clock表示鐘表風格 -->
<TimePicker
android:id="@+id/tp_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:calendarViewShown="false"
android:timePickerMode="spinner"
android:gravity="center"
android:spinnersShown="true" />
<Button
android:id="@+id/btn_ok"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="确 定"
android:textColor="@color/black"
android:textSize="17sp" />
<TextView
android:id="@+id/tv_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:textColor="@color/black"
android:textSize="17sp" />
</LinearLayout>
創作不易 覺得有幫助請點贊關注收藏