日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

AlertDialog(对话框)的基本使用

發布時間:2025/4/5 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 AlertDialog(对话框)的基本使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

AlertDialog可以在當前的界面彈出一個對話框,這個對話框是置頂于所有界面元素上的,能夠屏蔽掉其他控件的交互能力,因此,AlertDialog一般都是用于提示一些非常重要的內容或者警告信息。比如為了防止用戶誤刪重要內容,在刪除前彈出一個確認對話框。下面我們來學習一下它的用法。

效果圖:

?

?

?

?

activity_main.xml代碼:

<?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"><Buttonandroid:id="@+id/btn1"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="彈出對話框"android:textAllCaps="false" /></LinearLayout>

?

MainActivity.java代碼:

package com.example.administrator.activitydemo;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.Button; import android.widget.Toast;public class MainActivity extends AppCompatActivity implements View.OnClickListener {private Button btn1;@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();//初始化UI控件}private void initView() {btn1 = (Button) findViewById(R.id.btn1);btn1.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.btn1:function1();//彈出對話框break;default:break;}}private void function1() {AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);builder.setTitle("This is Dialog");builder.setMessage("Something important");builder.setCancelable(false);builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(MainActivity.this, "OK", Toast.LENGTH_SHORT).show();}});builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(MainActivity.this, "Cancel", Toast.LENGTH_SHORT).show();}});builder.show();}}

?

步驟1:聲名控件

步驟2:初始化控件

步驟3:設置點擊監聽

步驟4:定義function1()函數,用來封裝彈出對話框的功能

步驟5:在function()函數中實例化 AlertDialog.Builder類,用builder對象去設置一系列的函數來實現不同功能。

setTitle()? ? ? ? 設置對話框標題

setMessage()? ? ? ?設置對話框消息

setCancelable()? ? ?設置對話框是否可以取消

setPositiveButton() 設置對話框的確定按鈕

setNegativeButton()設置對話框的取消按鈕

show()? ? ? ? ? ?將對話框顯示出來

?

?

最后,如果不希望一條條的設置,也可以這么寫(這叫鏈式編程,因為每次設置之后返回值類型都是此類本身)。

AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this).setTitle("This is Dialog").setMessage("Something important").setCancelable(false).setPositiveButton("OK", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(MainActivity.this, "OK", Toast.LENGTH_SHORT).show();}}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(MainActivity.this, "Cancel", Toast.LENGTH_SHORT).show();}});builder.show();

?

?

?

?

?

?

?

總結

以上是生活随笔為你收集整理的AlertDialog(对话框)的基本使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。