Android自定義對話框,Android各種dialog

 2023-11-07 阅读 20 评论 0

摘要:Android Dialog之 提示對話框,單選對話框,復選對話框,列表對話框,日期對話框,時間對話框,自定義對話框,以及進度對話框。 Android自定義對話框、上代碼: import java.util.Calendar;import android.app.Activity;imp

Android Dialog之 提示對話框,單選對話框,復選對話框,列表對話框,日期對話框,時間對話框,自定義對話框,以及進度對話框。

Android自定義對話框、上代碼:

import java.util.Calendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;

import com.jbit.dialog_demo.R;

public class MainActivity extends Activity {
?? ?private Button btnAlert;
?? ?private Button btnList;
?? ?private Button btnCheckBox;
?? ?private Button btnRadio;
?? ?private Button btnProgress;
?? ?private Button btnDate;
?? ?private Button btnTime;
?? ?private Button btnUserDefined;
?? ?private ProgressDialog dialog;
?? ?private Dialog userDialog;
?? ?// 句柄
?? ?private Handler headler = new Handler();

?? ?@Override
?? ?protected void onCreate(Bundle savedInstanceState) {
?? ??? ?super.onCreate(savedInstanceState);
?? ??? ?setContentView(R.layout.activity_main);
?? ??? ?btnAlert = (Button) findViewById(R.id.main_bt_alert_nom);
?? ??? ?btnList = (Button) findViewById(R.id.main_bt_alert_list);
?? ??? ?btnCheckBox = (Button) findViewById(R.id.main_bt_alert_checkbox);
?? ??? ?btnRadio = (Button) findViewById(R.id.main_bt_alert_radio);
?? ??? ?btnProgress = (Button) findViewById(R.id.main_bt_alert_poion);
?? ??? ?btnDate = (Button) findViewById(R.id.main_bt_alert_date);
?? ??? ?btnTime = (Button) findViewById(R.id.main_bt_alert_time);
?? ??? ?btnUserDefined = (Button) findViewById(R.id.main_bt_alert_userdefinal);
?? ??? ?btnAlert.setOnClickListener(listener);
?? ??? ?btnList.setOnClickListener(listener);
?? ??? ?btnCheckBox.setOnClickListener(listener);
?? ??? ?btnRadio.setOnClickListener(listener);
?? ??? ?btnProgress.setOnClickListener(listener);
?? ??? ?btnDate.setOnClickListener(listener);
?? ??? ?btnTime.setOnClickListener(listener);
?? ??? ?btnUserDefined.setOnClickListener(listener);
?? ??? ?// btnTime.setOnClickListener(this);

?? ?}

?? ?private OnClickListener listener = new OnClickListener() {

?? ??? ?@Override
?? ??? ?public void onClick(View v) {
?? ??? ??? ?switch (v.getId()) {
?? ??? ??? ?case R.id.main_bt_alert_nom:
?? ??? ??? ??? ?showAlert();
?? ??? ??? ??? ?break;
?? ??? ??? ?case R.id.main_bt_alert_checkbox:
?? ??? ??? ??? ?showCheckBox();
?? ??? ??? ??? ?break;
?? ??? ??? ?case R.id.main_bt_alert_date:
?? ??? ??? ??? ?showDate();
?? ??? ??? ??? ?break;
?? ??? ??? ?case R.id.main_bt_alert_list:
?? ??? ??? ??? ?showList();
?? ??? ??? ??? ?break;
?? ??? ??? ?case R.id.main_bt_alert_radio:
?? ??? ??? ??? ?showRadio();
?? ??? ??? ??? ?break;
?? ??? ??? ?case R.id.main_bt_alert_poion:
?? ??? ??? ??? ?showPoion();
?? ??? ??? ??? ?break;
?? ??? ??? ?case R.id.main_bt_alert_time:
?? ??? ??? ??? ?showTime();
?? ??? ??? ??? ?break;
?? ??? ??? ?case R.id.main_bt_alert_userdefinal:
?? ??? ??? ??? ?showUserDefine();
?? ??? ??? ??? ?break;
?? ??? ??? ?case R.id.dialog_btn_ok:
?? ??? ??? ??? ?Toast.makeText(MainActivity.this, "你點擊了確定按鈕",
?? ??? ??? ??? ??? ??? ?Toast.LENGTH_SHORT).show();
?? ??? ??? ??? ?userDialog.dismiss();
?? ??? ??? ??? ?break;
?? ??? ??? ?case R.id.dialog_btn_clean:
?? ??? ??? ??? ?// 點擊取消時關閉對話框
?? ??? ??? ??? ?userDialog.dismiss();
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ?}

?? ??? ?private void showUserDefine() {
?? ??? ??? ?// 獲得加載視圖的layout
?? ??? ??? ?LayoutInflater lif = LayoutInflater.from(MainActivity.this);
?? ??? ??? ?// 加載視圖
?? ??? ??? ?View v = lif.inflate(R.layout.dialog_view, null);
?? ??? ??? ?// 為視圖內的按鈕添加事件操作
?? ??? ??? ?Button btnOk = (Button) v.findViewById(R.id.dialog_btn_ok);
?? ??? ??? ?Button btnClean = (Button) v.findViewById(R.id.dialog_btn_clean);
?? ??? ??? ?btnOk.setOnClickListener(listener);

?? ??? ??? ?btnClean.setOnClickListener(listener);
?? ??? ??? ?// 創建對話框
?? ??? ??? ?userDialog = new AlertDialog.Builder(MainActivity.this).setView(v)
?? ??? ??? ??? ??? ?.create();
?? ??? ??? ?// 設置點擊邊緣
?? ??? ??? ?userDialog.setCanceledOnTouchOutside(false);
?? ??? ??? ?// 顯示對話框
?? ??? ??? ?userDialog.show();
?? ??? ?}

?? ??? ?private void showTime() {
?? ??? ??? ?TimePickerDialog dialog = new TimePickerDialog(MainActivity.this,
?? ??? ??? ??? ??? ?new OnTimeSetListener() {

?? ??? ??? ??? ??? ??? ?@Override
?? ??? ??? ??? ??? ??? ?public void onTimeSet(TimePicker view, int hour,
?? ??? ??? ??? ??? ??? ??? ??? ?int minute) {
?? ??? ??? ??? ??? ??? ??? ?// TODO 自動生成的方法存根

?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?}, Calendar.HOUR, Calendar.MINUTE, true);
?? ??? ??? ?dialog.show();

?? ??? ?}

?? ??? ?/**
?? ??? ? * 進度條
?? ??? ? */
?? ??? ?private void showPoion() {
?? ??? ??? ?dialog = new ProgressDialog(MainActivity.this);
?? ??? ??? ?dialog.setMessage("Loading...");
?? ??? ??? ?dialog.setButton(DialogInterface.BUTTON1, new String("取消"),
?? ??? ??? ??? ??? ?new DialogInterface.OnClickListener() {

?? ??? ??? ??? ??? ??? ?@Override
?? ??? ??? ??? ??? ??? ?public void onClick(DialogInterface dialog, int which) {
?? ??? ??? ??? ??? ??? ??? ?headler.removeCallbacks(r);
?? ??? ??? ??? ??? ??? ??? ?dialog.dismiss();
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?});
?? ??? ??? ?dialog.setMax(100);
?? ??? ??? ?dialog.setProgress(50);
?? ??? ??? ?dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
?? ??? ??? ?dialog.show();
?? ??? ??? ?headler.postDelayed(r, 100);

?? ??? ?}

?? ??? ?private void showDate() {
?? ??? ??? ?Calendar calendar = Calendar.getInstance();
?? ??? ??? ?DatePickerDialog dia = new DatePickerDialog(MainActivity.this,
?? ??? ??? ??? ??? ?new OnDateSetListener() {

?? ??? ??? ??? ??? ??? ?@Override
?? ??? ??? ??? ??? ??? ?public void onDateSet(DatePicker view, int year,
?? ??? ??? ??? ??? ??? ??? ??? ?int month, int day) {
?? ??? ??? ??? ??? ??? ??? ?Toast.makeText(MainActivity.this, year + "-"
?? ??? ??? ??? ??? ??? ??? ??? ??? ?+ month + "-" + day, Toast.LENGTH_SHORT);
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?}, calendar.YEAR, calendar.MONTH, calendar.DAY_OF_MONTH);
?? ??? ??? ?dia.show();
?? ??? ?}

?? ??? ?private void showCheckBox() {
?? ??? ??? ?Dialog dialog = new AlertDialog.Builder(MainActivity.this)
?? ??? ??? ??? ??? ?.setTitle("多選對話框")
?? ??? ??? ??? ??? ?.setMultiChoiceItems(
?? ??? ??? ??? ??? ??? ??? ?new String[] { "item0", "item1", "item2" },
?? ??? ??? ??? ??? ??? ??? ?new boolean[] { true, false, false },
?? ??? ??? ??? ??? ??? ??? ?new DialogInterface.OnMultiChoiceClickListener() {

?? ??? ??? ??? ??? ??? ??? ??? ?@Override
?? ??? ??? ??? ??? ??? ??? ??? ?public void onClick(
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?DialogInterface dialogInterface,
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?int index, boolean isChecked) {

?? ??? ??? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ??? ?}).show();
?? ??? ?}

?? ??? ?private void showRadio() {
?? ??? ??? ?Dialog dialog = new AlertDialog.Builder(MainActivity.this)
?? ??? ??? ??? ??? ?.setTitle("單選對話框")
?? ??? ??? ??? ??? ?.setSingleChoiceItems(
?? ??? ??? ??? ??? ??? ??? ?new String[] { "item1", "item2", "item3" }, 0,
?? ??? ??? ??? ??? ??? ??? ?dialogClick).show();
?? ??? ?}

?? ??? ?private void showList() {
?? ??? ??? ?Dialog dialog = new AlertDialog.Builder(MainActivity.this)
?? ??? ??? ??? ??? ?.setTitle("列表對話框")
?? ??? ??? ??? ??? ?.setItems(new String[] { "item1", "item2", "item3" },
?? ??? ??? ??? ??? ??? ??? ?dialogClick).show();
?? ??? ?}

?? ??? ?private void showAlert() {
?? ??? ??? ?Dialog dialog = new AlertDialog.Builder(MainActivity.this)
?? ??? ??? ??? ??? ?.setIcon(R.drawable.ic_launcher).setMessage("這是一個提示框")
?? ??? ??? ??? ??? ?.setTitle("提示").setPositiveButton("取消", dialogClick)
?? ??? ??? ??? ??? ?.setNeutralButton("pass", dialogClick)
?? ??? ??? ??? ??? ?.setNegativeButton("ok", dialogClick).show();
?? ??? ?}

?? ??? ?private DialogInterface.OnClickListener dialogClick = new DialogInterface.OnClickListener() {

?? ??? ??? ?@Override
?? ??? ??? ?public void onClick(DialogInterface dialogInterface, int index) {
?? ??? ??? ??? ?// 顯示一個對話框顯示內容
?? ??? ??? ??? ?// 第一個參數上下文
?? ??? ??? ??? ?// 第二個參數提示的內容
?? ??? ??? ??? ?// 第三個參數 顯示的時間
?? ??? ??? ??? ?Toast.makeText(MainActivity.this, String.valueOf(index),
?? ??? ??? ??? ??? ??? ?Toast.LENGTH_LONG).show();
?? ??? ??? ?}
?? ??? ?};
?? ?};
?? ?private Runnable r = new Runnable() {
?? ??? ?@Override
?? ??? ?public void run() {

?? ??? ??? ?if (dialog != null) {
?? ??? ??? ??? ?if (dialog.getProgress() >= 100) {
?? ??? ??? ??? ??? ?dialog.dismiss();
?? ??? ??? ??? ??? ?Toast.makeText(MainActivity.this, "卸載完成。。。",
?? ??? ??? ??? ??? ??? ??? ?Toast.LENGTH_SHORT).show();
?? ??? ??? ??? ??? ?return;
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ??? ?if (dialog != null) {
?? ??? ??? ??? ?dialog.setProgress(dialog.getProgress() + 1);

?? ??? ??? ?}
?? ??? ??? ?headler.postDelayed(r, 100);
?? ??? ?}
?? ?};

?

頁面:

<?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/main_bt_alert_nom"
??????? android:layout_width="wrap_content"
??????? android:layout_height="wrap_content"
??????? android:text="提示對話框"
??????? android:textColor="#000000"
??????? android:textSize="20sp" />

??? <Button
??????? android:id="@+id/main_bt_alert_radio"
??????? android:layout_width="wrap_content"
??????? android:layout_height="wrap_content"
??????? android:text="單選對話框"
??????? android:textColor="#000000"
??????? android:textSize="20sp" />

??? <Button
??????? android:id="@+id/main_bt_alert_checkbox"
??????? android:layout_width="wrap_content"
??????? android:layout_height="wrap_content"
??????? android:text="復選對話框"
??????? android:textColor="#000000"
??????? android:textSize="20sp" />

??? <Button
??????? android:id="@+id/main_bt_alert_list"
??????? android:layout_width="wrap_content"
??????? android:layout_height="wrap_content"
??????? android:text="列表對話框"
??????? android:textColor="#000000"
??????? android:textSize="20sp" />

??? <Button
??????? android:id="@+id/main_bt_alert_date"
??????? android:layout_width="wrap_content"
??????? android:layout_height="wrap_content"
??????? android:text="日期對話框"
??????? android:textColor="#000000"
??????? android:textSize="20sp" />

??? <Button
??????? android:id="@+id/main_bt_alert_time"
??????? android:layout_width="wrap_content"
??????? android:layout_height="wrap_content"
??????? android:text="時間對話框"
??????? android:textColor="#000000"
??????? android:textSize="20sp" />

??? <Button
??????? android:id="@+id/main_bt_alert_userdefinal"
??????? android:layout_width="wrap_content"
??????? android:layout_height="wrap_content"
??????? android:text="自定義對話框"
??????? android:textColor="#000000"
??????? android:textSize="20sp" />
??? <Button
??????? android:id="@+id/main_bt_alert_poion"
??????? android:layout_width="wrap_content"
??????? android:layout_height="wrap_content"
??????? android:text="進度條對話框"
??????? android:textColor="#000000"
??????? android:textSize="20sp" />

</LinearLayout>

}

轉載于:https://www.cnblogs.com/xinyigebi/p/xinyigebi.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/5/167755.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息