Android Alertdialog(實(shí)現(xiàn)警告對(duì)話框)
在Android開發(fā)中,我們經(jīng)常會(huì)需要在Android界面上彈出一些對(duì)話框,比如詢問用戶或者讓用戶選擇。這些功能我們叫它Android Dialog對(duì)話框,AlertDialog實(shí)現(xiàn)方法為建造者模式。下面我們模擬卸載應(yīng)用程序時(shí)彈出的最為普通的警告對(duì)話框,如下圖:

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實(shí)現(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){
//實(shí)例化建造者
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//設(shè)置警告對(duì)話框的標(biāo)題
builder.setTitle("卸載");
//設(shè)置警告顯示的圖片
// builder.setIcon(android.R.drawable.ic_dialog_alert);
//設(shè)置警告對(duì)話框的提示信息
builder.setMessage("確定卸載嗎");
//設(shè)置”正面”按鈕,及點(diǎn)擊事件
builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(AlertDialogDemo.this,"點(diǎn)擊了確定按鈕",Toast.LENGTH_SHORT).show();
}
});
//設(shè)置“反面”按鈕,及點(diǎn)擊事件
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(AlertDialogDemo.this,"點(diǎn)擊了取消按鈕",Toast.LENGTH_SHORT).show();
}
});
//設(shè)置“中立”按鈕,及點(diǎn)擊事件
builder.setNeutralButton("等等看吧", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(AlertDialogDemo.this,"點(diǎn)擊了中立按鈕",Toast.LENGTH_SHORT).show();
}
});
//顯示對(duì)話框
builder.show();
}
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)點(diǎn)擊AlertDialog上按鈕時(shí)不關(guān)閉對(duì)話框的方法
- Android中AlertDialog各種對(duì)話框的用法實(shí)例詳解
- Android使用AlertDialog實(shí)現(xiàn)的信息列表單選、多選對(duì)話框功能
- Android中AlertDialog 點(diǎn)擊按鈕后不關(guān)閉對(duì)話框的功能
- Android修改源碼解決Alertdialog觸摸對(duì)話框邊緣消失的問題
- Android 自定義AlertDialog對(duì)話框樣式
- Android對(duì)話框AlertDialog.Builder使用方法詳解
- ANDROID中自定義對(duì)話框AlertDialog使用示例
- android自定義AlertDialog對(duì)話框
- Android開發(fā)之AlertDialog實(shí)現(xiàn)彈出對(duì)話框
相關(guān)文章
flutter實(shí)現(xiàn)發(fā)送驗(yàn)證碼功能
這篇文章主要為大家詳細(xì)介紹了flutter發(fā)送驗(yàn)證碼功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07
Android Compose 屬性動(dòng)畫使用探索詳解
這篇文章主要為大家介紹了Android Compose 屬性動(dòng)畫使用探索詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Android View滑動(dòng)的實(shí)現(xiàn)分析示例
View滑動(dòng)是Android實(shí)現(xiàn)自定義控件的基礎(chǔ),同時(shí)在開發(fā)中難免會(huì)遇到View的滑動(dòng)處理,其實(shí)不管是那種滑動(dòng)方法,基本思路是類似的;當(dāng)點(diǎn)擊事件傳到View時(shí),系統(tǒng)記下觸摸點(diǎn)的坐標(biāo),手指移動(dòng)時(shí)系統(tǒng)記下移動(dòng)后的左邊并算出偏移量,通過偏移量來修改View的坐標(biāo)2022-08-08
Android實(shí)現(xiàn)聯(lián)動(dòng)下拉框二級(jí)地市聯(lián)動(dòng)下拉框功能
這篇文章主要介紹了Android實(shí)現(xiàn)聯(lián)動(dòng)下拉框二級(jí)地市聯(lián)動(dòng)下拉框功能,本文給大家分享思路步驟,給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-12-12
Android實(shí)現(xiàn)本地Service方法控制音樂播放
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)本地Service方法控制音樂播放,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03
Android中TelephonyManager用法實(shí)例
這篇文章主要介紹了Android中TelephonyManager用法,結(jié)合實(shí)例形式分析了TelephonyManager類的功能,使用技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-03-03
Android報(bào)錯(cuò)Error:Could not find com.android.tools.build:gradle
這篇文章主要介紹了Android Studio報(bào)錯(cuò)Error:Could not find com.android.tools.build:gradle:4.1解決辦法,碰到該問題的同學(xué)快過來看看吧2021-08-08

