一是直接new一個Dialog目標,然後調用Dialog目標的show和dismiss辦法來操控對話框的顯現和躲藏。
二是在Activity的onCreateDialog(int id)辦法中創立Dialog目標並回來,然後調用Activty的showDialog(int id)和dismissDialog(int id)來顯現和躲藏對話框。
差異在於經過第二種辦法創立的對話框會承繼Activity的特點,比方取得Activity的menu事情等。下面案例主要介紹第一種。
1.最簡單的對話框
佈局文件:
就一個Button按鈕
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="79dp"
android:onClick="openDialog"
android:text="打開對話框" />
MainActivity.java
package com.example.lesson16_dialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
private Handler handler;
private int progress;
private static final int MAX_PROGRESS=100;
private static final int PRO=10;
private ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@SuppressWarnings("deprecation")
public void openDialog(View v) {
test1();
/*tes2();
tes3();
tes4();
tes5();
tes6();
test7();
myDialog();*/
}
public void test1() {
// 創建對話框對象
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
// 設置對話框的標題
alertDialog.setTitle("這是一個消息對話框");
// 設置對話框中的內容
alertDialog.setMessage("消息");
// 顯示對話框
alertDialog.show();
}
public void tes2() {
AlertDialog alertDialog = new AlertDialog.Builder(this)
.setTitle("哈哈").setMessage("什麼都沒有").show();
}
public void tes3() {
new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle("對話框")
.setMessage("是否創建文件")
.setPositiveButton("確認", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 創建文件了
new AlertDialog.Builder(MainActivity.this).setMessage(
"文件已經被創建").show();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
new AlertDialog.Builder(MainActivity.this)
.setMessage("您已經選擇了取消的按鈕,該文件不會被創建").create()
.show();
}
}).show();
}
public void tes4() {
final String items[] = { "Java", "PHP", "3G", ".NET" };
new AlertDialog.Builder(this).setTitle("簡單列表對話框")
.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 第一個參數 dialog int which 那個條目
Toast.makeText(getApplicationContext(),
"xxxxx" + items[which], Toast.LENGTH_LONG)
.show();
}
}).show();
}
public void tes5() {
final String items[] = { "Java", "PHP", "3G", ".NET" };
new AlertDialog.Builder(this).setTitle("單選列表對話框")
// .setSingleChoiceItems(items, checkedItem, listener)
// .setSingleChoiceItems(itemsId, checkedItem, listener)
// .setSingleChoiceItems(adapter, checkedItem, listener)
// .setSingleChoiceItems(cursor, checkedItem, labelColumn, listener)
// labelColumn如果數據源是數據集
// 數據集中的某一列會作爲列表對話框的數據加載的列表框中,該參數表示該列的名稱(字段名稱)
.setSingleChoiceItems(items, 1,new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// 第一個參數 dialog int which 那個條目
Toast.makeText(getApplicationContext(),
"xxxxx" + items[which],
Toast.LENGTH_LONG).show();
}
}).show();
}
public void tes6() {
final String items[] = { "Java", "PHP", "3G", ".NET" };
new AlertDialog.Builder(this)
.setTitle("多選列表對話框")
// .setMultiChoiceItems(itemsId, checkedItems, listener)
// .setMultiChoiceItems(cursor, isCheckedColumn, labelColumn,
// listener)
.setMultiChoiceItems(items,
new boolean[] { false, true, true, false },
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which, boolean isChecked) {
if (isChecked) {
Toast.makeText(getApplicationContext(),
"xxx" + items[which],
Toast.LENGTH_LONG).show();
}
}
})
.setPositiveButton("確認", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "確認",
Toast.LENGTH_LONG).show();
}
}).show();
}
//系統進度條
public void test7(){
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case PRO:
if (progress >= MAX_PROGRESS) {
// 重新設置
progress = 0;
progressDialog.dismiss();// 銷燬對話框
} else {
progress++;
progressDialog.incrementProgressBy(1);
// 延遲發送消息
handler.sendEmptyMessageDelayed(PRO, 100);
}
break;
default:
break;
}
}
};
progressDialog = new ProgressDialog(this);
progressDialog.setIcon(R.drawable.ic_launcher);
progressDialog.setTitle("正在加載數據......");
progressDialog.setMessage("請稍後...");
// 設置進度條對話框 旋轉STYLE_SPINNER,水平 STYLE_HORIZONTAL
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
// 樣式(水平,旋體)
// 進度最大值
progressDialog.setMax(MAX_PROGRESS);
progressDialog.setButton("暫停", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//刪除消息隊列
handler.removeMessages(PRO);
}
});
progressDialog.setButton2("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//刪除消息隊列
handler.removeMessages(PRO);
//恢復進度初始值
progress=0;
progressDialog.setProgress(progress);
}
});
// 顯示
progressDialog.show();
//必須設置到show之後 show之前 可能bug
progress = (progress>0)?progress:0;
progressDialog.setProgress(progress);
// 線程
handler.sendEmptyMessage(PRO);
}
//自定義的對話框
public void myDialog(){
LayoutInflater layoutInflater = getLayoutInflater();
View view = layoutInflater.inflate(R.layout.activity_main, null); //R.layout.activty_main自定義的佈局文件
new AlertDialog.Builder(this).setView(view).setTitle("自定義的對話框").setPositiveButton("確認按鈕", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//處理
}
}).show();
}
}