华域联盟 Andriod android实现弹出提示框

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实现弹出提示框

转载请保留出处和原文链接:https://www.cnhackhy.com/109023.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

Android获取文字高度的三种方法

Android带清除按钮、密码可见小眼睛的输入框

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们