android实现弹出提示框
更新时间:2021年01月20日 10:16:55 作者:Tsdx.cla
这篇文章主要为大家详细介绍了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实现Activities之间进行数据传递的方法
这篇文章主要介绍了Android实现Activities之间进行数据传递的方法,涉及Android中Activities的使用技巧,需要的朋友可以参考下2015-04-04
Android Jetpack库重要组件WorkManager的使用
WorkManager是Android Jetpack的一个强大的组件,用于处理后台耗时任务。后台任务可以是一次性的,也可以是重复的,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2022-08-08


最新评论