Main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/submenu"
android:title="Sub Menu">
<menu>
<group
android:id="@+id/checkable_group"
android:checkableBehavior="all" >
<item
android:id="@+id/bigfont"
android:title="Big Font"/>
</group>
<group
android:id="@+id/checkable_group2"
android:checkableBehavior="single" >
<item
android:id="@+id/red"
android:title="Red"
android:checked="true"/>
<item
android:id="@+id/green"
android:title="Green"/>
<item
android:id="@+id/blue"
android:title="Blue"/>
</group>
</menu>
</item>
</menu>
package com.example.optionmenuex02;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends Activity {
boolean bigfont;
String color ="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
if (bigfont) {
menu.findItem(R.id.bigfont).setChecked(true);
} else {
menu.findItem(R.id.bigfont).setChecked(false);
}
if(color.equals("red")) menu.findItem(R.id.red).setChecked(true);
if(color.equals("green")) menu.findItem(R.id.green).setChecked(true);
if(color.equals("blue")) menu.findItem(R.id.blue).setChecked(true);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.bigfont:
Toast.makeText(this, "bigfont", Toast.LENGTH_SHORT).show();
if (bigfont) {
bigfont = false;
} else {
bigfont = true;
}
return true;
case R.id.red:
Toast.makeText(this, "red", Toast.LENGTH_SHORT).show();
color = "red";
return true;
case R.id.green:
Toast.makeText(this, "green", Toast.LENGTH_SHORT).show();
color = "green";
return true;
case R.id.blue:
Toast.makeText(this, "blue", Toast.LENGTH_SHORT).show();
color = "blue";
return true;
}
return false;
}
}
'Android' 카테고리의 다른 글
리스트뷰 - 어댑터뷰 (0) | 2013.03.29 |
---|---|
메뉴 - context (0) | 2013.03.29 |
메뉴(menu) - 옵션 메뉴 (0) | 2013.03.29 |
셰이더 - ShapeDrawable (0) | 2013.03.29 |
셰이더 - 스윕 그래디언트 SweepGradient (0) | 2013.03.29 |