国产chinesehd精品酒店,邱淑贞aa极毛片,国产在线色视频,玖玖爱zh综合伊人久久

網(wǎng)站首頁(yè)
手機(jī)版

Android的AlertDialog詳解(android alert)

更新時(shí)間:2024-03-30 00:16:07作者:未知

Android的AlertDialog詳解(android alert)

AlertDialog的構(gòu)造方法全部是Protected的,所以不能直接通過(guò)new一個(gè)AlertDialog來(lái)創(chuàng)建出一個(gè)AlertDialog。
要?jiǎng)?chuàng)建一個(gè)AlertDialog,就要用到AlertDialog.Builder中的create()方法。
使用AlertDialog.Builder創(chuàng)建對(duì)話框需要了解以下幾個(gè)方法:
setTitle :為對(duì)話框設(shè)置標(biāo)題
setIcon :為對(duì)話框設(shè)置圖標(biāo)
setMessage:為對(duì)話框設(shè)置內(nèi)容
setView : 給對(duì)話框設(shè)置自定義樣式
setItems :設(shè)置對(duì)話框要顯示的一個(gè)list,一般用于顯示幾個(gè)命令時(shí)
setMultiChoiceItems :用來(lái)設(shè)置對(duì)話框顯示一系列的復(fù)選框
setNeutralButton :普通按鈕
setPositiveButton :給對(duì)話框添加"Yes"按鈕
setNegativeButton :對(duì)話框添加"No"按鈕
create : 創(chuàng)建對(duì)話框
show :顯示對(duì)話框
一、簡(jiǎn)單的AlertDialog
下面,創(chuàng)建一個(gè)簡(jiǎn)單的ALertDialog并顯示它:

[java] package com.tianjf;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;

public class Dialog_AlertDialogDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("對(duì)話框的標(biāo)題").
setMessage("對(duì)話框的內(nèi)容").
setIcon(R.drawable.ic_launcher).
create();
alertDialog.show();
}
}
package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
public class Dialog_AlertDialogDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("對(duì)話框的標(biāo)題").
setMessage("對(duì)話框的內(nèi)容").
setIcon(R.drawable.ic_launcher).
create();
alertDialog.show();
}
}運(yùn)行結(jié)果如下:
Android的AlertDialog詳解

二、帶按鈕的AlertDialog
上面的例子很簡(jiǎn)單,下面我們?cè)谶@個(gè)AlertDialog上面加幾個(gè)Button,實(shí)現(xiàn)刪除操作的提示對(duì)話框

[java] package com.tianjf;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;

public class Dialog_AlertDialogDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("確定刪除?").
setMessage("您確定刪除該條信息嗎?").
setIcon(R.drawable.ic_launcher).
setPositiveButton("確定", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
setNeutralButton("查看詳情", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}
package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
public class Dialog_AlertDialogDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("確定刪除?").
setMessage("您確定刪除該條信息嗎?").
setIcon(R.drawable.ic_launcher).
setPositiveButton("確定", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
setNeutralButton("查看詳情", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}在這個(gè)例子中,我們定義了三個(gè)按鈕,分別是"Yes"按鈕,"No"按鈕以及一個(gè)普通按鈕,每個(gè)按鈕都有onClick事件,TODO的地方可以放點(diǎn)了按鈕之后想要做的一些處理
看一下運(yùn)行結(jié)果:
Android的AlertDialog詳解

可以看到三個(gè)按鈕添加到了AlertDialog上,三個(gè)沒(méi)有添加事件處理的按鈕,點(diǎn)了只是關(guān)閉對(duì)話框,沒(méi)有任何其他操作。

三、類似ListView的AlertDialog
用setItems(CharSequence[] items, final OnClickListener listener)方法來(lái)實(shí)現(xiàn)類似ListView的AlertDialog
第一個(gè)參數(shù)是要顯示的數(shù)據(jù)的數(shù)組,第二個(gè)參數(shù)是點(diǎn)擊某個(gè)item的觸發(fā)事件

[java] package com.tianjf;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;

public class Dialog_AlertDialogDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final String[] arrayFruit = new String[] { "蘋果", "橘子", "草莓", "香蕉" };

Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("你喜歡吃哪種水果?").
setIcon(R.drawable.ic_launcher)
.setItems(arrayFruit, new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Dialog_AlertDialogDemoActivity.this, arrayFruit[which], Toast.LENGTH_SHORT).show();
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}
package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
public class Dialog_AlertDialogDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final String[] arrayFruit = new String[] { "蘋果", "橘子", "草莓", "香蕉" };
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("你喜歡吃哪種水果?").
setIcon(R.drawable.ic_launcher)
.setItems(arrayFruit, new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Dialog_AlertDialogDemoActivity.this, arrayFruit[which], Toast.LENGTH_SHORT).show();
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}運(yùn)行結(jié)果如下:
Android的AlertDialog詳解

