介绍
本篇文章给大家分享的是有关怎么在android中实现一个弹出提示框,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
提示框是利用AlertDialog实现的。
, new AlertDialog.Builder (MainActivity.this) .setTitle(“信息提示“)//设置对话框标题 ,,,,,.setMessage(“是否需要更换xxx ?“) ,,,,,.setPositiveButton(“是“,,new DialogInterface.OnClickListener(),{//添加确定按钮 ,,,,,@Override ,,,,,,public void onClick (DialogInterface 对话框,int 它),{//确定按钮的响应事件,点击事件没写,自己添加 ,,,,,,} ,,,,,}).setNegativeButton(“否“,,new DialogInterface.OnClickListener(),{//添加返回按钮 ,,,@Override ,,,,public void onClick (DialogInterface 对话框,int 它),{//响应事件,点击事件没写,自己添加 ,,,,} ,,,}),告诉();//在按键响应事件中显示此对话框 ,,} 以前,});>实现效果:
完整代码:
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 视图),{ ,,,new AlertDialog.Builder (MainActivity.this) .setTitle(“信息提示“)//设置对话框标题 ,,,,,.setMessage(“是否需要更换xxx ?“) ,,,,,.setPositiveButton(“是“,,new DialogInterface.OnClickListener(),{//添加确定按钮 ,,,,,@Override ,,,,,,public void onClick (DialogInterface 对话框,int 它),{//确定按钮的响应事件 ,,,,,,} ,,,,,}).setNegativeButton(“否“,,new DialogInterface.OnClickListener(),{//添加返回按钮 ,,,@Override ,,,,public void onClick (DialogInterface 对话框,int 它),{//响应事件 ,,,,} ,,,}),告诉();//在按键响应事件中显示此对话框 ,,} ,,}); ,} }& lt; ? xml version=?.0“,编码=皍tf-8" ?比; & lt; LinearLayout  xmlns: android=癶ttp://schemas.android.com/apk/res/android" ,xmlns:应用=癶ttp://schemas.android.com/apk/res-auto" ,xmlns:工具=癶ttp://schemas.android.com/tools" ,android: layout_width=癿atch_parent" ,android: layout_height=癿atch_parent" ,工具:上下文=?MainActivity"祝辞 & lt;按钮 android:才能layout_width=?00 dp" android:才能layout_marginLeft=?00 dp" android:才能layout_height=皐rap_content" android:文本=安拍艿慊鳌? android:才能id=癅 + id/button"/比; & lt;/LinearLayout>以上就是怎么在android中实现一个弹出提示框,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注行业资讯频道。
怎么在android中实现一个弹出提示框