DialogFragment
http://developer.android.com/guide/topics/ui/dialogs.html
허니컴 (API11) 부터 Dialog를 생성할때는 사용하는 객체이다.
이전 버전 부터 존재하던 AlertDialog를 사용하여 UI를 생성하는 방법과
Fragment를 쓰는 것과 동일한 방법으로 UI를 생성하는 방법이 있다.
Basic Dialog
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_dialog, container, false);
View tv = v.findViewById(R.id.text);
/**
* Create a new instance of MyDialogFragment, providing "num"
* as an argument.
*/
static MyDialogFragment newInstance(int num) {
MyDialogFragment f = new MyDialogFragment();
// Supply num input as an argument.
Bundle args = new Bundle();
args.putInt("num", num);
f.setArguments(args);
return f;
}
void showDialog() {
mStackLevel++;
// DialogFragment.show() will take care of adding the fragment
// in a transaction. We also want to remove any currently showing
// dialog, so make our own transaction and take care of that here.
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment prev = getFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);
// Create and show the dialog.
DialogFragment newFragment = MyDialogFragment.newInstance(mStackLevel);
newFragment.show(ft, "dialog");
}
Alert Dialog
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
int title = getArguments().getInt("title");
return new AlertDialog.Builder(getActivity())
.setIcon(R.drawable.alert_dialog_icon)
.setTitle(title)
.setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
((FragmentAlertDialog)getActivity()).doPositiveClick();
}
}
)
.setNegativeButton(R.string.alert_dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
((FragmentAlertDialog)getActivity()).doNegativeClick();
}
}
)
.create();
}
public static MyAlertDialogFragment newInstance(int title) {
MyAlertDialogFragment frag = new MyAlertDialogFragment();
Bundle args = new Bundle();
args.putInt("title", title);
frag.setArguments(args);
return frag;
}
void showDialog() {
DialogFragment newFragment = MyAlertDialogFragment.newInstance(
R.string.alert_dialog_two_buttons_title);
newFragment.show(getFragmentManager(), "dialog");
}
show 함수 인자가 다른 두개가 존재한다.
void show(FragmentManager manager, String tag)
Display the dialog, adding the fragment to the given FragmentManager.
int show(FragmentTransaction transaction, String tag)
Display the dialog, adding the fragment using an existing transaction and then committing the transaction.
'PROGRAMING > Android' 카테고리의 다른 글
interface를 이용하여 DialogFragment이벤트의 구현부를 다른 장소에 구현한다. (0) | 2013.03.03 |
---|---|
DialogFragment(AlertDialog) 안에 ListView 넣기 및 이벤트 받기 확인 (1) | 2013.03.03 |
Media File 삭제 , 단일 File MediaScanning 하기 (0) | 2013.02.24 |
Fragment에서 하단 OptionMenu 사용하는 방법 (0) | 2013.02.24 |
listView에서 checkbox 넣을 경우. 뷰 재사용 때문에 발생되는 문제 수정. (0) | 2013.02.24 |