轉載請備註出自於:http://blog.csdn.net/qq_22118507/article/details/51524086
1.AlertDialog對話框
AlertDialog的構造方法全部是Protected的,所以不能直接通過new一個AlertDialog來創建出一個AlertDialog。要創建一個AlertDialog,就要用到AlertDialog.Builder中的create()方法。
使用AlertDialog.Builder創建對話框需要了解以下幾個方法:
(1)普通框
AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this);
- builder.setIcon(R.drawable.icon);
- builder.setTitle("投票");
- builder.setMessage("您認爲什麼樣的內容能吸引您?");
- builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int whichButton) {
- }
- });
- builder.setNeutralButton("忽略", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int whichButton) {
- }
- });
- builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int whichButton) {
- }
- });
- builder.create().show();
這個數組用於列表選擇
- final String[] mItems = {"項目1","項目2","項目3","項目4","項目5","項目6","項目7"};
- AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this);
- builder.setTitle("列表選擇框");
- builder.setItems(mItems, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- //點擊後彈出窗口選擇了第幾項
- }
- });
- builder.create().show();
mSingleChoice
用於記錄單選中的ID
- final String[] mItems = {"item0","item1","itme2","item3","itme4","item5","item6"};
- AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this);
- mSingleChoiceID = -1;
- builder.setIcon(R.drawable.icon);
- builder.setTitle("單項選擇");
- builder.setSingleChoiceItems(mItems, 0, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int whichButton) {
- mSingleChoiceID = whichButton;
- }
- });
- builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int whichButton) {
- if(mSingleChoiceID > 0) {
- }
- }
- });
- builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int whichButton) {
- }
- });
- builder.create().show();
MultiChoiceID 用於記錄多選選中的id號 存在ArrayList中
選中後 add 進ArrayList
取消選中後 remove 出ArrayList。
- ArrayList <Integer>MultiChoiceID = new ArrayList <Integer>();
- AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this);
- MultiChoiceID.clear();
- builder.setIcon(R.drawable.icon);
- builder.setTitle("多項選擇");
- builder.setMultiChoiceItems(mItems,
- new boolean[]{false, false, false, false, false, false, false},
- new DialogInterface.OnMultiChoiceClickListener() {
- public void onClick(DialogInterface dialog, int whichButton,
- boolean isChecked) {
- if(isChecked) {
- MultiChoiceID.add(whichButton);
- showDialog("你選擇的id爲" + whichButton + " , " + mItems[whichButton]);
- }else {
- MultiChoiceID.remove(whichButton);
- }
- }
- });
- builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int whichButton) {
- String str = "";
- int size = MultiChoiceID.size();
- for (int i = 0 ;i < size; i++) {
- str+= mItems[MultiChoiceID.get(i)] + ", ";
- }
- showDialog("你選擇的是" + str);
- }
- });
- builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int whichButton) {
- }
- });
- builder.create().show();
- AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this);
- LayoutInflater factory = LayoutInflater.from(this);
- final View textEntryView = factory.inflate(R.layout.test, null);
- builder.setIcon(R.drawable.icon);
- builder.setTitle("自定義輸入框");
- builder.setView(textEntryView);
- builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int whichButton) {
- EditText userName = (EditText) textEntryView.findViewById(R.id.etUserName);
- EditText password = (EditText) textEntryView.findViewById(R.id.etPassWord);
- showDialog("姓名 :" + userName.getText().toString() + "密碼:" + password.getText().toString() );
- }
- });
- builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int whichButton) {
- }
- });
- builder.create().show();
-
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:orientation="horizontal"
- android:id="@+id/dialog">
- <LinearLayout
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:orientation="horizontal"
- android:id="@+id/dialogname">
- <TextView android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:id="@+id/tvUserName"
- android:text="姓名:" />
- <EditText android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:id="@+id/etUserName"
- android:minWidth="200dip"/>
- </LinearLayout>
- <LinearLayout
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:orientation="horizontal"
- android:id="@+id/dialognum"
- android:layout_below="@+id/dialogname"
- >
- <TextView android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:id="@+id/tvPassWord"
- android:text="密碼:" />
- <EditText android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:id="@+id/etPassWord"
- android:minWidth="200dip"/>
- </LinearLayout>
- </RelativeLayout>
layout中datepickerdialog.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"
-
android:background="#ffffffff"
-
>
-
<TextView
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:id="@+id/showtime"
-
android:textColor="#ff000000"
-
android:text=""
-
/>
-
<Button
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:id="@+id/setdate"
- android:text="@string/setdate"
- />
-
package com.example.testview;
-
-
import java.util.Calendar;
-
import java.util.Date;
-
import java.util.Locale;
-
-
import android.app.Activity;
-
import android.os.Bundle;
-
import android.widget.Button;
-
import android.widget.DatePicker;
-
import android.widget.TextView;
-
import android.view.View;
-
import android.view.View.OnClickListener;
-
import android.app.DatePickerDialog;
-
-
/**
-
*
-
* DatePickerDialog是設置日期對話框,通過OnDateSetListener監聽並重新設置日期,
-
* 當日期被重置後,會執行OnDateSetLintener類中的方法onDateSet()
-
*
-
*/
-
-
-
public class DatePickerDialogExample extends Activity {
-
-
private TextView showdate;
-
private Button setdate;
-
private int year;
-
private int month;
-
private int day;
-
-
-
@Override
-
public void onCreate(Bundle savedInstanceState)
-
{
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.datepickerdialog);
-
-
showdate=(TextView) this.findViewById(R.id.showtime);
-
setdate=(Button) this.findViewById(R.id.setdate);
-
//初始化Calendar日曆對象
-
Calendar mycalendar=Calendar.getInstance(Locale.CHINA);
-
Date mydate=new Date(); //獲取當前日期Date對象
-
mycalendar.setTime(mydate);////爲Calendar對象設置時間爲當前日期
-
-
year=mycalendar.get(Calendar.YEAR); //獲取Calendar對象中的年
-
month=mycalendar.get(Calendar.MONTH);//獲取Calendar對象中的月
-
day=mycalendar.get(Calendar.DAY_OF_MONTH);//獲取這個月的第幾天
-
showdate.setText("當前日期:"+year+"-"+(month+1)+"-"+day); //顯示當前的年月日
-
-
//添加單擊事件--設置日期
-
setdate.setOnClickListener(new OnClickListener(){
-
-
@Override
-
public void onClick(View v)
-
{
-
/**
-
* 構造函數原型:
-
* public DatePickerDialog (Context context, DatePickerDialog.OnDateSetListener callBack,
-
* int year, int monthOfYear, int dayOfMonth)
-
* content組件運行Activity,
-
* DatePickerDialog.OnDateSetListener:選擇日期事件
-
* year:當前組件上顯示的年,monthOfYear:當前組件上顯示的月,dayOfMonth:當前組件上顯示的第幾天
-
*
-
*/
-
//創建DatePickerDialog對象
-
DatePickerDialog dpd=new DatePickerDialog(DatePickerDialogExample.this,Datelistener,year,month,day);
-
dpd.show();//顯示DatePickerDialog組件
-
}
-
});
-
-
}
-
private DatePickerDialog.OnDateSetListener Datelistener=new DatePickerDialog.OnDateSetListener()
-
{
-
/**params:view:該事件關聯的組件
-
* params:myyear:當前選擇的年
-
* params:monthOfYear:當前選擇的月
-
* params:dayOfMonth:當前選擇的日
-
*/
-
@Override
-
public void onDateSet(DatePicker view, int myyear, int monthOfYear,int dayOfMonth) {
-
-
-
//修改year、month、day的變量值,以便以後單擊按鈕時,DatePickerDialog上顯示上一次修改後的值
-
year=myyear;
-
month=monthOfYear;
-
day=dayOfMonth;
-
//更新日期
-
updateDate();
-
-
}
-
//當DatePickerDialog關閉時,更新日期顯示
-
private void updateDate()
-
{
-
//在TextView上顯示日期
-
showdate.setText("當前日期:"+year+"-"+(month+1)+"-"+day);
-
}
-
};
-
-
- }
3.自定義對話框
public SelectDialog(Context context, int theme) {
super(context, theme);
}
public SelectDialog(Context context) {
super(context);
}
public setContentView(int view) {
setContentView(int view);
}
}