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

Android中AlertDialog四種對話框的最科學(xué)編寫用法(實(shí)例代碼)

 更新時(shí)間:2019年11月25日 10:09:03   作者:Geeksongs  
這篇文章主要介紹了Android中AlertDialog四種對話框的最科學(xué)編寫用法,本文通過代碼講解的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

首先我們上圖:

 xml的代碼如下,用于編寫按鈕:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
xmlns:widget="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
  <Button
    android:id="@+id/button_1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="簡單的dialog"
    />
  <Button
    android:id="@+id/button_2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="列表的dialog"
    />
  <Button
    android:id="@+id/button_3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="單選的dialog"
    />
  <Button
    android:id="@+id/button_4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="多選的dialog"
    />
</LinearLayout>

Java代碼如下,用于實(shí)現(xiàn)邏輯:

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity{
  int index;
  String [] item = {"Android","IOS","Spark","Hadoop","Web"};
  boolean[] bools = {false,false,false,false,false};
  // 設(shè)置boolean數(shù)組所有的選項(xiàng)設(shè)置默認(rèn)沒選
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
      actionBar.hide();
    }
    Button button=(Button)findViewById(R.id.button_1);
    button.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setIcon(R.drawable.girl);
        builder.setTitle("標(biāo)題欄");
        builder.setMessage("對話框內(nèi)容,可自行設(shè)置");
        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("取消", new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialogInterface, int i) {
            Toast.makeText(MainActivity.this, "點(diǎn)擊了取消", Toast.LENGTH_SHORT).show();
          }
        });
        builder.setNeutralButton("好的", new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialogInterface, int i) {
            Toast.makeText(MainActivity.this, "點(diǎn)擊了“好的”", Toast.LENGTH_SHORT).show();
          }
        });
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
      }
    });
    Button button2=(Button)findViewById(R.id.button_2);
    button2.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("請選擇一個(gè)技術(shù)分支");
        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();
      }
    });
    Button button3=(Button)findViewById(R.id.button_3);
    button3.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("請選擇技術(shù)分支:");
        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();
      }
    });
    Button button4=(Button)findViewById(R.id.button_4);
    button4.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("請選擇技術(shù)分支:");
        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();
      }
    });
  }
}

總結(jié)

以上所述是小編給大家介紹的Android中AlertDialog四種對話框的最科學(xué)編寫用法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!

相關(guān)文章

  • Android鬧鐘啟動時(shí)間設(shè)置無效問題的解決方法

    Android鬧鐘啟動時(shí)間設(shè)置無效問題的解決方法

    這篇文章主要為大家詳細(xì)介紹了Android鬧鐘啟動時(shí)間設(shè)置無效問題的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • 分析Android 11.0Settings源碼之主界面加載

    分析Android 11.0Settings源碼之主界面加載

    這篇文章主要介紹了分析Android 11.0Settings源碼之主界面加載,對Android源碼感興趣的同學(xué),可以著重看一下
    2021-04-04
  • Android RecyclerView點(diǎn)擊事件

    Android RecyclerView點(diǎn)擊事件

    這篇文章主要為大家詳細(xì)介紹了Android RecyclerView點(diǎn)擊事件的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-02-02
  • 最簡單的SD卡文件遍歷Android程序

    最簡單的SD卡文件遍歷Android程序

    這篇文章主要為大家詳細(xì)介紹了最簡單的SD卡文件遍歷Android程序,感興趣的小伙伴們可以參考一下
    2016-07-07
  • Android 不解壓直接讀取zip包的方法

    Android 不解壓直接讀取zip包的方法

    下面小編就為大家分享一篇Android 不解壓直接讀取zip包的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • 安卓(Android) 監(jiān)聽按鍵長按事件

    安卓(Android) 監(jiān)聽按鍵長按事件

    本文主要介紹Android 中監(jiān)聽按鍵的長按事件,對按鍵機(jī)制進(jìn)行詳解,并附有代碼實(shí)例,具有參考價(jià)值,希望能幫到有需要的小伙伴
    2016-07-07
  • Android中ExpandableListView使用示例詳解

    Android中ExpandableListView使用示例詳解

    這篇文章主要為大家詳細(xì)介紹了Android中ExpandableListView使用示例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • Android RenderScript實(shí)現(xiàn)高斯模糊

    Android RenderScript實(shí)現(xiàn)高斯模糊

    這篇文章主要為大家詳細(xì)介紹了Android RenderScript實(shí)現(xiàn)高斯模糊的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • Android編程之分辨率處理相關(guān)代碼段合集

    Android編程之分辨率處理相關(guān)代碼段合集

    這篇文章主要介紹了Android編程之分辨率處理相關(guān)代碼段合集,涉及Android針對分辨率的計(jì)算與轉(zhuǎn)換等相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-11-11
  • flutter簡單使用案例

    flutter簡單使用案例

    這篇文章主要介紹了使用Flutter短視頻上滑翻頁效果,本篇介紹了?Flutter的翻頁組件PageView的使用,通過?PageView可以輕松實(shí)現(xiàn)類似短視頻的縱向上滑翻頁的效果,也可以實(shí)現(xiàn)橫向翻頁效果(如閱讀類軟件),需要的朋友可以參考下
    2023-05-05

最新評論

文安县| 新昌县| 电白县| 耒阳市| 安岳县| 自贡市| 称多县| 绥滨县| 大庆市| 靖江市| 神农架林区| 黑龙江省| 柘荣县| 三门峡市| 南通市| 嘉兴市| 彰化市| 辽阳市| 元江| 皮山县| 康马县| 崇仁县| 盱眙县| 天气| 阳信县| 渭南市| 祁门县| 晋江市| 吴江市| 普安县| 静乐县| 清苑县| 赞皇县| 嘉黎县| 丹凤县| 九龙城区| 伊宁县| 绥中县| 陆河县| 邢台市| 广安市|