1.0 Android對(duì)話框哄辣,因?yàn)樾枨蟮牟煌瑫?huì)有很多種请梢。如下:
2.0 本篇的內(nèi)容主要有:
- 1.
確定取消對(duì)話框
- 2.
單選對(duì)話框
- 3.
多選對(duì)話框
- 4.
進(jìn)度對(duì)話框
- 5.
有具體進(jìn)度的對(duì)話框
3.0 新建一個(gè)項(xiàng)目Dialogs
,目錄如下:
4.0 先簡(jiǎn)單寫好布局文件activity_main.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:orientation="vertical">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="click01"
android:text="確定取消對(duì)話框"/>
<Button
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="click02"
android:text="單選對(duì)話框"/>
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="click03"
android:text="多選對(duì)話框"/>
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="click04"
android:text="進(jìn)度對(duì)話框"/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="click05"
android:text="有具體進(jìn)度的對(duì)話框"/>
</LinearLayout>
5.0 確定取消對(duì)話框
/**
* 彈出確定取消對(duì)話框
*
* @param view
*/
public void click01(View view) {
// 工廠設(shè)計(jì)模式,得到創(chuàng)建對(duì)話框的工廠
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("警告");
builder.setMessage("若練此功力穗,必先自宮毅弧,是否繼續(xù)?");
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 dialog, int which) {
Toast.makeText(MainActivity.this, "如不自宮,一定不成功", Toast.LENGTH_SHORT).show();
}
});
//千萬(wàn)別忘了這一步
builder.show();
//以前寫的格式是:
//builder.creste();
//builder.show();
//但我們查看show()源代碼
//會(huì)發(fā)現(xiàn)這個(gè)方法里面已經(jīng)調(diào)用了create()方法
}
demo完整的代碼放最后崖面,先解釋下原理元咙。
5.1 Android提供了AlertDialog.Builder
類,注意導(dǎo)的包有兩個(gè)
-
import android.app.AlertDialog
這里我們導(dǎo)入默認(rèn)的就行了巫员。在 **`Android 5.0以下就是原始風(fēng)格庶香, 5.0 以上為 Material 風(fēng)格。 -
android.support.v7.app.AlertDialog
這個(gè)V7包中的AlertDialog
在Android2.1以上
可以提供兼容性的Material 風(fēng)格 Dialog
简识。也就是說赶掖,使用這個(gè)包中的AlertDialog
的話感猛,從2.1到7.0都是Material風(fēng)格
的Dialog
。
這兩AlertDialog
具體什么差別奢赂,可以參考這篇文章:
Material風(fēng)格的Dialog(android.support.v7.app.AlertDialog)
具體Material風(fēng)格
是什么樣子陪白,可以參考這篇文章:
Android Material Design風(fēng)格基本使用(實(shí)現(xiàn)簡(jiǎn)易新聞APP)
- 這里
Android
提供了AlertDialog.Builder
的寫法,大家可能會(huì)覺得怪異呈驶。
我們可以查看源代碼拷泽,發(fā)現(xiàn)Builder
是AlertDialog
類的內(nèi)部類,所以這里需要點(diǎn)一下袖瞻。
運(yùn)行效果:
6.0 單選對(duì)話框
/**
* 單選對(duì)話框
*
* @param view
*/
public void click02(View view) {
// 工廠設(shè)計(jì)模式司致,得到創(chuàng)建對(duì)話框的工廠
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("請(qǐng)選擇您的性別:");
final String[] items = {"男", "女", "人妖"};
//第一個(gè)參數(shù)傳入具體選項(xiàng);
//第二個(gè)參數(shù)聋迎,默認(rèn)勾選脂矫;沒有勾選,傳入-1霉晕;
//第三個(gè)參數(shù)傳入 點(diǎn)擊監(jiān)聽
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "您的性別為:" + items[which], Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
builder.show();
}
我們?cè)诰帉懛椒ǖ臅r(shí)候庭再,可以發(fā)現(xiàn):
- 1.從資源文件中裝載數(shù)據(jù):
public AlertDialog.Builder setSingleChoiceItems(int itemsId, int checkedItem, final OnClickListener listener)
- 2.從數(shù)據(jù)集中裝載數(shù)據(jù)
public AlertDialog.Builder setSingleChoiceItems(Cursor cursor, int checkedItem, String labelColumn, final OnClickListener listener)
- 3.從字符串?dāng)?shù)組中裝載數(shù)據(jù)
public AlertDialog.Builder setSingleChoiceItems(CharSequence[] items, int checkedItem, final OnClickListener listener)
- 4.從ListAdapter對(duì)象中裝載數(shù)據(jù)
public AlertDialog.Builder setSingleChoiceItems(ListAdapter adpater, int checkedItem, final OnClickListener listener)
這四種方法有時(shí)間再一一舉例,目前使用的時(shí)就用上面例子中的這種即可牺堰。
運(yùn)行效果:
6.0 多選對(duì)話框
/**
* 多選對(duì)話框
*
* @param view
*/
public void click03(View view) {
// 工廠設(shè)計(jì)模式拄轻,得到創(chuàng)建對(duì)話框的工廠
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("請(qǐng)選擇您愛吃的水果:");
final String[] items = {"蘋果", "梨子", "香蕉", "菠蘿", "哈密瓜"};
final boolean[] checkeds = {true, false, true, false, true};
//第一個(gè)參數(shù)傳入具體選項(xiàng);
//第二個(gè)參數(shù)伟葫,具體選項(xiàng)是否被選中的數(shù)組
//第三個(gè)參數(shù)傳入 點(diǎn)擊監(jiān)聽
builder.setMultiChoiceItems(items, checkeds, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
Toast.makeText(MainActivity.this, items[which] + isChecked, Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("提交", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//這里簡(jiǎn)單處理下選中的內(nèi)容恨搓,便于后面輸出查看
//遍歷checkeds,判斷真假
StringBuffer sb = new StringBuffer();
for (int i = 0; i < checkeds.length; i++) {
if (checkeds[i]) {
sb.append(items[i] + " ");
}
}
Toast.makeText(MainActivity.this, "您喜歡吃的水果是:" + sb.toString(), Toast.LENGTH_SHORT).show();
}
});
builder.show();
}
和單選對(duì)話框類似筏养。
同樣斧抱,setMultiChoiceItems()
方法有3種:
-
public AlertDialog.Builder setMultiChoiceItems (CharSequence[] items, boolean[] checkedItems, DialogInterface.OnMultiChoiceClickListener listener)
代碼中有解釋。
-
public AlertDialog.Builder setMultiChoiceItems (Cursor cursor, String isCheckedColumn, String labelColumn, DialogInterface.OnMultiChoiceClickListener listener)
第一個(gè)參數(shù)變成了需要一個(gè)Cursor對(duì)象
-
public AlertDialog.Builder setMultiChoiceItems (int itemsId, boolean[] checkedItems, DialogInterface.OnMultiChoiceClickListener listener)
從資源文件中裝載數(shù)據(jù)渐溶。
上面變藍(lán)色的字體可以點(diǎn)擊直接進(jìn)入Android SDK API文檔辉浦。
運(yùn)行效果:
7.0 進(jìn)度對(duì)話框
進(jìn)度對(duì)話框所用的ProgressDialog 是AlertDialog的子類。
點(diǎn)擊可以查看API文檔:公共類 ProgressDialog
/**
* 進(jìn)度對(duì)話框
*
* @param view
*/
public void click04(View view) {
final ProgressDialog pd = new ProgressDialog(this);
pd.setTitle("請(qǐng)稍后:");
pd.setMessage("正在拼命加載中...");
pd.show();
new Thread() {
@Override
public void run() {
try {
Thread.sleep(30000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//將進(jìn)度條關(guān)閉
pd.dismiss();
}
}.start();
}
運(yùn)行效果:
8.0 帶具體進(jìn)度的對(duì)話框
/**
* 帶具體進(jìn)度的對(duì)話框
*
* @param view
*/
public void click05(View view) {
final ProgressDialog pd = new ProgressDialog(this);
pd.setTitle("請(qǐng)稍后:");
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setMessage("正在拼命加載中...");
// 設(shè)置進(jìn)度條的最大值
pd.setMax(100);
pd.show();
new Thread() {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
try {
Thread.sleep(300);
pd.setProgress(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//將進(jìn)度條關(guān)閉
pd.dismiss();
}
}.start();
}
運(yùn)行效果:
在java語(yǔ)言中實(shí)現(xiàn)復(fù)選框被選中可以這樣:
實(shí)例.setChecked(true);
我這里只是簡(jiǎn)單的實(shí)現(xiàn)茎辐,強(qiáng)調(diào)原理宪郊,下面這篇文章可以工程化的處理對(duì)話框:
Android Dialog使用詳解
里面還實(shí)現(xiàn)了自定義對(duì)話框用于實(shí)現(xiàn)如下效果:
9.0 附上MainActivity.java源碼
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* 彈出確定取消對(duì)話框
*
* @param view
*/
public void click01(View view) {
// 工廠設(shè)計(jì)模式,得到創(chuàng)建對(duì)話框的工廠
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("警告");
builder.setMessage("若練此功拖陆,必先自宮弛槐,是否繼續(xù)?");
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 dialog, int which) {
Toast.makeText(MainActivity.this, "如果不自宮丐黄,一定不成功", Toast.LENGTH_SHORT).show();
}
});
builder.show();
}
/**
* 單選對(duì)話框
*
* @param view
*/
public void click02(View view) {
// 工廠設(shè)計(jì)模式斋配,得到創(chuàng)建對(duì)話框的工廠
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("請(qǐng)選擇您的性別:");
final String[] items = {"男", "女", "人妖"};
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "您的性別為:" + items[which], Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
builder.show();
}
/**
* 多選對(duì)話框
*
* @param view
*/
public void click03(View view) {
// 工廠設(shè)計(jì)模式孔飒,得到創(chuàng)建對(duì)話框的工廠
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("請(qǐng)選擇您愛吃的水果:");
final String[] items = {"蘋果", "梨子", "香蕉", "菠蘿", "哈密瓜"};
final boolean[] checkeds = {true, false, true, false, true};
builder.setMultiChoiceItems(items, checkeds, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
Toast.makeText(MainActivity.this, items[which] + isChecked, Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("提交", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < checkeds.length; i++) {
if (checkeds[i]) {
sb.append(items[i] + " ");
}
}
Toast.makeText(MainActivity.this, "您喜歡吃的水果是:" + sb.toString(), Toast.LENGTH_SHORT).show();
}
});
builder.show();
}
/**
* 進(jìn)度對(duì)話框
*
* @param view
*/
public void click04(View view) {
final ProgressDialog pd = new ProgressDialog(this);
pd.setTitle("請(qǐng)稍后:");
pd.setMessage("正在拼命加載中...");
pd.show();
new Thread() {
@Override
public void run() {
try {
Thread.sleep(30000);
} catch (InterruptedException e) {
e.printStackTrace();
}
pd.dismiss();
}
}.start();
}
/**
* 帶具體進(jìn)度的對(duì)話框
*
* @param view
*/
public void click05(View view) {
final ProgressDialog pd = new ProgressDialog(this);
pd.setTitle("請(qǐng)稍后:");
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setMessage("正在拼命加載中...");
// 設(shè)置進(jìn)度條的最大值
pd.setMax(100);
pd.show();
new Thread() {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
try {
Thread.sleep(300);
pd.setProgress(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
pd.dismiss();
}
}.start();
}
}
END