android實(shí)現(xiàn)彈出提示框
本文實(shí)例為大家分享了anadroid實(shí)現(xiàn)彈出提示框的具體代碼,供大家參考,具體內(nèi)容如下
提示框是利用AlertDialog實(shí)現(xiàn)的。
代碼:
(設(shè)置在button的點(diǎn)擊事件中)
new AlertDialog.Builder(MainActivity.this).setTitle("信息提示")//設(shè)置對話框標(biāo)題 .setMessage("是否需要更換xxx?") .setPositiveButton("是", new DialogInterface.OnClickListener() {//添加確定按鈕 @Override public void onClick(DialogInterface dialog, int which) {//確定按鈕的響應(yīng)事件,點(diǎn)擊事件沒寫,自己添加 } }).setNegativeButton("否", new DialogInterface.OnClickListener() {//添加返回按鈕 @Override public void onClick(DialogInterface dialog, int which) {//響應(yīng)事件,點(diǎn)擊事件沒寫,自己添加 } }).show();//在按鍵響應(yīng)事件中顯示此對話框 } });
實(shí)現(xiàn)效果:
完整代碼:
package com.example.myapplicationusealertdialog; import androidx.appcompat.app.AppCompatActivity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { Button bnt; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bnt = findViewById(R.id.button); bnt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { new AlertDialog.Builder(MainActivity.this).setTitle("信息提示")//設(shè)置對話框標(biāo)題 .setMessage("是否需要更換xxx?") .setPositiveButton("是", new DialogInterface.OnClickListener() {//添加確定按鈕 @Override public void onClick(DialogInterface dialog, int which) {//確定按鈕的響應(yīng)事件 } }).setNegativeButton("否", new DialogInterface.OnClickListener() {//添加返回按鈕 @Override public void onClick(DialogInterface dialog, int which) {//響應(yīng)事件 } }).show();//在按鍵響應(yīng)事件中顯示此對話框 } }); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:layout_width="200dp" android:layout_marginLeft="100dp" android:layout_height="wrap_content" android:text="點(diǎn)擊" android:id="@+id/button"/> </LinearLayout>
其實(shí)AlertDialog可以添加其他的功能選項(xiàng),比如在提示框里面綁定xml布局顯示,再比如定義多個(gè)選擇按鈕什么的,這些大家可以自行學(xué)習(xí)。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android 彈出提示框的使用(圖文實(shí)例)
- Android使用Toast顯示消息提示框
- Android仿QQ、微信聊天界面長按提示框效果
- Android編程之自定義AlertDialog(退出提示框)用法實(shí)例
- Android仿IOS自定義AlertDialog提示框
- Android仿百度谷歌搜索自動提示框AutoCompleteTextView簡單應(yīng)用示例
- Android超實(shí)用的Toast提示框優(yōu)化分享
- Android中仿IOS提示框的實(shí)現(xiàn)方法
- Android模擬美團(tuán)客戶端進(jìn)度提示框
- Android模仿Toast實(shí)現(xiàn)提示框效果
相關(guān)文章
Android編程實(shí)現(xiàn)切換imageView的方法分析
這篇文章主要介紹了Android編程實(shí)現(xiàn)切換imageView的方法,結(jié)合具體實(shí)例形式分析了切換imageView的相關(guān)設(shè)置技巧與注意事項(xiàng),需要的朋友可以參考下2017-09-09android byte[] 和short[]轉(zhuǎn)換的方法代碼
這篇文章主要介紹了android byte[] 和short[]轉(zhuǎn)換的方法代碼,有需要的朋友可以參考一下2014-01-01android中在Activity中響應(yīng)ListView內(nèi)部按鈕的點(diǎn)擊事件的兩種方法
本篇文章主要介紹了android中在Activity中響應(yīng)ListView內(nèi)部按鈕的點(diǎn)擊事件的兩種方法,有需要的可以了解一下。2016-11-11RecyclerView仿應(yīng)用列表實(shí)現(xiàn)網(wǎng)格布局
這篇文章主要為大家詳細(xì)介紹了RecyclerView仿應(yīng)用列表實(shí)現(xiàn)網(wǎng)格布局,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09Android編程使用內(nèi)容提供者方式(ContentProvider)進(jìn)行存儲的方法
這篇文章主要介紹了Android編程使用內(nèi)容提供者方式進(jìn)行存儲的方法,涉及Android內(nèi)容提供者的創(chuàng)建,配置及針對數(shù)據(jù)的增刪改查等操作技巧,需要的朋友可以參考下2016-01-01Android中正確使用字體圖標(biāo)(iconfont)的方法
IconFont字體不僅僅流行于Web開發(fā),在移動開發(fā)中也漸漸的使用的范圍更廣泛。這篇文章主要介紹了在Android開發(fā)中使用icon font的代碼和方法。對大家學(xué)習(xí)使用iconfont有一定的參考借鑒價(jià)值,有需要的朋友們下面來一起看看吧。2016-10-10Flutter?Ping檢查服務(wù)器通訊信號強(qiáng)度實(shí)現(xiàn)步驟
這篇文章主要為大家介紹了Flutter?Ping檢查服務(wù)器通訊信號強(qiáng)度實(shí)現(xiàn)步驟詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06Android BroadcastReceiver廣播簡單使用
這篇文章主要為大家詳細(xì)介紹了Android BroadcastReceiver廣播簡單的使用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04Android RadarView雷達(dá)圖(蜘蛛網(wǎng)圖)的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android RadarView雷達(dá)圖(蜘蛛網(wǎng)圖)的實(shí)現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03