四、類似RadioButton的AlertDialog
用setSingleChoiceItems(CharSequence[] items, int checkedItem, final OnClickListener listener)方法來(lái)實(shí)現(xiàn)類似RadioButton的AlertDialog
第一個(gè)參數(shù)是要顯示的數(shù)據(jù)的數(shù)組,第二個(gè)參數(shù)是初始值(初始被選中的item),第三個(gè)參數(shù)是點(diǎn)擊某個(gè)item的觸發(fā)事件
在這個(gè)例子里面我們?cè)O(shè)了一個(gè)selectedFruitIndex用來(lái)記住選中的item的index

[java] package com.tianjf;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;

public class Dialog_AlertDialogDemoActivity extends Activity {

private int selectedFruitIndex = 0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final String[] arrayFruit = new String[] { "蘋果", "橘子", "草莓", "香蕉" };

Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("你喜歡吃哪種水果?").
setIcon(R.drawable.ic_launcher)
.setSingleChoiceItems(arrayFruit, 0, new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
selectedFruitIndex = which;
}
}).
setPositiveButton("確認(rèn)", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Dialog_AlertDialogDemoActivity.this, arrayFruit[selectedFruitIndex], Toast.LENGTH_SHORT).show();
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}
package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
public class Dialog_AlertDialogDemoActivity extends Activity {

private int selectedFruitIndex = 0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final String[] arrayFruit = new String[] { "蘋果", "橘子", "草莓", "香蕉" };
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("你喜歡吃哪種水果?").
setIcon(R.drawable.ic_launcher)
.setSingleChoiceItems(arrayFruit, 0, new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
selectedFruitIndex = which;
}
}).
setPositiveButton("確認(rèn)", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Dialog_AlertDialogDemoActivity.this, arrayFruit[selectedFruitIndex], Toast.LENGTH_SHORT).show();
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}
運(yùn)行結(jié)果如下:
Android的AlertDialog詳解

五、類似CheckBox的AlertDialog
用setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, final OnMultiChoiceClickListener listener)方法來(lái)實(shí)現(xiàn)類似CheckBox的AlertDialog
第一個(gè)參數(shù)是要顯示的數(shù)據(jù)的數(shù)組,第二個(gè)參數(shù)是選中狀態(tài)的數(shù)組,第三個(gè)參數(shù)是點(diǎn)擊某個(gè)item的觸發(fā)事件

[java] package com.tianjf;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;

public class Dialog_AlertDialogDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final String[] arrayFruit = new String[] { "蘋果", "橘子", "草莓", "香蕉" };
final boolean[] arrayFruitSelected = new boolean[] {true, true, false, false};

Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("你喜歡吃哪種水果?").
setIcon(R.drawable.ic_launcher)
.setMultiChoiceItems(arrayFruit, arrayFruitSelected, new DialogInterface.OnMultiChoiceClickListener() {

@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
arrayFruitSelected[which] = isChecked;
}
}).
setPositiveButton("確認(rèn)", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < arrayFruitSelected.length; i++) {
if (arrayFruitSelected[i] == true)
{
stringBuilder.append(arrayFruit[i] + "、");
}
}
Toast.makeText(Dialog_AlertDialogDemoActivity.this, stringBuilder.toString(), Toast.LENGTH_SHORT).show();
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}
package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
public class Dialog_AlertDialogDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final String[] arrayFruit = new String[] { "蘋果", "橘子", "草莓", "香蕉" };
final boolean[] arrayFruitSelected = new boolean[] {true, true, false, false};
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("你喜歡吃哪種水果?").
setIcon(R.drawable.ic_launcher)
.setMultiChoiceItems(arrayFruit, arrayFruitSelected, new DialogInterface.OnMultiChoiceClickListener() {

@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
arrayFruitSelected[which] = isChecked;
}
}).
setPositiveButton("確認(rèn)", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < arrayFruitSelected.length; i++) {
if (arrayFruitSelected[i] == true)
{
stringBuilder.append(arrayFruit[i] + "、");
}
}
Toast.makeText(Dialog_AlertDialogDemoActivity.this, stringBuilder.toString(), Toast.LENGTH_SHORT).show();
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}運(yùn)行結(jié)果如下:
Android的AlertDialog詳解

