Android中AlertDialog四種對話框的最科學(xué)編寫用法(實例代碼)
首先我們上圖:
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代碼如下,用于實現(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ù)組所有的選項設(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, "點擊了確定", Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Toast.makeText(MainActivity.this, "點擊了取消", Toast.LENGTH_SHORT).show(); } }); builder.setNeutralButton("好的", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Toast.makeText(MainActivity.this, "點擊了“好的”", 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("請選擇一個技術(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é)編寫用法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
Android形狀圖形與狀態(tài)列表圖形及九宮格圖片超詳細(xì)講解
這篇文章主要介紹了Android形狀圖形與狀態(tài)列表圖形及九宮格圖片的應(yīng)用方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-09-09LayoutAnimation給ListView中的item設(shè)置動態(tài)出場效果(實例)
下面小編就為大家?guī)硪黄狶ayoutAnimation給ListView中的item設(shè)置動態(tài)出場效果(實例)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10Android實現(xiàn)界面內(nèi)嵌多種卡片視圖(ViewPager、RadioGroup)
這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)界面內(nèi)嵌多種卡片視圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-09-09Android :okhttp+Springmvc文件解析器實現(xiàn)android向服務(wù)器上傳照片
這篇文章主要介紹了Android :okhttp+Springmvc文件解析器實現(xiàn)android向服務(wù)器上傳照片,需要的朋友可以參考下2020-05-05Android?Flutter實現(xiàn)搜索的三種方式詳解
這篇文章主要為大家詳細(xì)介紹了Android?Flutter實現(xiàn)搜索的三種方式,文中的示例代碼講解詳細(xì),具有一定的借鑒價值,感興趣的可以了解一下2022-08-08Kotlin利用Regex如何構(gòu)建正則表達(dá)式詳解
正則表達(dá)式,又稱規(guī)則表達(dá)式。下面這篇文章主要給大家介紹了關(guān)于Kotlin利用Regex構(gòu)建正則表達(dá)式的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2017-12-12使用RecylerView完成拖動排序高仿qq側(cè)滑刪除功能
最近在做一個android項目,使用到Recylerview完成拖動排序,側(cè)滑刪除功能,今天小編把思路分享到腳本之家平臺,供大家學(xué)習(xí)2016-10-10android使用service和activity獲取屏幕尺寸的方法
這篇文章主要介紹了android使用service和activity獲取屏幕尺寸的方法,實例分析了基于service和activity兩種方法獲取屏幕尺寸的相關(guān)技巧,非常簡單實用,需要的朋友可以參考下2015-08-08