package com.kgmyh.four.optionsmenu.text;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
public class IntroActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intro);
}
}
package com.kgmyh.four.optionsmenu.text;
import android.app.Activity;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.tv);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
//버튼이 눌리면 메뉴의 옵션을 미리 바꿔줌
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
ColorStateList color = tv.getTextColors();
int txtColor = color.getDefaultColor();//현재 글자색을 int로 리턴받음
//빨강파알에 대한 메뉴 Item을 받아옴
MenuItem blueMI = menu.findItem(R.id.color_blue);
MenuItem redMI = menu.findItem(R.id.color_red);
//빨강이나 파랑을 선택했을 때 선택된 체크박스에 체크가 남아있게함
if(txtColor==Color.RED){
redMI.setChecked(true);//red선택
blueMI.setChecked(false);//blue해제
}else if(txtColor==Color.BLUE){
redMI.setChecked(false);//red해제
blueMI.setChecked(true);//blue선택
}
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
String txt = (String)item.getTitle();
Toast.makeText(this, "메뉴 : "+txt+"가 선택됨", Toast.LENGTH_SHORT).show();
if(id==R.id.color_blue){
tv.setTextColor(Color.BLUE);
}else if(id==R.id.color_red){
tv.setTextColor(Color.RED);
}else if(id==R.id.menu_intro){
//IntroActivity를 실행
//Intent객체 생성 -> System에 무엇을 실행할지에 대한 정보를 알려주기 위한 객체
Intent intent = new Intent(this, IntroActivity.class);//component와 class객체
//System에 요청 - startActivity(intent)
startActivity(intent);
}
return true;
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:textSize="25dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:padding="@dimen/padding_medium"
android:text="@string/intro"
tools:context=".IntroActivity" />
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/tv"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="@dimen/padding_medium"
android:text="@string/hello_world"
tools:context=".MainActivity" />
</RelativeLayout>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_setting"
android:title="설정"
android:orderInCategory="100"
/>
<item android:id="@+id/menu_refresh"
android:title="새로고침"
android:orderInCategory="200"
/>
<!-- sub menu -->
<item android:id="@+id/menu_file"
android:title="File"
android:orderInCategory="300">
<!-- "file" submenu -->
<menu>
<item android:id="@+id/create_new"
android:title="새파일" />
<item android:id="@+id/open"
android:title="열기" />
<item android:id="@+id/save"
android:title="저장"/>
</menu>
</item>
<!-- 선택 메뉴 -->
<item android:id="@+id/choiceColor"
android:title="색선택"
android:orderInCategory="400">
<menu>
<item android:id="@+id/color_blue"
android:checkable="true"
android:title="파랑"/>
<item android:id="@+id/color_red"
android:checkable="true"
android:title="빨강"/>
</menu>
</item>
<item android:id="@+id/choiceColor2"
android:title="색선택2"
android:orderInCategory="500">
<menu>
<group android:checkableBehavior="single">
<item android:id="@+id/color_blue2"
android:title="파랑"/>
<item android:id="@+id/color_red2"
android:title="빨강"/>
</group>
</menu>
</item>
<item android:id="@+id/menu_intro"
android:title="소개"
android:orderInCategory="600"/>
</menu>
<resources>
<string name="app_name">OptionsMenuExam</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">OptionsMenu테스트</string>
<string name="title_activity_intro">App Introduction</string>
<string name="intro">Options 메뉴 테스트 app\n버젼 : 1.0</string>
</resources>
'프로그래밍 > Android' 카테고리의 다른 글
메뉴(OptionsMenu) - menu, item, group태그 (0) | 2012.07.31 |
---|---|
사용자지정 Adapter설정[실습] (0) | 2012.07.30 |
리스트뷰(어뎁터뷰, 리스트뷰, 스피너, 갤러리) (0) | 2012.07.30 |
ImageView (0) | 2012.07.27 |
TextView (0) | 2012.07.27 |