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è)置對(duì)話(huà)框標(biāo)題 .setMessage('是否需要更換xxx?') .setPositiveButton('是', new DialogInterface.OnClickListener() {//添加確定按鈕 @Override public void onClick(DialogInterface dialog, int which) {//確定按鈕的響應(yīng)事件,點(diǎn)擊事件沒(méi)寫(xiě),自己添加 } }).setNegativeButton('否', new DialogInterface.OnClickListener() {//添加返回按鈕 @Override public void onClick(DialogInterface dialog, int which) {//響應(yīng)事件,點(diǎn)擊事件沒(méi)寫(xiě),自己添加 } }).show();//在按鍵響應(yīng)事件中顯示此對(duì)話(huà)框 } });
實(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è)置對(duì)話(huà)框標(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)事件中顯示此對(duì)話(huà)框 } }); }}
<?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: /></LinearLayout>
其實(shí)AlertDialog可以添加其他的功能選項(xiàng),比如在提示框里面綁定xml布局顯示,再比如定義多個(gè)選擇按鈕什么的,這些大家可以自行學(xué)習(xí)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 詳解Android studio 動(dòng)態(tài)fragment的用法2. 解決Android studio xml界面無(wú)法預(yù)覽問(wèn)題3. 圖文詳解vue中proto文件的函數(shù)調(diào)用4. Spring Boot和Thymeleaf整合結(jié)合JPA實(shí)現(xiàn)分頁(yè)效果(實(shí)例代碼)5. php模擬實(shí)現(xiàn)斗地主發(fā)牌6. 什么是python的自省7. Vue封裝一個(gè)TodoList的案例與瀏覽器本地緩存的應(yīng)用實(shí)現(xiàn)8. vue 使用localstorage實(shí)現(xiàn)面包屑的操作9. .Net Core使用Coravel實(shí)現(xiàn)任務(wù)調(diào)度的完整步驟10. Vuex localStorage的具體使用

網(wǎng)公網(wǎng)安備