Adrnoid開發(fā)系列(二十五):使用AlertDialog創(chuàng)建各種類型的對話框
來源:程序員人生 發(fā)布時(shí)間:2014-11-07 08:37:22 閱讀次數(shù):2056次
AlertDialog可以生成各種內(nèi)容的對話框,但是每種對話框都會(huì)有這類的結(jié)構(gòu):

類似下邊這類的:

這只是最簡單的對話框。
我們來看下創(chuàng)建1個(gè)對話框需要的步驟:
1、使用創(chuàng)建AlertDialog.Builder對象
2、調(diào)用AlertDialog.Builder的setTitle()或setCustomTitle()方法設(shè)置標(biāo)題
3、調(diào)用AlertDialog.Builder的setIcon()方法設(shè)置圖標(biāo)
4、調(diào)用1些其他設(shè)置方法設(shè)置標(biāo)題
5、調(diào)用AlertDialog.Builder的setPositiveButton()、setNegativeButton()或setNeutralButton()添加多個(gè)按鈕
6、調(diào)用create()方法創(chuàng)建AlertDialog對象,再調(diào)用AlertDialog對象的show()方法將該對話框顯示出來。
新建Android項(xiàng)目,然后編寫main.xml:
<?xml version="1.0" encoding="utf⑻"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<!-- 顯示1個(gè)普通的文本編輯框組件 -->
<EditText
android:id="@+id/show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:editable="false"/>
<!-- 定義1個(gè)普通的按鈕組件 -->
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="簡單對話框"
android:onClick="simple"
/>
<!-- 定義1個(gè)普通的按鈕組件 -->
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="簡單列表項(xiàng)對話框"
android:onClick="simpleList"
/>
<!-- 定義1個(gè)普通的按鈕組件 -->
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="單選列表項(xiàng)對話框"
android:onClick="singleChoice"
/>
<!-- 定義1個(gè)普通的按鈕組件 -->
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="多選列表項(xiàng)對話框"
android:onClick="multiChoice"
/>
<!-- 定義1個(gè)普通的按鈕組件 -->
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定義列表項(xiàng)對話框"
android:onClick="customList"
/>
<!-- 定義1個(gè)普通的按鈕組件 -->
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定義View對話框"
android:onClick="customView"
/>
</LinearLayout>
這里是定義了6個(gè)按鈕和1個(gè)文本顯示框,并且設(shè)置了相應(yīng)的onClick屬性
接下來,我們就要編寫主界面的java代碼:AlertDialogTest.java
package org.crazyit.ui;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.TableLayout;
import android.widget.TextView;
public class AlertDialogTest extends Activity
{
TextView show;
String[] items = new String[] {
"瘋狂Java講義", "瘋狂Ajax講義",
"輕量級(jí)Java EE企業(yè)利用實(shí)戰(zhàn)",
"瘋狂Android講義" };
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
show = (TextView) findViewById(R.id.show);
}
public void simple(View source)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this)
// 設(shè)置對話框標(biāo)題
.setTitle("這是對話框標(biāo)題")
// 設(shè)置圖標(biāo)
.setIcon(R.drawable.tools)
.setMessage("這是對話框內(nèi)容");
// 為AlertDialog.Builder添加【肯定】按鈕
setPositiveButton(builder);
// 為AlertDialog.Builder添加【取消】按鈕
setNegativeButton(builder)
.create()
.show();
}
public void simpleList(View source)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this)
// 設(shè)置對話框標(biāo)題
.setTitle("簡單列表項(xiàng)對話框")
// 設(shè)置圖標(biāo)
.setIcon(R.drawable.tools)
// 設(shè)置簡單的列表項(xiàng)內(nèi)容
.setItems(items, new OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
show.setText("你選中了《" + items[which] + "》");
}
});
// 為AlertDialog.Builder添加【肯定】按鈕
setPositiveButton(builder);
// 為AlertDialog.Builder添加【取消】按鈕
setNegativeButton(builder)
.create()
.show();
}
public void singleChoice(View source)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this)
// 設(shè)置對話框標(biāo)題
.setTitle("單選列表項(xiàng)對話框")
// 設(shè)置圖標(biāo)
.setIcon(R.drawable.tools)
// 設(shè)置單選列表項(xiàng),默許選中第2項(xiàng)(索引為1)
.setSingleChoiceItems(items, 1, new OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
show.setText("你選中了《" + items[which] + "》");
}
});
// 為AlertDialog.Builder添加【肯定】按鈕
setPositiveButton(builder);
// 為AlertDialog.Builder添加【取消】按鈕
setNegativeButton(builder)
.create()
.show();
}
public void multiChoice(View source)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this)
// 設(shè)置對話框標(biāo)題
.setTitle("多選列表項(xiàng)對話框")
// 設(shè)置圖標(biāo)
.setIcon(R.drawable.tools)
// 設(shè)置多選列表項(xiàng),設(shè)置勾選第2項(xiàng)、第4項(xiàng)
.setMultiChoiceItems(items
, new boolean[]{false , true ,false ,true}, null);
// 為AlertDialog.Builder添加【肯定】按鈕
setPositiveButton(builder);
// 為AlertDialog.Builder添加【取消】按鈕
setNegativeButton(builder)
.create()
.show();
}
public void customList(View source)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this)
// 設(shè)置對話框標(biāo)題
.setTitle("自定義列表項(xiàng)對話框")
// 設(shè)置圖標(biāo)
.setIcon(R.drawable.tools)
// 設(shè)置自定義列表項(xiàng)
.setAdapter(new ArrayAdapter<String>(this
, R.layout.array_item
, items), null);
// 為AlertDialog.Builder添加【肯定】按鈕
setPositiveButton(builder);
// 為AlertDialog.Builder添加【取消】按鈕
setNegativeButton(builder)
.create()
.show();
}
public void customView(View source)
{
//裝載/res/layout/login.xml界面布局
TableLayout loginForm = (TableLayout)getLayoutInflater()
.inflate( R.layout.login, null);
new AlertDialog.Builder(this)
// 設(shè)置對話框的圖標(biāo)
.setIcon(R.drawable.tools)
// 設(shè)置對話框的標(biāo)題
.setTitle("自定義View對話框")
// 設(shè)置對話框顯示的View對象
.setView(loginForm)
// 為對話框設(shè)置1個(gè)“肯定”按鈕
.setPositiveButton("登錄" , new OnClickListener()
{
@Override
public void onClick(DialogInterface dialog,
int which)
{
// 此處可履行登錄處理
}
})
// 為對話框設(shè)置1個(gè)“取消”按鈕
.setNegativeButton("取消", new OnClickListener()
{
@Override
public void onClick(DialogInterface dialog,
int which)
{
// 取消登錄,不做任何事情。
}
})
// 創(chuàng)建、并顯示對話框
.create()
.show();
}
private AlertDialog.Builder setPositiveButton(
AlertDialog.Builder builder)
{
// 調(diào)用setPositiveButton方法添加肯定按鈕
return builder.setPositiveButton("肯定", new OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
show.setText("單擊了【肯定】按鈕!");
}
});
}
private AlertDialog.Builder setNegativeButton(
AlertDialog.Builder builder)
{
// 調(diào)用setNegativeButton方法添加取消按鈕
return builder.setNegativeButton("取消", new OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
show.setText("單擊了【取消】按鈕!");
}
});
}
}
在這里邊,第5個(gè)和第6個(gè)按鈕用到了兩個(gè)樣式:array_item.xml和login.xml
我們看下他們的內(nèi)容:
array_item.xml:
<?xml version="1.0" encoding="utf⑻"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/TextView"
android:textColor="#f0f"
android:textSize="30dp"
android:shadowColor="#ff0"
android:shadowRadius="2"
android:shadowDx="5"
android:shadowDy="5"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
login.xml:
<?xml version="1.0" encoding="utf⑻"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/loginForm"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="用戶名:"
android:textSize="10pt"
/>
<!-- 輸入用戶名的文本框 -->
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="請?zhí)顚懙卿泿ぬ?hào)"
android:selectAllOnFocus="true"
/>
</TableRow>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="密碼:"
android:textSize="10pt"
/>
<!-- 輸入密碼的文本框 -->
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="請?zhí)顚懨艽a"
android:password="true"
/>
</TableRow>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="電話號(hào)碼:"
android:textSize="10pt"
/>
<!-- 輸入電話號(hào)碼的文本框 -->
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="請?zhí)顚懩碾娫捥?hào)碼"
android:selectAllOnFocus="true"
android:phoneNumber="true"
/>
</TableRow>
</TableLayout>
通過AlertDialog可以制作出不同風(fēng)格的對話框,在很多時(shí)候都比較有用
并且我們可以通過肯定按鈕來把數(shù)據(jù)通過Intent傳遞到另外1個(gè)界面中。
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)