Android AlertDialog的幾種用法詳解
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)沒(méi)選
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 ViewPager中顯示圖片與播放視頻的填坑記錄
這篇文章主要給介紹了關(guān)于Android ViewPager中顯示圖片與播放視頻的一些填坑記錄,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-05-05
Android應(yīng)用中實(shí)現(xiàn)跳轉(zhuǎn)外部瀏覽器打開鏈接功能
在開發(fā)Android應(yīng)用程序時(shí),有時(shí)候我們需要讓用戶跳轉(zhuǎn)到外部瀏覽器打開特定的鏈接,例如打開一個(gè)網(wǎng)頁(yè)、下載文件等,本文將介紹如何在Android應(yīng)用中實(shí)現(xiàn)跳轉(zhuǎn)外部瀏覽器打開鏈接的功能,感興趣的朋友一起看看吧2024-06-06
如何通過(guò)Battery Historian分析Android APP耗電情況
Android 從兩個(gè)層面統(tǒng)計(jì)電量的消耗,分別為軟件排行榜及硬件排行榜。它們各有自己的耗電榜單,軟件排行榜為機(jī)器中每個(gè) App 的耗電榜單,硬件排行榜則為各個(gè)硬件的耗電榜單。這兩個(gè)排行榜的統(tǒng)計(jì)是互為獨(dú)立,互不干擾的2021-06-06
internal修飾符探索kotlin可見(jiàn)性控制詳解
這篇文章主要為大家介紹了internal修飾符探索kotlin可見(jiàn)性控制詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Android短信驗(yàn)證碼倒計(jì)時(shí)驗(yàn)證的2種常用方式
各位開發(fā)者們?cè)陂_發(fā)中經(jīng)常會(huì)遇到獲取短信驗(yàn)證碼,獲取驗(yàn)證碼后需要等待1分鐘倒計(jì)時(shí),這段時(shí)間是不能再次發(fā)送短信請(qǐng)求的。這篇文章總結(jié)了兩種常用的Android​短信驗(yàn)證碼倒計(jì)時(shí)驗(yàn)證方式,有需要的朋友們可以參考借鑒,下面來(lái)一起看看吧。2016-12-12
Android編程開發(fā)之在Canvas中利用Path繪制基本圖形(圓形,矩形,橢圓,三角形等)
這篇文章主要介紹了Android編程開發(fā)之在Canvas中利用Path繪制基本圖形的方法,涉及Android基本的圖形繪制技巧,結(jié)合實(shí)例分析了繪制圓形,矩形,橢圓,三角形等基本圖形的實(shí)現(xiàn)方法,需要的朋友可以參考下2016-01-01
Android中使用am命令實(shí)現(xiàn)在命令行啟動(dòng)程序詳解
這篇文章主要介紹了Android中使用am命令實(shí)現(xiàn)在命令行啟動(dòng)程序詳解,本文詳細(xì)講解了am命令的語(yǔ)法,然后給出了啟動(dòng)內(nèi)置程序的操作實(shí)例,需要的朋友可以參考下2015-04-04

