国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

android實現彈出提示框

瀏覽:135日期:2022-09-21 09:32:23

本文實例為大家分享了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();//在按鍵響應事件中顯示此對話框 } });

實現效果:

android實現彈出提示框

完整代碼:

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: /></LinearLayout>

其實AlertDialog可以添加其他的功能選項,比如在提示框里面綁定xml布局顯示,再比如定義多個選擇按鈕什么的,這些大家可以自行學習。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Android
相關文章:
主站蜘蛛池模板: 永吉县| 城固县| 安丘市| 饶平县| 天祝| 德令哈市| 敦煌市| 响水县| 农安县| 梧州市| 年辖:市辖区| 台州市| 交口县| 无锡市| 河南省| 桓仁| 噶尔县| 赣榆县| 綦江县| 潜山县| 鄂托克旗| 闸北区| 西丰县| 鄱阳县| 轮台县| 蒲城县| 陕西省| 牙克石市| 昌宁县| 略阳县| 苏尼特左旗| 闸北区| 阿克苏市| 舒城县| 棋牌| 宁国市| 平武县| 磐石市| 哈尔滨市| 宁河县| 高清|