这篇文章主要为大家详细介绍了android实现弹出提示框,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了anadroid实现弹出提示框的具体代码,供大家参考,具体内容如下
提示框是利用AlertDialog实现的。
代码:
(设置在button的点击事件中)
 new AlertDialog.Builder(MainActivity.this).setTitle("信息提示")//设置对话框标题
      .setMessage("是否需要更换xxx?")
      .setPositiveButton("是", new DialogInterface.OnClickListener() {//添加确定按钮
       @Override
       public void onClick(DialogInterface dialog, int which) {//确定按钮的响应事件,点击事件没写,自己添加
       }
      }).setNegativeButton("否", new DialogInterface.OnClickListener() {//添加返回按钮
     @Override
     public void onClick(DialogInterface dialog, int which) {//响应事件,点击事件没写,自己添加
     }
    }).show();//在按键响应事件中显示此对话框
   }
 });
实现效果:

完整代码:
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("信息提示")//设置对话框标题
      .setMessage("是否需要更换xxx?")
      .setPositiveButton("是", new DialogInterface.OnClickListener() {//添加确定按钮
       @Override
       public void onClick(DialogInterface dialog, int which) {//确定按钮的响应事件
       }
      }).setNegativeButton("否", new DialogInterface.OnClickListener() {//添加返回按钮
     @Override
     public void onClick(DialogInterface dialog, int which) {//响应事件
     }
    }).show();//在按键响应事件中显示此对话框
   }
  });
 }
}
<?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="点击"
  android:id="@+id/button"/>
</LinearLayout>
其实AlertDialog可以添加其他的功能选项,比如在提示框里面绑定xml布局显示,再比如定义多个选择按钮什么的,这些大家可以自行学习。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程学习网。
				 织梦狗教程
				
			本文标题为:android实现弹出提示框
				
        
 
            
        基础教程推荐
             猜你喜欢
        
	     - Android多返回栈技术 2023-04-15
 - Flutter手势密码的实现示例(附demo) 2023-04-11
 - Flutter绘图组件之CustomPaint使用详解 2023-05-12
 - Android开发使用RecyclerView添加点击事件实例详解 2023-06-15
 - android studio按钮监听的5种方法实例详解 2023-01-12
 - iOS开发教程之XLForm的基本使用方法 2023-05-01
 - Android中的webview监听每次URL变化实例 2023-01-23
 - IOS 播放系统提示音使用总结(AudioToolbox) 2023-03-01
 - IOS应用内跳转系统设置相关界面的方法 2022-11-20
 - 解决Android Studio突然不显示logcat日志的问题 2023-02-04
 
    	
    	
    	
    	
    	
    	
    	
    	
						
						
						
						
						
				
				
				
				