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>