Android OptionMenu

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,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。