频道栏目
IT货架 > > 正文
Android OptionMenu
网友分享于:Jan 1, 1970 8:00:00 AM    来源: IT货架   

标签: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   

广告服务联系QQ:1134687142 | 网站地图

版权所有: IT货架- 内容来自互联网,仅供用于技术学习,请遵循相关法律法规. 京ICP备11030978号-1