您好,登錄后才能下訂單哦!
這篇文章主要介紹“Android AlertDialog的幾種用法介紹”,在日常操作中,相信很多人在Android AlertDialog的幾種用法介紹問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Android 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="簡單的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("標題欄"); builder.setMessage("正文部分,簡單的文本"); builder.setPositiveButton("確定",new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "點擊了確定", Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("取消",null); builder.setNeutralButton("中立",null); AlertDialog alertDialog = builder.create(); alertDialog.show(); } private String [] item = {"游戲","運動","電影","旅游","看書"}; public void dialog_2(View v){ AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("請選擇"); 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("請選擇"); 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(); } // 設置boolean數(shù)組所有的選項設置默認沒選 boolean[] bools = {false,false,false,false,false}; public void dialog_4(View v){ AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("請選擇"); 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("請輸入密碼"); 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, "密碼錯誤", 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的幾種用法介紹”的學習就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。