最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android AlertDialog的幾種用法詳解

 更新時(shí)間:2021年08月17日 14:16:58   作者:中國人醒來了  
這篇文章主要介紹了Android AlertDialog的幾種用法詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下

AlertDialog的幾種用法

xml代碼:

<?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"
    android:orientation="vertical"
    tools:context="com.example.lesson7_3_id19_alertdialog.MainActivity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="簡(jiǎn)單的dialog"
        android:onClick="dialog_1"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="列表的dialog"
        android:onClick="dialog_2"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="單選的dialog"
        android:onClick="dialog_3"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="多選的dialog"
        android:onClick="dialog_4"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="自定義View的dialog"
        android:onClick="dialog_5"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="使用adapter的dialog"
        android:onClick="dialog_6"/>
</LinearLayout>

java代碼:

package com.example.lesson7_3_id19_alertdialog;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void dialog_1(View v){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(R.mipmap.ic_launcher_round);

        builder.setTitle("標(biāo)題欄");
        builder.setMessage("正文部分,簡(jiǎn)單的文本");
        builder.setPositiveButton("確定",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "點(diǎn)擊了確定", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("取消",null);
        builder.setNeutralButton("中立",null);
        AlertDialog alertDialog = builder.create();
        alertDialog.show();

    }
    private String [] item = {"游戲","運(yùn)動(dòng)","電影","旅游","看書"};
    public void dialog_2(View v){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("請(qǐng)選擇");
        builder.setItems(item, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "選擇了"+item[which], Toast.LENGTH_SHORT).show();
            }
        });
        // 取消可以不添加
        //builder.setNegativeButton("取消",null);
        AlertDialog alertDialog = builder.create();
        alertDialog.show();

    }
    int index;
    public void dialog_3(View v){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("請(qǐng)選擇");
        builder.setSingleChoiceItems(item, index, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                index = which;
            }
        });
        builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "選擇了"+item[index], Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("取消",null);
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }
    // 設(shè)置boolean數(shù)組所有的選項(xiàng)設(shè)置默認(rèn)沒選
    boolean[] bools = {false,false,false,false,false};
    public void dialog_4(View v){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("請(qǐng)選擇");
        builder.setMultiChoiceItems(item, bools, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                bools[which] = isChecked;
            }
        });
        builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                StringBuffer sb = new StringBuffer();
                for (int i = 0; i < item.length; i++) {
                    if (bools[i]) {
                       sb.append(item[i] + " ");
                    }
                }
                Toast.makeText(MainActivity.this, "選擇了" + sb.toString(), Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("取消",null);
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }
    public void dialog_5(View v){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("連接wifi");
        final EditText et = new EditText(this);
        et.setHint("請(qǐng)輸入密碼");
        et.setSingleLine(true);
        builder.setView(et);
        builder.setNegativeButton("取消",null);
        builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String password = et.getText().toString();
                if (password.equals("123456")) {
                    Toast.makeText(MainActivity.this, "連接成功", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(MainActivity.this, "密碼錯(cuò)誤", Toast.LENGTH_SHORT).show();
                }
            }
        });
        AlertDialog alertDialog = builder.create();
        alertDialog.show();

    }
    public void dialog_6(View v){
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,item);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("使用適配器");
        builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "選擇了"+item[which], Toast.LENGTH_SHORT).show();
            }
        });
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }
}

 

到此這篇關(guān)于Android AlertDialog的幾種用法詳解的文章就介紹到這了,更多相關(guān)Android AlertDialog方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • android自定義倒計(jì)時(shí)控件示例

    android自定義倒計(jì)時(shí)控件示例

    這篇文章主要介紹了Android秒殺倒計(jì)時(shí)自定義TextView示例,大家參考使用吧
    2014-01-01
  • Android Listview中顯示不同的視圖布局詳解及實(shí)例代碼

    Android Listview中顯示不同的視圖布局詳解及實(shí)例代碼

    這篇文章主要介紹了Android Listview中顯示不同的視圖布局詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • Android入門之Fragment的使用教程

    Android入門之Fragment的使用教程

    Fragment是Android3.0后引入的一個(gè)新的API,他出現(xiàn)的初衷是為了適應(yīng)大屏幕的平板電腦。本文主要來和大家講講Fragment的使用,感興趣的小伙伴可以了解一下
    2022-12-12
  • Android?Drawable目錄下的XML圖形文件詳解

    Android?Drawable目錄下的XML圖形文件詳解

    在?Android?開發(fā)中,res/drawable?目錄下的?XML?文件是一種強(qiáng)大的圖形資源定義方式,它們比位圖資源更靈活、更易于維護(hù),下面我將詳細(xì)解析各種類型的?Drawable?XML?文件及其使用方法,需要的朋友可以參考下
    2025-04-04
  • Android開發(fā)中應(yīng)用程序分享功能實(shí)例

    Android開發(fā)中應(yīng)用程序分享功能實(shí)例

    這篇文章主要介紹了Android開發(fā)中應(yīng)用程序分享功能,結(jié)合實(shí)例形式分析了基于Intent實(shí)現(xiàn)Android程序分享功能的技巧,需要的朋友可以參考下
    2016-02-02
  • Android Studio3安裝圖文教程

    Android Studio3安裝圖文教程

    這篇文章主要為大家詳細(xì)介紹了Android Studio3安裝圖文教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • Android?Studio實(shí)現(xiàn)購買售賣系統(tǒng)

    Android?Studio實(shí)現(xiàn)購買售賣系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了Android?Studio實(shí)現(xiàn)購買售賣系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • Android如何自定義升級(jí)對(duì)話框示例詳解

    Android如何自定義升級(jí)對(duì)話框示例詳解

    對(duì)話框是我們?cè)谄綍r(shí)經(jīng)常會(huì)遇到的一個(gè)功能,但自帶的對(duì)話框不夠美觀,大家一般都會(huì)自定義,下面這篇文章主要給大家介紹了關(guān)于Android如何自定義升級(jí)對(duì)話框的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-08-08
  • Android解析json數(shù)據(jù)示例代碼(三種方式)

    Android解析json數(shù)據(jù)示例代碼(三種方式)

    本篇文章主要介紹了Android解析json數(shù)據(jù)示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-03-03
  • Android 中ImageView的ScaleType使用方法

    Android 中ImageView的ScaleType使用方法

    這篇文章主要介紹了Android 中ImageView的ScaleType使用方法的相關(guān)資料,希望通過本能幫助到大家,需要的朋友可以參考下
    2017-09-09

最新評(píng)論

昭通市| 旌德县| 新巴尔虎左旗| 特克斯县| 弥勒县| 靖西县| 元阳县| 平果县| 平原县| 罗山县| 福清市| 冷水江市| 肇州县| 布尔津县| 宾川县| 灌阳县| 江门市| 桦甸市| 原平市| 银川市| 宁国市| 禹州市| 中宁县| 漯河市| 武功县| 洛宁县| 长阳| 肇源县| 广昌县| 富民县| 鹤庆县| 阳东县| 颍上县| 房产| 梅州市| 辉县市| 巴东县| 光泽县| 洛阳市| 崇仁县| 旅游|