読者です 読者をやめる 読者になる 読者になる

liguofeng29’s blog

個人勉強用ブログだっす。

AndroidのUI - Menu

    

activity_main.xml
 
<?xml version="1.0" encoding="utf-8"?>
<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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<EditText
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
 
MainActivity.java
 
package com.example.liguofeng.menu;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
EditText edit;

// FONT SIZE ID
final int FONT_10 = 0x111;
final int FONT_12 = 0x112;
final int FONT_14 = 0x113;
final int FONT_16 = 0x114;
final int FONT_18 = 0x115;
// FONT COLOR ID
final int FONT_RED = 0x116;
final int FONT_BLUE = 0x117;
final int FONT_GREEN = 0x118;
// 選択可能メニュ
final int ITEM_CHECKABLE_01 = 0x119;
final int ITEM_CHECKABLE_02 = 0x120;
final int ITEM_CHECKABLE_03 = 0x121;
// 単一選択メニュ
final int ITEM_GROUP_01 = 0x122;
final int ITEM_GROUP_02 = 0x123;
final int ITEM_GROUP_03 = 0x124;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

edit = (EditText) findViewById(R.id.edit);
}

// Menu設定
@Override
public boolean onCreateOptionsMenu(Menu menu) {
SubMenu fontSizeMenu = menu.addSubMenu("Font Size");
// fontSizeMenu設定
fontSizeMenu.setHeaderTitle("select font size");
fontSizeMenu.add(0, FONT_10, 0, "size 10");
fontSizeMenu.add(0, FONT_12, 0, "size 12");
fontSizeMenu.add(0, FONT_14, 0, "size 14");
fontSizeMenu.add(0, FONT_16, 0, "size 16");
fontSizeMenu.add(0, FONT_18, 0, "size 18");
// color設定
SubMenu colorMenu = menu.addSubMenu("Color Size");
colorMenu.setHeaderTitle("select font color");
colorMenu.add(0, FONT_RED, 0, "RED");
colorMenu.add(0, FONT_BLUE, 0, "BLUE");
colorMenu.add(0, FONT_GREEN, 0, "GREEN");
// checkableのMenuItem設定
SubMenu checkableMenu = menu.addSubMenu("Checkable Menu Item");
checkableMenu.add(0, ITEM_CHECKABLE_01, 0, "選択可能01").setCheckable(true);
checkableMenu.add(0, ITEM_CHECKABLE_02, 0, "選択可能02").setCheckable(true);
checkableMenu.add(0, ITEM_CHECKABLE_03, 0, "選択可能03").setCheckable(true);
// GroupCheckableのMenuItem設定
SubMenu groupCheckable = menu.addSubMenu("single Menu Item");
groupCheckable.add(11, ITEM_GROUP_01, 0, "単一選択可能01").setChecked(true);
groupCheckable.add(11, ITEM_GROUP_02, 0, "単一選択可能02");
groupCheckable.add(11, ITEM_GROUP_03, 0, "単一選択可能03");
// group化
groupCheckable.setGroupCheckable(11, true, true);

return super.onCreateOptionsMenu(menu);
}

// MenuItemのCallbak
@Override
public boolean onOptionsItemSelected(MenuItem mi) {
switch (mi.getItemId()) {
case FONT_10 :
edit.setTextSize(10 * 2);
break;
case FONT_12 :
edit.setTextSize(12 * 2);
break;
case FONT_14 :
edit.setTextSize(14 * 2);
break;
case FONT_16 :
edit.setTextSize(16 * 2);
break;
case FONT_18 :
edit.setTextSize(18 * 2);
break;
case FONT_RED :
edit.setTextColor(Color.RED);
break;
case FONT_BLUE :
edit.setTextColor(Color.BLUE);
break;
case FONT_GREEN :
edit.setTextColor(Color.GREEN);
break;
case ITEM_CHECKABLE_01 :
mi.setChecked(!mi.isChecked());
Toast.makeText(this, "ITEM_CHECKABLE_01",Toast.LENGTH_SHORT).show();
break;
case ITEM_CHECKABLE_02 :
mi.setChecked(!mi.isChecked());
Toast.makeText(this, "ITEM_CHECKABLE_02",Toast.LENGTH_SHORT).show();
break;
case ITEM_CHECKABLE_03 :
mi.setChecked(!mi.isChecked());
Toast.makeText(this, "ITEM_CHECKABLE_03",Toast.LENGTH_SHORT).show();
break;
case ITEM_GROUP_01 :
mi.setChecked(!mi.isChecked());
Toast.makeText(this, "ITEM_GROUP_01",Toast.LENGTH_SHORT).show();
break;
case ITEM_GROUP_02 :
mi.setChecked(!mi.isChecked());
Toast.makeText(this, "ITEM_GROUP_02",Toast.LENGTH_SHORT).show();
break;
case ITEM_GROUP_03 :
mi.setChecked(!mi.isChecked());
Toast.makeText(this, "ITEM_GROUP_03",Toast.LENGTH_SHORT).show();
break;
}

return true;
}
}