码迷,mamicode.com
首页 > 移动开发 > 详细

Android OptionMenu

时间:2014-08-03 17:47:15      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   color   java   os   io   

1、Java

package com.fish.helloworld;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {

    private Button m_Button;
    
    public static final int RED_MENU_ID = Menu.FIRST;
    public static final int GREEN_MENU_ID = Menu.FIRST + 1;
    public static final int BLUE_MENU_ID = Menu.FIRST + 2;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.skeleton_activity);
        
        m_Button = (Button)findViewById(R.id.button1);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        super.onCreateOptionsMenu(menu);
        menu.add(0, RED_MENU_ID, 0, "RED");
        menu.add(0, GREEN_MENU_ID, 0, "GREEN");
        menu.add(0, BLUE_MENU_ID, 0, "BLUE");
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch(item.getItemId()){
        case RED_MENU_ID:
            m_Button.setBackgroundColor(Color.RED);
            m_Button.setText("RED");
            return true;
        case GREEN_MENU_ID:
            m_Button.setBackgroundColor(Color.GREEN);
            m_Button.setText("GREEN");
            return true;
        case BLUE_MENU_ID:
            m_Button.setBackgroundColor(Color.BLUE);
            m_Button.setText("BLUE");
            return true;
        }
        
        return super.onOptionsItemSelected(item);
    }
}

 

2、XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

 

Android OptionMenu,布布扣,bubuko.com

Android OptionMenu

标签:android   style   blog   http   color   java   os   io   

原文地址:http://www.cnblogs.com/sshoub/p/3888524.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!