目錄
一、提示資訊對話框:
二、單選對話框:
三、多選對話框:
四、自定義對話框:
演示專案完整代碼:
一、提示資訊對話框:

//顯示提示訊息對話框
private void showMsgDialog() {
//創建AlertDialog構造器Builder物件,AlertDialog建議使用android.support.v7.app包下的,
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//設定對話框標題
builder.setTitle("提示資訊對話框");
//設定提示資訊
builder.setMessage("是否確定退出!");
//設定對話框圖示
builder.setIcon(R.mipmap.ic_launcher);
//添加確定按鈕
builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//添加確定按鈕點擊的處理代碼
Toast.makeText(MainActivity.this, "點擊了確定!", Toast.LENGTH_SHORT).show();
}
});
//添加取消按鈕
builder.setNegativeButton("取消",null);
//創建并顯示對話框
builder.show();
}
二、單選對話框:

//顯示單選對話框
private void showSingleChoiceDialog() {
//創建AlertDialog構造器Builder物件,AlertDialog建議使用android.support.v7.app包下的,
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//設定對話框標題
builder.setTitle("請選擇性別");
//設定對話框圖示
builder.setIcon(R.mipmap.ic_launcher);
final String[] sexs = new String[]{"男", "女"};
//設定單選選項
builder.setSingleChoiceItems(sexs, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "您選擇了:"+sexs[which], Toast.LENGTH_SHORT).show();
}
});
//添加確定按鈕
builder.setPositiveButton("確定", null);
//創建并顯示對話框
builder.show();
}
三、多選對話框:

//顯示多選對話框
private void showMultiChoiceDialog() {
//創建AlertDialog構造器Builder物件,AlertDialog建議使用android.support.v7.app包下的,
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//設定對話框標題
builder.setTitle("請選擇傳感器");
//設定對話框圖示
builder.setIcon(R.mipmap.ic_launcher);
final String[] sensors = new String[]{"溫濕度傳感器", "光照傳感器","CO2傳感器","風速傳感器"};
//設定多選選項
builder.setMultiChoiceItems(sensors, new boolean[]{false,true,true,false}, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
}
});
//添加確定按鈕
builder.setPositiveButton("確定", null);
//創建并顯示對話框
builder.show();
}
四、自定義對話框:
自定義對話框布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#2372c1"
android:gravity="center"
android:text="提示"
android:padding="5dp"
android:textColor="#fff"
android:textSize="25sp" />
<TextView
android:id="@+id/tvContent"
android:layout_width="match_parent"
android:layout_height="200dp"
android:gravity="center"
android:text="自定義對話框內容" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#c0c0c0"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="@+id/btnOk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="確定" />
<Button
android:id="@+id/btnCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="取消" />
</LinearLayout>
</LinearLayout>
MyDialog.Java
package com.newland.dialogdemo;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
public class MyDialog extends Dialog {
private String title;
private String content;
private TextView tvTitle;
private TextView tvContent;
private Button btnOk;
private Button btnCancel;
public MyDialog(Context context) {
super(context);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//去除標題
requestWindowFeature(Window.FEATURE_NO_TITLE);
//引入自定義對話框布局
setContentView(R.layout.my_dialog);
//初始化控制元件
initView();
//設定標題
tvTitle.setText(title);
//設定內容
tvContent.setText(content);
//注冊確認按鈕監聽器
btnOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//點擊確認時的操作
}
});
//注冊取消按鈕監聽器
btnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//關閉對話框
dismiss();
}
});
}
//初始化控制元件
private void initView() {
tvTitle = findViewById(R.id.tvTitle);
tvContent = findViewById(R.id.tvContent);
btnOk = findViewById(R.id.btnOk);
btnCancel = findViewById(R.id.btnCancel);
}
public void setTitle(String title) {
this.title = title;
}
public void setContent(String content) {
this.content = content;
}
}
MainActivity.Java
//顯示自定義對話框
private void showCustomDialog() {
MyDialog dialog = new MyDialog(this);
dialog.setTitle("自定義對話框");
dialog.setContent("你好!這里是自定義對話框!");
dialog.show();
}
演示專案完整代碼:

