【移動應(yīng)用開發(fā)技術(shù)】android之Dialog的使用_第1頁
【移動應(yīng)用開發(fā)技術(shù)】android之Dialog的使用_第2頁
【移動應(yīng)用開發(fā)技術(shù)】android之Dialog的使用_第3頁
【移動應(yīng)用開發(fā)技術(shù)】android之Dialog的使用_第4頁
【移動應(yīng)用開發(fā)技術(shù)】android之Dialog的使用_第5頁
已閱讀5頁,還剩10頁未讀, 繼續(xù)免費(fèi)閱讀

付費(fèi)下載

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

【移動應(yīng)用開發(fā)技術(shù)】android之Dialog的使用

一個對話框就是一個小窗體,提示用戶做出決定或輸入額外的信息。對話框不填滿屏幕,通常用于模態(tài)事件,要求用戶采取某些行動,程序才能繼續(xù)往下執(zhí)行。下面用在程序里程序具體說明Dialog的使用activity_main.xml中的內(nèi)容:<RelativeLayout

xmlns:android="/apk/res/android"

xmlns:tools="/tools"

android:layout_width="match_parent"

android:layout_height="match_parent">

<Button

android:id="@+id/button1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_alignParentTop="true"

android:onClick="showDialog"

android:text="普通對話框"

/>

<Button

android:id="@+id/button2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_below="@+id/button1"

android:onClick="showDialog"

android:text="有按鈕的對話框"

/>

<Button

android:id="@+id/button3"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_below="@+id/button2"

android:onClick="showDialog"

android:text="列表對話框"

/>

<Button

android:id="@+id/button4"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_below="@+id/button3"

android:onClick="showDialog"

android:text="單選對話框"

/>

<Button

android:id="@+id/button5"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_below="@+id/button4"

android:onClick="showDialog"

android:text="多選對話框"

/>

<Button

android:id="@+id/button6"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_below="@+id/button5"

android:onClick="showDialog"

android:text="自定義對話框"

/>

<Button

android:id="@+id/button7"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_below="@+id/button6"

android:onClick="showDialog"

android:text="TimePickDialog對話框"

/>

<Button

android:id="@+id/button8"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_below="@+id/button7"

android:onClick="showDialog"

android:text="DatePickerDialog對話框"

/>

</RelativeLayout>新建一個dialog_button_item.xml布局文件:<?xml

version="1.0"

encoding="utf-8"?>

<LinearLayout

xmlns:android="/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="horizontal"

>

<Button

android:id="@+id/button1"

android:layout_width="0dp"

android:layout_weight="1"

android:layout_height="wrap_content"

android:text="Button"

/>

<Button

android:id="@+id/button2"

android:layout_width="0dp"

android:layout_weight="1"

android:layout_height="wrap_content"

android:text="Button"

/>

<Button

android:id="@+id/button3"

android:layout_width="0dp"

android:layout_weight="1"

android:layout_height="wrap_content"

android:text="Button"

/>

<Button

android:id="@+id/button4"

android:layout_width="0dp"

android:layout_weight="1"

android:layout_height="wrap_content"

android:text="Button"

/>

</LinearLayout>MainActivity.java中的內(nèi)容:package

com.zzh.day11_dialog;

import

java.util.Calendar;

import

android.app.Activity;

import

android.app.AlertDialog;

import

android.app.DatePickerDialog;

import

android.app.DatePickerDialog.OnDateSetListener;

import

android.app.TimePickerDialog;

import

android.content.DialogInterface;

import

android.content.DialogInterface.OnClickListener;

import

android.content.DialogInterface.OnMultiChoiceClickListener;

import

android.os.Bundle;

import

android.view.View;

import

android.widget.Button;

import

android.widget.DatePicker;

import

android.widget.TimePicker;

import

android.widget.Toast;

public

class

MainActivity

extends

Activity

