原本定於20號更新下微博,怎奈公司人少活多,小編以一名android工程師的身份,“勇敢”的投入到了SSH的開發當中,已深深不能自拔,沒辦法只好把Dialog的相關方法一分爲二,分期發佈,這一講裏會介紹Dialog的類型和基本使用方法,下一講裏將會介紹幾種常見的Dialog效果.
Dialog(對話框)有四個子類,分別是:
1、AlertDialog,具有0-3個按鈕,可以放選項、複選框單選框等,以建議的方式域用戶交互可以。
2、ProgressDialog,顯示一個進度的圓環或者進度條。
3、DataPickerDialog,選擇日期的dialog。
4、TimePickerDialog,選擇時間的dialog
我們最常用的就是AlertDialog和ProgressDialog,AlertDialog的構造方法都是Protected類型的,所以不能簡單的new一個出來,只有通過創建工具即Bulider來創建。
AlertDialog的常用方法有以下幾種:
setTitle(CharSequence title),setTitle(int titleId),設置標題字符串
setSingleChoiceItems,設置爲單選項對話框
setMultiChoiceItems設置爲多選選項對話款
setItems,設置爲選項對話框,不區分多選單選
setPositiveButton(CharSequence text, DialogInterface.OnClickListener listener),確定按鍵
setNegativeButton(CharSequence text, DialogInterface.OnClickListener listener),取消按鍵
setNeutralButton(CharSequence text, DialogInterface.OnClickListener listener),中間按鍵
setCustomTitle(View customTitleView),設置自定義的Title視圖
setView(View view),設置對話框內容爲自定義的視圖
下面我們來看一下今天的內容。
package com.teach.dialog;
/**
* @author Arthur Lee
* @time 04/19/2014
* */
import java.util.Calendar;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.DatePickerDialog;
import android.app.Notification.Style;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast;
/**
* */
public class DialogHelper {
private Context context;
//構造函數,用以綁定當前的Context.
public DialogHelper(Context context){
this.context = context;
}
//普通的Dialog提示框,由標題欄,信息欄和按鈕區三部分組成。
public void setNormal(){
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
dialog.setTitle("這是標題欄").setMessage("這是信息欄").setPositiveButton("確定", new OnClickListener(){
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
Toast.makeText(context, "你點擊了確定按鈕", Toast.LENGTH_LONG).show();
}}).setNegativeButton("取消", new OnClickListener(){
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
Toast.makeText(context, "你點擊了取消按鈕", Toast.LENGTH_LONG).show();
}}).setNeutralButton("中間選項", new OnClickListener(){
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
Toast.makeText(context, "你點擊了中間選項", Toast.LENGTH_LONG).show();
}});
dialog.create().show();
}
//帶列表選項的Dialog
public void setList(){
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
final String[] str = new String[]{"one","two","three"};
/*
*在setItems中有兩個參數,
*第一個參數是選項數據源,格式不固定,我在此使用的是字符串數組,若想實現更高級的選項模式,可使用Adapter,仿照第二講裏介紹的那樣即可,在這裏不再敘述
*第二個參數就是監聽,用於處理點擊選項後的操作*/
dialog.setItems(str, new OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int position) {
// TODO Auto-generated method stub
Toast.makeText(context, "你選擇了"+str[position], Toast.LENGTH_LONG).show();
}
});
dialog.create().show();
}
//帶單選項的Dialog
public void setSingleList(){
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
final String[] str = new String[]{"one","two","three","four"};
/*
*在setSingleChoiceItems中有三個參數,
*第一個參數是選項數據源,格式不固定,我在此使用的是字符串數組,若想實現更高級的選項模式,可使用Adapter,仿照第二講裏介紹的那樣即可,在這裏不再敘述
*第二個參數是當前選中的選項,在列表項中是從0開始計數的,所以我在此設置爲-1,即表示初始化生成後不選中任何選項
*第三個參數就是監聽,用於處理點擊選項後的操作*/
dialog.setSingleChoiceItems(str, -1, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int position) {
// TODO Auto-generated method stub
Toast.makeText(context, "你選擇了"+str[position], Toast.LENGTH_LONG).show();
}
});
dialog.create().show();
}
//加載進度條框
public void setProgreess(){
/* ProgressDialog是AlertDialog的子類,
* 可以顯示進度動畫:用旋轉的環表示進度未定義的task;用進度條表示定義了進度的task。
* 這個類型的dialog也可以提供按鈕,比如下載過程中的取消按鈕。 打開一個進度dialog簡單到只要調用ProgressDialog.show()就可以了。
**/
ProgressDialog progress = new ProgressDialog(context);
/*
* 這是一個最簡單的進度條,用以表示進度未定義的task
* 第一個參數是程序的context,
* 第二個參數是標題信息
* 第三個參數表示這個進程是不清楚的,它只在創建進度條的時候有意義 */
// progress.show(context, "標題", "下載中,請耐心等待",true);
/*
* 下面來做一個可移動化顯示進度的加載框
*/
//設置當前進度條的格式爲水平模式
progress.setProgress(ProgressDialog.STYLE_HORIZONTAL);
progress.setMessage("下載中");
//設置當前進度條是否可通過點擊陰影區關 閉,true可以,false 不可以
progress.setCancelable(true);
progress.show();
//真正應用進度條時,大家應該把ProgressDialog和Handler結合起來使用
//因爲控制進度條的變化最終需要Thread的接入,而Handler可以再線程之間相互傳值,控制progressDialog的開啓和關閉,我會在Dialog常用方法(二)中舉一個很實用的小例子。
}
//時間設置對話框
public void setTimer(){
Calendar can = Calendar.getInstance();
TimePickerDialog.OnTimeSetListener timeListener = new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker arg0, int hour, int minute) {
// TODO Auto-generated method stub
Toast.makeText(context, "你選擇了"+hour+"點"+minute+"分", Toast.LENGTH_LONG).show();
}
};
/**
* TimePickerDialog方法有五個參數:
* 第一個參數爲彈出的時間對話框所在的activity指針;
* 第二個參數是監聽類;
* 第三個參數是對話框彈出時的初始小時
* 第四個參數是彈出對話框的初始分鐘,
* 第五個參數爲設置24時顯示參數,true代表時間以24時制顯示時間,false爲不是以24小時顯示*/
TimePickerDialog dialog = new TimePickerDialog(context, timeListener, can.get(Calendar.HOUR_OF_DAY), can.get(Calendar.MINUTE), false);
dialog.show();
}
//日期設置對話框
public void setDate(){
Calendar can = Calendar.getInstance();
//設置日期監聽事件
DatePickerDialog.OnDateSetListener dateListener =
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker,
int year, int month, int dayOfMonth) {
//Calendar月份是從0開始,所以month要加1
Toast.makeText(context,"你選擇了" + year + "年" +
(month+1) + "月" + dayOfMonth + "日",Toast.LENGTH_LONG).show();
}
};
/**
* DatePickerDialog方法有五個參數:
* 第一個參數爲彈出的時間對話框所在的activity指針;
* 第二個參數是監聽類;
* 第三個參數是對話框彈出時的初始年份
* 第四個參數是對話框的初始月份,
* 第五個參數是對話框的初始日期,*/
DatePickerDialog dialog = new DatePickerDialog(context,dateListener , can.get(Calendar.YEAR),can.get(Calendar.MONTH), can.get(Calendar.DAY_OF_MONTH));
dialog.show();
}
}
小編這次選擇SecondFragment作爲展示頁面,繼續使用第一講裏創建的模板。
dHelper = new DialogHelper(getActivity());
bt_normal = (Button)view.findViewById(R.id.second_bt_normal);
bt_list = (Button)view.findViewById(R.id.second_bt_list);
bt_singlelist = (Button)view.findViewById(R.id.second_bt_singlelist);
bt_progress = (Button)view.findViewById(R.id.second_bt_progress);
bt_time = (Button)view.findViewById(R.id.second_bt_time);
bt_date = (Button)view.findViewById(R.id.second_bt_date);
bt_normal.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
dHelper.setNormal();
}
});
bt_list.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
dHelper.setList();
}
});
bt_singlelist.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
dHelper.setSingleList();
}
});
bt_progress.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
dHelper.setProgreess();
}
});
bt_time.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
dHelper.setTimer();
}
});
bt_date.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
dHelper.setDate();
}
});
相應的SecongFragement的XML文件我們做以下修改,這是用IE打開的所以出現這種情況,在這裏先對大家說聲不好意思啊。
<?xml version="1.0" encoding="UTF-8"?>
-<LinearLayout android:orientation="vertical" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<!-- @author Arthur Lee -->
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="20sp" android:text="第二頁面" android:id="@+id/view_second"/>
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="普通對話框" android:id="@+id/second_bt_normal"/>
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="帶列表的對話框" android:id="@+id/second_bt_list"/>
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="帶單選列表的對話框" android:id="@+id/second_bt_singlelist"/>
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="進度對話框" android:id="@+id/second_bt_progress"/>
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="時間對話框" android:id="@+id/second_bt_time"/>
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="日期對話框" android:id="@+id/second_bt_date"/>
</LinearLayout>
下面我們來看一下效果圖,
最後,還是用一句話結尾:我不是一名好的程序員,因爲我只會默默奉獻。