本篇文章給大家分享的是有關(guān)怎么在android中實(shí)現(xiàn)一個(gè)彈出提示框,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。

提示框是利用AlertDialog實(shí)現(xiàn)的。
new AlertDialog.Builder(MainActivity.this).setTitle("信息提示")//設(shè)置對(duì)話框標(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ì)話框
}
});實(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ì)話框標(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ì)話框
}
});
}
}<?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:id="@+id/button"/> </LinearLayout>
以上就是怎么在android中實(shí)現(xiàn)一個(gè)彈出提示框,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)站題目:怎么在android中實(shí)現(xiàn)一個(gè)彈出提示框-創(chuàng)新互聯(lián)
標(biāo)題鏈接:http://www.chinadenli.net/article44/dcdihe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、云服務(wù)器、網(wǎng)站內(nèi)鏈、服務(wù)器托管、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容