{

@Override

protected

void

onCreate(Bundle

savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}

/**

*

@param

view

*/

public

void

showDialog(View

view)

{

switch

(view.getId())

{

case

R.id.button1://普通對話框

AlertDialog.Builder

builder

=

new

AlertDialog.Builder(this);

builder.setTitle("普通對話框");//設(shè)置對話框的標(biāo)題

builder.setIcon(R.drawable.ic_launcher);//設(shè)置對話框的小圖標(biāo)

builder.setMessage("信息信息信息信息信息信息信息");//設(shè)置對話框要顯示的信息

AlertDialog

ad

=

builder.create();//創(chuàng)建AlertDialog的對象

ad.show();//設(shè)置對話框顯示

break;

case

R.id.button2://有按鈕的對話框

/**

*

setPositiveButton,setNegativeButton,setNeutralButton中參數(shù)的意義

*

參數(shù)一:按鈕上要顯示的文字

*

參數(shù)二:按鈕的點(diǎn)擊事件

*

*/

new

AlertDialog.Builder(this).setTitle("按鈕").setIcon(R.drawable.ic_launcher)//

.setMessage("有按鈕的對話框")//

.setPositiveButton("OK",

new

OnClickListener()//對OK按鈕的事件進(jìn)行監(jiān)聽,處理。后邊的setNegativeButton,setNeutralButton,也是一樣的。

{

@Override

public

void

onClick(DialogInterface

dialog,

int

which)

{

Toast.makeText(MainActivity.this,

"你點(diǎn)擊了OK按鈕",

Toast.LENGTH_SHORT).show();

}

})//

.setNegativeButton("Cencel",

null)//

.setNeutralButton("SO

SO",

null).show();

break;

case

R.id.button3://對話框的內(nèi)容是一個列表

new

AlertDialog.Builder(this).setTitle("列表").setIcon(R.drawable.ic_launcher)//

.setItems(new

String[]{"條目一","條目二","條目三","條目四"},

new

OnClickListener()

{

/*

*

DialogInterface

dialog:被點(diǎn)擊的對象

*

int

which:被點(diǎn)擊的位置

*/

@Override

public

void

onClick(DialogInterface

dialog,

int

which)

{

Toast.makeText(MainActivity.this,

which+"",

Toast.LENGTH_SHORT).show();

}

}).show();

break;

case

R.id.button4://對話框中的內(nèi)容是一個單選列表

/**

*

setSingleChoiceItems中的參數(shù)意義

*

參數(shù)一:單選框中有哪些條目,是一個數(shù)組

*

參數(shù)二:默認(rèn)被選中的單選框

*

參數(shù)三:點(diǎn)擊事件

*

重載的方法:

*

setSingleChoiceItems(int

itemsId,

int

checkedItem,

DialogInterface.OnClickListener

listener)

*

參數(shù)一:是把數(shù)組定義在xml文件中,在Java文件中使用R.array引用。

*

*/

new

AlertDialog.Builder(this).setTitle("單選").setIcon(R.drawable.ic_launcher)//

.setSingleChoiceItems(new

String[]{"red","blue","green"},

-1,

new

OnClickListener()

{

@Override

public

void

onClick(DialogInterface

dialog,

int

which)

{

Toast.makeText(MainActivity.this,

"選擇了第

"+(which+1)+"

個顏色",

Toast.LENGTH_SHORT).show();

}

}).show();

break;

case

R.id.button5://多選對話框

/**

*

setMultiChoiceItems(CharSequence[]

items,

boolean[]

checkedItems,

DialogInterface.OnMultiChoiceClickListener

listener)

*

參數(shù)一:多選框中的條目;

*

參數(shù)二:是否被選中,如果是null,則都沒有選中

*

參數(shù)三:與單選框的事件不同,DialogInterface.OnMultiChoiceClickListener

*

*/

new

AlertDialog.Builder(this).setTitle("多選").setIcon(R.drawable.ic_launcher)//

.setMultiChoiceItems(new

String[]{"red","blue","green"},

null,

new

OnMultiChoiceClickListener()

{

/*

onClick(DialogInterface

dialog,

int

which,

boolean

isChecked)

*

參數(shù)一:被點(diǎn)擊的對象;

*

參數(shù)二:被點(diǎn)擊的位置,位置是從0開始的

*

參數(shù)三:被點(diǎn)擊的對象是否選中,如果選中則是true,如果取消選中,則是false

*/

@Override

public

void

onClick(DialogInterface

dialog,

int

which,

boolean

isChecked)

{

Toast.makeText(MainActivity.this,

"選擇了第

"+(which+1)+"

個顏色",

Toast.LENGTH_SHORT).show();

}

}).show();

break;

case

R.id.button6://自定義對話框

View

view_button

=

getLayoutInflater().inflate(R.layout.dialog_button_item,

null);//轉(zhuǎn)換模板,將定義好的按鈕小xml文件轉(zhuǎn)換成View對象

final

AlertDialog

dialog

=

new

AlertDialog.Builder(this).setTitle("自定義").setIcon(R.drawable.ic_launcher).setView(view_button).create();//

dialog.show();

Button

btn

=

(Button)

view_button.findViewById(R.id.button1);//給按鈕設(shè)置點(diǎn)擊事件

btn.setOnClickListener(new

View.OnClickListener()

{

@Override

public

void

onClick(View

v)

{

Toast.makeText(MainActivity.this,

"lalalla",

Toast.LENGTH_SHORT).show();

dialog.dismiss();//事件執(zhí)行完成以后,對話框消失

}

});

break;

case

R.id.button7://時間選擇對話框,時分

/**

*

TimePickerDialog(Context

context,

TimePickerDialog.OnTimeSetListener

callBack,

int

hourOfDay,

int

minute,

boolean

is24HourView)

*

參數(shù)一:上下文

*

參數(shù)二:TimePickerDialog的點(diǎn)擊事件,

*

參數(shù)三:小時

*

參數(shù)四:分鐘

*

參數(shù)五:是否使用24小時制,是true

*

*/

TimePickerDialog

tpd

=

new

TimePickerDialog(this,new

TimePickerDialog.OnTimeSetListener()//此監(jiān)聽事件的接口是在TimePickerDialog.OnTimeSetListener()

{

@Override

public

void

onTimeSet(TimePicker

vi

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論