Android Alertdialog(實現(xiàn)警告對話框)
在Android開發(fā)中,我們經(jīng)常會需要在Android界面上彈出一些對話框,比如詢問用戶或者讓用戶選擇。這些功能我們叫它Android Dialog對話框,AlertDialog實現(xiàn)方法為建造者模式。下面我們模擬卸載應用程序時彈出的最為普通的警告對話框,如下圖:
layout布局界面代碼示例:
<?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"> <Button android:text="卸載" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="show" android:id="@+id/button" /> </LinearLayout>
Java實現(xiàn)代碼:
import android.content.DialogInterface; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Toast; /** * Created by panchengjia on 2016/11/21. */ public class AlertDialogDemo extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.alterdialog); } public void show(View v){ //實例化建造者 AlertDialog.Builder builder = new AlertDialog.Builder(this); //設置警告對話框的標題 builder.setTitle("卸載"); //設置警告顯示的圖片 // builder.setIcon(android.R.drawable.ic_dialog_alert); //設置警告對話框的提示信息 builder.setMessage("確定卸載嗎"); //設置”正面”按鈕,及點擊事件 builder.setPositiveButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(AlertDialogDemo.this,"點擊了確定按鈕",Toast.LENGTH_SHORT).show(); } }); //設置“反面”按鈕,及點擊事件 builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(AlertDialogDemo.this,"點擊了取消按鈕",Toast.LENGTH_SHORT).show(); } }); //設置“中立”按鈕,及點擊事件 builder.setNeutralButton("等等看吧", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(AlertDialogDemo.this,"點擊了中立按鈕",Toast.LENGTH_SHORT).show(); } }); //顯示對話框 builder.show(); } }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- Android實現(xiàn)點擊AlertDialog上按鈕時不關閉對話框的方法
- Android中AlertDialog各種對話框的用法實例詳解
- Android使用AlertDialog實現(xiàn)的信息列表單選、多選對話框功能
- Android中AlertDialog 點擊按鈕后不關閉對話框的功能
- Android修改源碼解決Alertdialog觸摸對話框邊緣消失的問題
- Android 自定義AlertDialog對話框樣式
- Android對話框AlertDialog.Builder使用方法詳解
- ANDROID中自定義對話框AlertDialog使用示例
- android自定義AlertDialog對話框
- Android開發(fā)之AlertDialog實現(xiàn)彈出對話框
相關文章
Android通過AIDL在兩個APP之間Service通信
這篇文章主要為大家詳細介紹了Android通過AIDL在兩個APP之間Service通信,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05Android studio實現(xiàn)簡易的計算器功能
這篇文章主要為大家詳細介紹了Android studio實現(xiàn)簡易的計算器功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05Android Flutter實現(xiàn)興趣標簽選擇功能
我們在首次使用內(nèi)容類 App 的時候,不少都會讓我們選擇個人偏好,通過這些標簽選擇可以預先知道用戶的偏好信息。我們本篇就來看看 Flutter 如何實現(xiàn)興趣標簽的選擇,需要的可以參考一下2022-11-11解決android studio 打開java文件 內(nèi)容全變了的問題
這篇文章主要介紹了解決android studio 打開java文件 內(nèi)容全變了的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03Core Animation一些Demo總結(jié) (動態(tài)切換圖片、大轉(zhuǎn)盤、圖片折疊、進度條等動畫效果)
這篇文章主要介紹了Core Animation一些Demo總結(jié) (動態(tài)切換圖片、大轉(zhuǎn)盤、圖片折疊、進度條等動畫效果)的相關資料,需要的朋友可以參考下2016-02-02