package com.test.menu;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
public class MenuDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("메뉴키를 누르고 선택하세요");
setContentView(tv);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem item = menu.add(0,1,0,"바다");
item.setIcon(R.drawable.icon);
item.setAlphabeticShortcut('a');
menu.add(0,2,0,"하늘").setIcon(R.drawable.icon);
SubMenu sm = menu.addSubMenu("기타");
sm.add(0,3,0,"우주");
sm.add(0,4,0,"도시");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case 1:
Toast.makeText(this, "바다 선택", Toast.LENGTH_SHORT).show();
return true;
case 2:
Toast.makeText(this, "하늘 선택", Toast.LENGTH_SHORT).show();
return true;
case 3:
Toast.makeText(this, "우주 선택", Toast.LENGTH_SHORT).show();
return true;
case 4:
Toast.makeText(this, "도시 선택", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
}
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
public class MenuDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("메뉴키를 누르고 선택하세요");
setContentView(tv);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem item = menu.add(0,1,0,"바다");
item.setIcon(R.drawable.icon);
item.setAlphabeticShortcut('a');
menu.add(0,2,0,"하늘").setIcon(R.drawable.icon);
SubMenu sm = menu.addSubMenu("기타");
sm.add(0,3,0,"우주");
sm.add(0,4,0,"도시");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case 1:
Toast.makeText(this, "바다 선택", Toast.LENGTH_SHORT).show();
return true;
case 2:
Toast.makeText(this, "하늘 선택", Toast.LENGTH_SHORT).show();
return true;
case 3:
Toast.makeText(this, "우주 선택", Toast.LENGTH_SHORT).show();
return true;
case 4:
Toast.makeText(this, "도시 선택", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
}
res/menu/menu.xml 파일에 메뉴를 설정하고 MenuInflater 를 이용하여 전개하는 방법
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_ocean"
android:title="바다"
android:icon="@drawable/icon"
android:alphabeticShortcut="o"/>
<item android:id="@+id/menu_sky"
android:title="하늘"
android:icon="@drawable/icon"
android:alphabeticShortcut="s"/>
<item android:id="@+id/menu_etc"
android:title="기타"
android:icon="@drawable/icon"
android:alphabeticShortcut="e">
<menu>
<item android:id="@+id/menu_univ"
android:title="우주"/>
<item android:id="@+id/menu_city"
android:title="도시"/>
</menu>
</item>
</menu>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_ocean"
android:title="바다"
android:icon="@drawable/icon"
android:alphabeticShortcut="o"/>
<item android:id="@+id/menu_sky"
android:title="하늘"
android:icon="@drawable/icon"
android:alphabeticShortcut="s"/>
<item android:id="@+id/menu_etc"
android:title="기타"
android:icon="@drawable/icon"
android:alphabeticShortcut="e">
<menu>
<item android:id="@+id/menu_univ"
android:title="우주"/>
<item android:id="@+id/menu_city"
android:title="도시"/>
</menu>
</item>
</menu>
MenuDemoActivity.java
package com.test.menu;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
public class MenuDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("메뉴키를 누르고 선택하세요");
setContentView(tv);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
/*
MenuItem item = menu.add(0,1,0,"바다");
item.setIcon(R.drawable.icon);
item.setAlphabeticShortcut('a');
menu.add(0,2,0,"하늘").setIcon(R.drawable.icon);
SubMenu sm = menu.addSubMenu("기타");
sm.add(0,3,0,"우주");
sm.add(0,4,0,"도시");
*/
/*코드를 이용하여 메뉴를 설정하는 대신 res/menu/menu.xml 파일을 이용한다*/
MenuInflater inflater = this.getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.menu_ocean:
Toast.makeText(this, "바다 선택", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_sky:
Toast.makeText(this, "하늘 선택", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_univ:
Toast.makeText(this, "우주 선택", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_city:
Toast.makeText(this, "도시 선택", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
}
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
public class MenuDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("메뉴키를 누르고 선택하세요");
setContentView(tv);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
/*
MenuItem item = menu.add(0,1,0,"바다");
item.setIcon(R.drawable.icon);
item.setAlphabeticShortcut('a');
menu.add(0,2,0,"하늘").setIcon(R.drawable.icon);
SubMenu sm = menu.addSubMenu("기타");
sm.add(0,3,0,"우주");
sm.add(0,4,0,"도시");
*/
/*코드를 이용하여 메뉴를 설정하는 대신 res/menu/menu.xml 파일을 이용한다*/
MenuInflater inflater = this.getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.menu_ocean:
Toast.makeText(this, "바다 선택", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_sky:
Toast.makeText(this, "하늘 선택", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_univ:
Toast.makeText(this, "우주 선택", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_city:
Toast.makeText(this, "도시 선택", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
}
Option Menu의 서브메뉴에 체크(체크박스, 라이옵버튼)를 설정하는 예
옵션메뉴의 주 메뉴에는 체크를 설정할 수 없으나 주 메뉴의 서브메뉴에는 체크를 설정할 수 있다.
checkableBehavior 속성의 값은 "none", "single", "all" 등이 가능하며, none은 체크가 안되는 단독명령이고, single은 여러개의 항목 중에서 한개만 체크할 수 있다는 의미이고, all은 모두 체크가 가능한 상태를 의미한다
res/menu/menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_ocean"
android:title="바다"
android:icon="@drawable/icon"
android:alphabeticShortcut="o"/>
<item android:id="@+id/menu_sky"
android:title="하늘"
android:icon="@drawable/icon"
android:alphabeticShortcut="s"/>
<item android:id="@+id/menu_etc"
android:title="기타"
android:icon="@drawable/icon"
android:alphabeticShortcut="e">
<menu>
<group android:checkableBehavior="single">
<item android:id="@+id/menu_univ"
android:checked="true"
android:title="우주"/>
<item android:id="@+id/menu_city"
android:title="도시"/>
</group>
</menu>
</item>
</menu>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_ocean"
android:title="바다"
android:icon="@drawable/icon"
android:alphabeticShortcut="o"/>
<item android:id="@+id/menu_sky"
android:title="하늘"
android:icon="@drawable/icon"
android:alphabeticShortcut="s"/>
<item android:id="@+id/menu_etc"
android:title="기타"
android:icon="@drawable/icon"
android:alphabeticShortcut="e">
<menu>
<group android:checkableBehavior="single">
<item android:id="@+id/menu_univ"
android:checked="true"
android:title="우주"/>
<item android:id="@+id/menu_city"
android:title="도시"/>
</group>
</menu>
</item>
</menu>
메뉴 아이템의 체크상태를 확인하고 동적으로 변경하는 예
package com.test.menu;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
public class MenuDemoActivity extends Activity {
/** Called when the activity is first created. */
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tv = new TextView(this);
tv.setText("메뉴키를 누르고 선택하세요");
setContentView(tv);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
/*
MenuItem item = menu.add(0,1,0,"바다");
item.setIcon(R.drawable.icon);
item.setAlphabeticShortcut('a');
menu.add(0,2,0,"하늘").setIcon(R.drawable.icon);
SubMenu sm = menu.addSubMenu("기타");
sm.add(0,3,0,"우주");
sm.add(0,4,0,"도시");
*/
/*코드를 이용하여 메뉴를 설정하는 대신 res/menu/menu.xml 파일을 이용한다*/
MenuInflater inflater = this.getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem univ = menu.findItem(R.id.menu_univ);
MenuItem city = menu.findItem(R.id.menu_city);
if(univ.isChecked())tv.setText("우주가 체크됨");
else if(city.isChecked())tv.setText("도시가 선택됨");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.menu_ocean:
Toast.makeText(this, "바다 선택", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_sky:
Toast.makeText(this, "하늘 선택", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_univ:
item.setChecked(true);
Toast.makeText(this, "우주 선택", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_city:
item.setChecked(true);
Toast.makeText(this, "도시 선택", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.