android實現(xiàn)彈出提示框
本文實例為大家分享了anadroid實現(xiàn)彈出提示框的具體代碼,供大家參考,具體內(nèi)容如下
提示框是利用AlertDialog實現(xiàn)的。
代碼:
(設(shè)置在button的點擊事件中)
new AlertDialog.Builder(MainActivity.this).setTitle("信息提示")//設(shè)置對話框標(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)事件中顯示此對話框
}
});
實現(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è)置對話框標(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)事件中顯示此對話框
}
});
}
}
<?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布局顯示,再比如定義多個選擇按鈕什么的,這些大家可以自行學(xué)習(xí)。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android在連拍菜單中增加連拍張數(shù)選項功能實現(xiàn)代碼
想要增加連拍張數(shù)選項需要在entries, entryvalues中添加兩項,同時在mtk_strings.xml中添加相應(yīng)的字符串,具體如下,感興趣的朋友可以參考下哈2013-06-06
Android音頻系統(tǒng)AudioTrack使用方法詳解
這篇文章主要為大家詳細(xì)介紹了Android音頻系統(tǒng)AudioTrack的使用方法,如何使用AudioTrack進(jìn)行音頻播放,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07
Android應(yīng)用啟動另外一個apk應(yīng)用的方法
這篇文章主要介紹了Android應(yīng)用啟動另外一個apk應(yīng)用的方法,涉及Android基于intent的package調(diào)用與管理技巧,需要的朋友可以參考下2016-02-02
Android動態(tài)修改ToolBar的Menu菜單示例
本篇文章主要介紹了Android動態(tài)修改ToolBar的Menu菜單示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02
Android實現(xiàn)Activities之間進(jìn)行數(shù)據(jù)傳遞的方法
這篇文章主要介紹了Android實現(xiàn)Activities之間進(jìn)行數(shù)據(jù)傳遞的方法,涉及Android中Activities的使用技巧,需要的朋友可以參考下2015-04-04
Android?Jetpack庫重要組件WorkManager的使用
WorkManager是Android?Jetpack的一個強(qiáng)大的組件,用于處理后臺耗時任務(wù)。后臺任務(wù)可以是一次性的,也可以是重復(fù)的,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08