六、自定義View的AlertDialog
有時(shí)候我們不能滿足系統(tǒng)自帶的AlertDialog風(fēng)格,就比如說(shuō)我們要實(shí)現(xiàn)一個(gè)Login畫面,有用戶名和密碼,這時(shí)我們就要用到自定義View的AlertDialog
先創(chuàng)建Login畫面的布局文件
[html] <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" >

<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/user" />

<EditText
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" >

<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/passward" />

<EditText
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/user" />
<EditText
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/passward" />
<EditText
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
然后在Activity里面把Login畫面的布局文件添加到AlertDialog上
[java] package com.tianjf;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

public class Dialog_AlertDialogDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// 取得自定義View
LayoutInflater layoutInflater = LayoutInflater.from(this);
View myLoginView = layoutInflater.inflate(R.layout.login, null);

Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("用戶登錄").
setIcon(R.drawable.ic_launcher).
setView(myLoginView).
setPositiveButton("登錄", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}
package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
public class Dialog_AlertDialogDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 取得自定義View
LayoutInflater layoutInflater = LayoutInflater.from(this);
View myLoginView = layoutInflater.inflate(R.layout.login, null);

Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("用戶登錄").
setIcon(R.drawable.ic_launcher).
setView(myLoginView).
setPositiveButton("登錄", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}運(yùn)行結(jié)果如下:
Android的AlertDialog詳解
摘自 殤?的專欄

本文標(biāo)簽: 對(duì)話框  按鈕  你喜歡  

為您推薦

Android的AlertDialog詳解(android alert)

Android的AlertDialog詳解 AlertDialog的構(gòu)造方法全部是Protected的,所以不能直接通過(guò)new一個(gè)AlertDialog來(lái)創(chuàng)建出

2024-03-30 00:16

解決Mono(解決某種癢點(diǎn)的文案)

解決Mono For Android不能編譯問(wèn)題:Could not create the Android package 最近一直在用Xamarin Mono

2024-03-30 00:16

mt65xx android phone驅(qū)動(dòng)

mt65xx android phone驅(qū)動(dòng) 第一部分:在Windows XP下,安裝Android Phone驅(qū)動(dòng) 1. 下載附件中的驅(qū)動(dòng)壓縮包,下載后放在桌

2024-03-30 00:16

刷機(jī)神器(刷機(jī)神器_手機(jī)掛機(jī)賺錢)

刷機(jī)神器 甜椒刷機(jī)助手For Android使用教程 2012年是一個(gè)變革的時(shí)代。自從騰訊、360、百度等幾大互聯(lián)網(wǎng)巨頭強(qiáng)勢(shì)進(jìn)入刷機(jī)市場(chǎng),這讓原本還處于玩家級(jí)別

2024-03-30 00:15

Android 4.0 SDK 環(huán)境搭建體驗(yàn) (Windows 7 Ultimate) 圖文介紹

Android 4.0 SDK 環(huán)境搭建體驗(yàn) (Windows 7 Ultimate) 圖文介紹 Android 4.0 SDK 已經(jīng)出來(lái)好一段時(shí)間了,早就想試

2024-03-30 00:15

最快速的Android開發(fā)環(huán)境搭建ADT-Bundle及Hello World

最快速的Android開發(fā)環(huán)境搭建ADT-Bundle及Hello World ( 由于現(xiàn)在Google已經(jīng)推出了android studio,想快速加入到an

2024-03-30 00:15