如何使用DialogFragment類來創建和顯示對話框
本示例演示如何使用DialogFragment類來顯示和管理一個AlertDialog對話框。代碼在Android3.0中編譯測試通過。</span>
1. 定義清單文件(AndroidManifest.xml)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="my.android.test"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="11" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<!-- android:enabled
這個屬性用于指定Activity是否能夠被安裝到系統中,如果設置為true則能夠安裝,否則不能安裝
默認設置時true -->
<activity android:name=".FragmentAlertDialog"
android:label="@string/app_name"
android:enabled="@bool/atLeastHoneycomb">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
2. 定義資源文件(strings.xml和bools.xml)
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, FragmentAlertDialog!</string>
<string name="app_name">FragmentDialog</string>
<string name="fragment_hide_show">Fragment/Hide and Show</string>
<string name="fragment_context_menu">Fragment/Context Menu</string>
<string name="fragment_context_menu_msg">Fragment populating a context
menu; long press the button to see.</string>
<string name="long_press">Long press me</string>
<string name="fragment_dialog">App/Fragment/Dialog</string>
<string name="show">Show</string>
<string name="alert_dialog_two_buttons_title">
Lorem ipsum dolor sit aie consectetur adipiscing\nPlloaso mako nuto
siwuf cakso dodtos anr koop.
</string>
<string name="alert_dialog_cancel">Cancel</string>
<string name="alert_dialog_ok">OK</string>
</resources>
bools.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="atLeastHoneycomb">true</bool>
</resources>
3. 定義布局文件(fragment_dialog.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:padding="4dip"
android:gravity="center_horizontal"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical|center_horizontal"
android:textAppearance="?android:attr/textAppearanceMedium"
android:gravity="top|center_horizontal" />
<Button android:id="@+id/show"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_weight="0"
android:text="@string/show">
<requestFocus />
</Button>
</LinearLayout>
4. 創建Activity類文件(FragmentAlertDialog.java)
package my.android.test;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
publicclass FragmentAlertDialogextends Activity {
/** 當Activity被首次創建時,系統會調用這個方法 */
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//把布局文件填充到Activity中
setContentView(R.layout.fragment_dialog);
//查找布局中的text元素,并設置要顯示的文本。
View tv = findViewById(R.id.text);
((TextView)tv).setText("Example of displaying an alert dialog with a DialogFragment");
//查找布局中的show按鈕,并設置點擊事件監聽器。用戶點擊時該按鈕時,顯示一個Fragment對話框。
Button button = (Button)findViewById(R.id.show);
button.setOnClickListener(new OnClickListener(){
publicvoid onClick(View v){
showDialog();
}
});
}
/**
* 顯示Fragment對話框。
*/
void showDialog(){
//獲取Fragment對話框實例,在獲取實例的時候,設置這個對話框的標題。
DialogFragment newFragment = MyAlertDialogFragment.newInstance(
R.string.alert_dialog_two_buttons_title);
/**
* 顯示Fragment對話框。
* show(FragmentManager, String)方法:本方法把Fragment添加到給定的Fragment管理器中,并顯示對話框。
* 對于創建明確的事務、給Fragment對象添加給定的標簽,并提交這個事務,這個方法是非常方便的。
* 這個方法不會把事務添加到回退堆棧中。當這個Fragment被關閉時,要執行一個新的從Activity中
* 刪除這個Fragment對象的事務。
* FragmentManager:指定Fragment對象要被添加到Fragment管理器對象。
* String:指定Fragment對象的標簽。
*/
newFragment.show(getFragmentManager(), "dialog");
}
/**
* 定義Fragment對話框對象ok按鈕點擊時,要執行的方法。
*/
publicvoid doPositiveClick(){
Log.i("FragmentAlertDialog","Positive click!");
}
/**
* 定義Fragment對話框對象cancel按鈕點擊時,要執行的方法。
*/
publicvoid doNegativieClick(){
Log.i("FragmentAlertDialog","Negative click");
}
/**
* 定義Fragment對話框的靜態類,繼承DialogFragment類。
*/
publicstaticclass MyAlertDialogFragmentextends DialogFragment{
/**
* 創建Fragment對話框實例
* @param title:指定對話框的標題。
* @return:Fragment對話框實例。
*/
publicstatic MyAlertDialogFragment newInstance(int title){
MyAlertDialogFragment frag = new MyAlertDialogFragment();
Bundle args = new Bundle();
args.putInt("title", title);
frag.setArguments(args);
return frag;
}
/**
* 覆寫Fragment類的onCreateDialog方法,在FragmentDialog的show方法執行之后,
* 系統會調用這個回調方法。
*/
@Override
public Dialog onCreateDialog(Bundle saveInstanceState){
//獲取對象實例化時傳入的窗口標題。
int title = getArguments().getInt("title");
//返回提醒對話框。
returnnew AlertDialog.Builder(getActivity())
//設置標題圖標
.setIcon(R.drawable.alert_dialog_icon)
//設置標題
.setTitle(title)
//設置確定按鈕的標題和點擊事件監聽器。
.setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener() {
@Override
publicvoid onClick(DialogInterface dialog,int which) {
((FragmentAlertDialog)getActivity()).doPositiveClick();
}
}
)
//設置取消按鈕的標題和點擊事件監聽器。
.setNegativeButton(R.string.alert_dialog_cancel,
new DialogInterface.OnClickListener() {
@Override
publicvoid onClick(DialogInterface dialog,int which) {
((FragmentAlertDialog)getActivity()).doNegativieClick();
}
}
)
//創建對話框
.create();
}
}
}