작업환경 :
minSdkVersion 8 (Android 2.2 / Froyo)
targetSdkVersion 16 (Android 4.1.2 / IceCream
타겟을 허니컴(3.0) 이상으로 작업 할 때,
이전 버전(.../ 2.2 / 2.3 / 2.3.3 ... )에서 사용하던 기능들 구현방법이 다소 바꼈다.
크게 TabWidget 은 ActionBar 로,
Dialog 는 Fragment 를 이용하여 구현해야 한댄다.
아래 소스는
Dialog 창 띄우는 기능과
AlertDialog (확인 버튼 / 취소 버튼) 기능을 넣은 소스이다.
참고로, 3.0 이하에서도 구현되도록 작성됐음!
// 파일 총 4 개
// main.xml / TestDialogFragmentActivity.java / fragment_dialog.xml 파일 / string.xml 파일
/* 이하 소스 1 _ layout 폴더 > main.xml 파일 */
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:padding="4dip"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0000"
tools:context=".TestDialogFragmentActivity" >
<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" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="0"
>
<Button
android:id="@+id/show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/show">
<requestFocus />
</Button>
<Button
android:id="@+id/show_alert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/show_alert">
</Button>
</LinearLayout>
</LinearLayout>
/*
* 이하 소스 2 _ values 폴더 > string.xml 파일
*/
/*
* 이하 소스 3 _ layout 폴더 > fragment_dialog.xml 파일
*/
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:padding="4dip"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#4000"
tools:context=".TestDialogFragmentActivity" >
<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 _ src 폴더 > TestDialogFragmentActivity.java 파일
*/
package com.example.test_fregmentdialog;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class TestDialogFragmentActivity extends FragmentActivity {
int mStackLevel = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View tv = findViewById(R.id.text);
((TextView)tv).setText("다이얼로그 프레그먼트 예제. " + "\n" + "\n"
+ "첫번째 다이얼로그 : '보여주기'버튼 클릭." + "\n"
+ "'보여주기'버튼을 계속 누를 경우에 다른 다이얼로그 스타일들이"
+ "계속 보여주고 스택에 쌓여지고 '뒤로'버튼을 누르면 이전 다이얼로그가 보여주게됩니다." + "\n"+ "\n"
+ "두번째 Alert Dialog를 보려면 'Alert 보여주기'버튼을 누르세요." + "\n"
);
Button button_1 = (Button)findViewById(R.id.show);
button_1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog();
}
});
Button button_2 = (Button)findViewById(R.id.show_alert);
button_2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showAlertDialog();
}
});
if(savedInstanceState != null) {
mStackLevel = savedInstanceState.getInt("level");
}
}
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("level", mStackLevel);
}
void showDialog() {
mStackLevel++;
// FragmentTransaction ft = getFragmentManager().beginTransaction();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
// Fragment prev = getFragmentManager().findFragment("string");
// Fragment prev = getFragmentManager().findFragmentByTag("dialog");
Fragment prev = getSupportFragmentManager().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");
}
// AlertDialog()
void showAlertDialog() {
DialogFragment newFragment = MyAlertDialogFragment.newInstance(R.string.alert_dialog_two_buttons_title);
newFragment.show(getSupportFragmentManager(), "dialog");
}
// AlertDialog()
public void doPositiveClick() {
Log.i("FragmentAlertDialog", "PositiveClick");
}
// AlertDialog()
public void doNegativeClick() {
Log.i("FragmentAlertDialog", "NegativeClick");
}
static String getNameForNum(int num) {
switch ((num-1)%6) {
case 1: return "STYLE_NO_TITLE";
case 2: return "STYLE_NO_FRAME";
case 3: return "STYLE_NO_INPUT (this window can't receive input, so "
+ "you will need to press the bottom show button)";
case 4: return "STYLE_NORMAL with dark fullscreen theme";
case 5: return "STYLE_NORMAL with light theme";
case 6: return "STYLE_NO_TITLE with light theme";
case 7: return "STYLE_NO_FRAME with light theme";
case 8: return "STYLE_NORMAL with light fullscreen theme";
}
return "STYLE_NORMAL";
}
public static class MyDialogFragment extends DialogFragment {
int mNum;
static MyDialogFragment newInstance(int num) {
MyDialogFragment f = new MyDialogFragment();
Bundle args = new Bundle();
args.putInt("num", num);
f.setArguments(args);
return f;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNum = getArguments().getInt("num");
int style = DialogFragment.STYLE_NORMAL, theme = 0;
switch ((mNum-1)%6) {
case 1: style = DialogFragment.STYLE_NO_TITLE; break;
case 2: style = DialogFragment.STYLE_NO_FRAME; break;
case 3: style = DialogFragment.STYLE_NO_INPUT; break;
case 4: style = DialogFragment.STYLE_NORMAL; break;
case 5: style = DialogFragment.STYLE_NORMAL; break;
case 6: style = DialogFragment.STYLE_NO_TITLE; break;
case 7: style = DialogFragment.STYLE_NO_FRAME; break;
case 8: style = DialogFragment.STYLE_NO_INPUT; break;
}
switch ((mNum-1)%6) {
case 4: theme = android.R.style.Theme_Holo; break;
case 5: theme = android.R.style.Theme_Holo_Light_Dialog; break;
case 6: theme = android.R.style.Theme_Holo_Light; break;
case 7: theme = android.R.style.Theme_Holo_Light_Panel; break;
case 8: theme = android.R.style.Theme_Holo_Light; break;
}
setStyle(style, theme);
}
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);
((TextView)tv).setText("Dialog #" + mNum + ": using style " + getNameForNum(mNum));
Button button_1 = (Button)v.findViewById(R.id.show);
button_1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// ((FragmentDialog)getActivity()).showDialog();
((TestDialogFragmentActivity)getActivity()).showDialog();
}
});
return v;
}
}
// AlertDialog()
public static class MyAlertDialogFragment extends DialogFragment {
public static MyAlertDialogFragment newInstance(int title) {
MyAlertDialogFragment frag = new MyAlertDialogFragment();
Bundle args = new Bundle();
args.putInt("title", title);
frag.setArguments(args);
return frag;
}
@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) {
((TestDialogFragmentActivity)getActivity()).doPositiveClick();
}
}
)
.setNegativeButton(R.string.alert_dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
((TestDialogFragmentActivity)getActivity()).doNegativeClick();
}
}
)
.create();
}
}
}
참고 사이트 (대중 마구마구 넣어놨어요. 참고^^)
[번역] Using DialogFragments
http://someboyj.pe.kr/blog/2012/06/12/%EB%B2%88%EC%97%AD-using-dialogfragments/
Callback to a Fragment from a DialogFragment
http://stackoverflow.com/questions/13733304/callback-to-a-fragment-from-a-dialogfragment
Using DialogFragments
http://android-developers.blogspot.kr/2012/05/using-dialogfragments.html
Android Developers / Fragment / DialogFragment / Dialogs
http://developer.android.com/reference/android/app/DialogFragment.html
http://developer.android.com/guide/components/fragments.html
http://developer.android.com/guide/topics/ui/dialogs.html
com.example.android.supportv4.app.FragmentDialogSupport
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android-apps/4.0.3_r1/com/example/android/supportv4/app/FragmentDialogSupport.java
'안드로이드' 카테고리의 다른 글
[Android][Error]our project contains error(s), please fix them before running your application (0) | 2012.12.14 |
---|---|
[Android][Error] ClassCastException (0) | 2012.12.14 |
[Android][Layout]requestFocus (0) | 2012.12.13 |
[Android][Error]error: Error parsing XML : unbound prefix (0) | 2012.12.06 |
[Android][Emulator]AVD Failed to allocate memory (0) | 2012.12.06 |