CHAP 7 . 메뉴와 대화상자
description
Transcript of CHAP 7 . 메뉴와 대화상자
CHAP 7. 메뉴와 대화상자
© 2012 생능출판사 All rights reserved
메뉴의 종류 옵션 메뉴 : 사용자가 MENU 키를 누를 때
나타난다 .
컨텍스트 메뉴 : 컨텍스트 메뉴는 사용자가 화면을 일정 시간 이상으로 길게 누르면 나타나는 메뉴이다 .
© 2012 생능출판사 All rights reserved
옵션 메뉴 현재 액티비티와 관련된 동작
© 2012 생능출판사 All rights reserved
컨텍스트 메뉴 UI 의 어떤 항목과 관련된 동작
© 2012 생능출판사 All rights reserved
팝업 메뉴 뷰에 부착된 모달 메뉴 (modal menu) API 레벨 11 부터 제공 팝업 메뉴의 용도
오버플로우 스타일 메뉴 제공 서브 메뉴의 역할 드롭다운 메뉴
© 2012 생능출판사 All rights reserved
메뉴도 XML 로 정의
res/menu.xml
<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/new_game" android:icon="@drawable/icon" android:title=" 새로운 게임 " />
<item android:id="@+id/quit" android:icon="@drawable/icon" android:title=" 취소 " />
</menu>
리소스 식별자
아이콘 이미지
메뉴 타이틀
© 2012 생능출판사 All rights reserved
메뉴 팽창 메뉴 리소스를 팽창 (inflate) 하면 실제 메뉴가
생성
© 2012 생능출판사 All rights reserved
메뉴 리소스 팽창
@Overridepublic boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();inflater.inflate(R.menu.menu, menu);return true;
}
© 2012 생능출판사 All rights reserved
옵션 메뉴 옵션 메뉴는 액티비티의 옵션을 설정하는 메뉴 버전 3.0 이상에서는 MENU 버튼과 액션 바의 2
가지 방법으로 옵션 메뉴를 사용할 수 있다 .
© 2012 생능출판사 All rights reserved
옵션 메뉴 생성 사용자가 옵션 키를 누르면 다음의 메소드가
호출된다 .
@Overridepublic boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();inflater.inflate(R.menu.menu, menu);return true;
}
© 2012 생능출판사 All rights reserved
옵션 메뉴 이벤트 처리@Overridepublic boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {case R.id.new_game:
Toast.makeText(this, " 새로운 게임 선택 ", Toast.LENGTH_SHORT).show();
return true;case R.id.quit:
Toast.makeText(this, " 취소 선택 ",
Toast.LENGTH_SHORT).show();return true;
default:return super.onOptionsItemSelected(item);
}}
© 2012 생능출판사 All rights reserved
컨텍스트 메뉴 사용자가 항목 위에서 “오래 누르기” (long-press)
를 하면 컨텍스트 메뉴가 표시 PC 에서 마우스 오른쪽 버튼을 눌렀을 때 나오는
메뉴와 개념적으로 유사
© 2012 생능출판사 All rights reserved
컨텍스트 메뉴 예제 사용자 인터페이스 작성
<?xml version="1.0" encoding="UTF-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/an-droid" android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Only I can change my life. No one can do it for me." android:textSize="50px" android:typeface="serif" /></LinearLayout>
© 2012 생능출판사 All rights reserved
컨텍스트 메뉴 예제
...public class ContextMenuActivity extends Activity { TextView text; @Override
public void onCreate(BundlesavedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); text = (TextView) findViewById(R.id.TextView01);
registerForContextMenu(text); }
© 2012 생능출판사 All rights reserved
컨텍스트 메뉴 컨텍스트 메뉴는 다음과 같은 메소드가 호출되면서
생성된다 .
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);menu.setHeaderTitle(" 컨텍스트 메뉴 ");menu.add(0, 1, 0, " 배경색 : RED");menu.add(0, 2, 0, " 배경색 : GREEN");menu.add(0, 3, 0, " 배경색 : BLUE");
}
© 2012 생능출판사 All rights reserved
실행 결과
© 2012 생능출판사 All rights reserved
컨텍스트 메뉴 이벤트 처리
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1:
text.setBackgroundColor(Color.RED);return true;
case 2:
text.setBackgroundColor(Color.GREEN);return true;
case 3:
text.setBackgroundColor(Color.BLUE);return true;
default:return
super.onContextItemSelected(item);}
}
© 2012 생능출판사 All rights reserved
서브 메뉴 메뉴 안의 메뉴
<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/an-droid" > <item android:id="@+id/file" android:icon="@drawable/file" android:title="file"> <menu> <item android:id="@+id/create_new" android:title="new"/> <item android:id="@+id/open" android:title="open"/> </menu> </item></menu>
© 2012 생능출판사 All rights reserved
코드로 서브 메뉴 생성 기존의 메뉴에 동적으로 서브 메뉴 추가
@Overridepublic boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);SubMenu sub =
menu.addSubMenu("file");sub.add(0, 1, 0, "new");sub.add(0, 2, 0, "open");return true;
}
© 2012 생능출판사 All rights reserved
대화 상자 AlertDialog
ProgressDialog
DatePickerDialog TimePickerDialog
© 2012 생능출판사 All rights reserved
대화 상자 생성 , 표시 , 제거 메카니즘public class DialogTestActivity extends Activity {
...
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_PAUSED_ID:
return new
AlertDialog.Builder(AlertDialogTest.this).create();
...
}
return null;
}
...
showDialog(DIALOG_PAUSED_ID); ...
dismissDialog(DIALOG_PAUSED_ID);
...}
© 2012 생능출판사 All rights reserved
ALERTDIALOG
텍스트 메시지
버튼 ( 또는 체크박스 목록 )
제목
© 2012 생능출판사 All rights reserved
ALERTDIALOGAlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setTitle(" 종료 확인 대화 상자 ")
.setMessage(" 애플리케이션을 종료하시겠습니까 ?")
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog,
int whichButton) {AlertDialog2Activity.this.finish();
}}).setNegativeButton("No",
new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog,
int whichButton) {dialog.cancel();
}});AlertDialog alert = builder.create();return alert;
© 2012 생능출판사 All rights reserved
목록을 사용하는 대화상자public class AlertDialogTest03 extends Activity {
protected Dialog onCreateDialog(int id) {switch (id) {case DIALOG_YES_NO_MESSAGE:
final CharSequence[] items ={ "Red", "Green", "Blue" };
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(" 색상을 선택하시오 ");builder.setItems(items, new
DialogInterface.OnClickListener(){ public void onClick(DialogInterface
dialog, int item) { Toast.makeText(getApplicationContext(),
items[item], Toast.LENGTH_SHORT).show();}}
);AlertDialog alert = builder.create();return alert;
}return null;
}...
© 2012 생능출판사 All rights reserved
체크박스를 사용하는 대화상자public class AlertDialogTest03 extends Activity {
protected Dialog onCreateDialog(int id) {switch (id) {case DIALOG_YES_NO_MESSAGE:
final CharSequence[] items ={ "Red", "Green", "Blue" };AlertDialog.Builder builder = new AlertDialog.Builder(this)builder.setTitle(" 색상을 선택하시오 ")builder.setSingleChoiceItems(items, -1, new
DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int
item) { Toast.makeText(getApplicationContext(),
items[item], Toast.LENGTH_SHORT).show();
}});AlertDialog alert = builder.create();return alert;
}return null;
}...
© 2012 생능출판사 All rights reserved
PROGRESSDIALOG
ProgressDialog dialog = ProgressDialog.show(MyActivity.this, "", "Loading. Please wait...", true, true);
© 2012 생능출판사 All rights reserved
PROGRESSDIALOG...public class AlertDialog4Activity extends Activity {
@Overrideprotected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);setContentView(R.layout.main);Button b = (Button) findViewById(R.id.Button01);b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {ProgressDialog dialog =
ProgressDialog.show(this, "", " 로딩 중입니다 .
기다려주세요 .", true, true);// 작업을 한다 . //dialog.dismiss(); 작업이 끝나면
dismiss() 를 호출}
});}
}
© 2012 생능출판사 All rights reserved
프로그레스바
© 2012 생능출판사 All rights reserved
PROGRESSBAR...public class AlertDialog5Activity extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button b = (Button) findViewById(R.id.Button01); b.setOnClickListener(new OnClickListener() { public void onClick(View v){ ProgressDialog progressDialog; progressDialog= new ProgressDialog(AlertDialog5Activity.this); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.setMessage("Loading..."); progressDialog.show(); progressDialog.setProgress(30);
} }); }}
© 2012 생능출판사 All rights reserved
커스텀 대화 상자 사용자가 마음대로 대화 상자의 내용을 디자인할 수
있는 대화 상자
© 2012 생능출판사 All rights reserved
XML 로 대화 상자의 내용을 선언<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout_root" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:padding="10dp" > <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_marginRight="10dp" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="fill_parent" android:textColor="#FFF" /></LinearLayout>
© 2012 생능출판사 All rights reserved
커스텀 대화상자 예제...public class CustomDialogActivity extends Activity
static final int DIALOG_CUSTOM_ID = 0;@Overridepublic void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);setContentView(R.layout.main);Button b = (Button) findViewById(R.id.button1);b.setOnClickListener(new OnClickListener()
public void onClick(View v) showDialog(DIALOG_CUSTOM_ID);
);
© 2012 생능출판사 All rights reserved
@Overrideprotected Dialog onCreateDialog(int id) {
Dialog dialog = null;switch (id) {case DIALOG_CUSTOM_ID:
dialog = new Dialog(this);
dialog.setContentView(R.layout.custom_dialog);dialog.setTitle("Custom Dialog");TextView text = (TextView)
dialog.findViewById(R.id.text);text.setText("Hello, this is a custom
dialog!");ImageView image = (ImageView)
dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.android);break;
}return dialog;
}}
© 2012 생능출판사 All rights reserved
실행결과