<?xml version="1.0" encoding="utf-8"?>
<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/btnShowMsgDlg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提示資訊對話框"/>
<Button
android:id="@+id/btnShowSingleDlg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="單選對話框"/>
<Button
android:id="@+id/btnShowMultiDlg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="多選對話框"/>
<Button
android:id="@+id/btnShowCustomDlg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定義對話框"/>
</LinearLayout>
package com.newland.dialogdemo;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
//顯示提示資訊對話框按鈕
private Button btnShowMsgDlg;
//顯示單選對話框按鈕
private Button btnShowSingleDlg;
//顯示多選對話框按鈕
private Button btnShowMultiDlg;
//顯示自定義對話框按鈕
private Button btnShowCustomDlg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化視圖以及監聽器
initView();
}
//初始化視圖以及監聽器
private void initView() {
//初始化控制元件
btnShowMsgDlg = findViewById(R.id.btnShowMsgDlg);
btnShowSingleDlg = findViewById(R.id.btnShowSingleDlg);
btnShowMultiDlg = findViewById(R.id.btnShowMultiDlg);
btnShowCustomDlg = findViewById(R.id.btnShowCustomDlg);
//注冊按鈕監聽器
btnShowMsgDlg.setOnClickListener(this);
btnShowSingleDlg.setOnClickListener(this);
btnShowMultiDlg.setOnClickListener(this);
btnShowCustomDlg.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnShowMsgDlg: //顯示提示資訊對話框按鈕
showMsgDialog();
break;
case R.id.btnShowSingleDlg: //顯示單選對話框按鈕
showSingleChoiceDialog();
break;
case R.id.btnShowMultiDlg: //顯示多選對話框按鈕
showMultiChoiceDialog();
break;
case R.id.btnShowCustomDlg: //顯示自定義對話框按鈕
showCustomDialog();
break;
}
}
//顯示提示訊息對話框
private void showMsgDialog() {
//創建AlertDialog構造器Builder物件,AlertDialog建議使用android.support.v7.app包下的,
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//設定對話框標題
builder.setTitle("提示資訊對話框");
//設定提示資訊
builder.setMessage("是否確定退出!");
//設定對話框圖示
builder.setIcon(R.mipmap.ic_launcher);
//添加確定按鈕
builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//添加確定按鈕點擊的處理代碼
Toast.makeText(MainActivity.this, "點擊了確定!", Toast.LENGTH_SHORT).show();
}
});
//添加取消按鈕
builder.setNegativeButton("取消",null);
//創建并顯示對話框
builder.show();
}
//顯示單選對話框
private void showSingleChoiceDialog() {
//創建AlertDialog構造器Builder物件,AlertDialog建議使用android.support.v7.app包下的,
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//設定對話框標題
builder.setTitle("請選擇性別");
//設定對話框圖示
builder.setIcon(R.mipmap.ic_launcher);
final String[] sexs = new String[]{"男", "女"};
//設定單選選項
builder.setSingleChoiceItems(sexs, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "您選擇了:"+sexs[which], Toast.LENGTH_SHORT).show();
}
});
//添加確定按鈕
builder.setPositiveButton("確定", null);
//創建并顯示對話框
builder.show();
}
//顯示多選對話框
private void showMultiChoiceDialog() {
//創建AlertDialog構造器Builder物件,AlertDialog建議使用android.support.v7.app包下的,
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//設定對話框標題
builder.setTitle("請選擇傳感器");
//設定對話框圖示
builder.setIcon(R.mipmap.ic_launcher);
final String[] sensors = new String[]{"溫濕度傳感器", "光照傳感器","CO2傳感器","風速傳感器"};
//設定多選選項
builder.setMultiChoiceItems(sensors, new boolean[]{false,true,true,false}, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
}
});
//添加確定按鈕
builder.setPositiveButton("確定", null);
//創建并顯示對話框
builder.show();
}
//顯示自定義對話框
private void showCustomDialog() {
MyDialog dialog = new MyDialog(this);
dialog.setTitle("自定義對話框");
dialog.setContent("你好!這里是自定義對話框!");
dialog.show();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/241042.html
標籤:其他
上一篇:適合新手前端學習資料(第十五天)
