Dialog Fragment的優點是當Activity重起的時候
Diaolog上面的值也會跟著重設
就和Fragmentㄧ樣
1. 首先到官網http://developer.android.com/guide/topics/ui/dialogs.html#DialogFragment
2. 接著創見ㄧ個class 繼承 DialogFragment
然後override它的onCreatDialog方法
範例如下
public class TestDialog extends DialogFragment{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return super onCreareDialog(saveInstanceState);
}
}
3. 接著在裡面回傳AlertDialog就可以了
而用法可以參考前幾篇
範例如下
public class TestDialog extends DialogFragment{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("內容")
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// do what you want to do
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
4. 而在show dialog則直接new該class
然後用show(FragmentManager, TAG)就可以了
TestDialog dialog = new TestDialog();
dialog.show(getFragmentManager(), "TAG");
也可以參考官網http://developer.android.com/guide/topics/ui/dialogs.html#ShowingADialog
5. 最後就可以看到他和ㄧ般的Dialog一樣了
留言列表