建立上下文選單

never123450發表於2014-07-07

fragment_main.xml

<LinearLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.xwy.cai.MainActivity$PlaceholderFragment"
    android:orientation="vertical"
     >

    <TextView
        android:id="@+id/show"
        android:textSize="28px"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="開啟選單" />

</LinearLayout>

contentmenu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/color1" android:title="紅色"></item>
	<item android:id="@+id/color2" android:title="綠色"></item>
	<item android:id="@+id/color3" android:title="藍色"></item>
	<item android:id="@+id/color4" android:title="橙色"></item>
	<item android:id="@+id/color5" android:title="恢復預設"></item>

</menu>

MainActivity.java

package com.xwy.cai;

import android.support.v4.app.Fragment;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MainActivity extends Activity {

	private TextView tv;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_main);

		tv=(TextView)findViewById(R.id.show);
		registerForContextMenu(tv);		//為文字框註冊上下文選單
	}

	
	//建立上下文選單
		/************************************************************/
		public void onCreateContextMenu(ContextMenu menu, View v,
				ContextMenuInfo menuInfo) {
			MenuInflater inflator=new MenuInflater(this); 	//例項化一個MenuInflater物件
			inflator.inflate(R.menu.contentmenu, menu); 	//解析選單檔案
			menu.setHeaderIcon(R.drawable.ic_launcher);		//為選單頭設定圖示
			menu.setHeaderTitle("請選擇文字顏色:");			//為選單頭設定標題

		}
		@Override
		public boolean onContextItemSelected(MenuItem item) {
			switch(item.getItemId()){
				case R.id.color1:		//當選擇紅顏色時
					tv.setTextColor(Color.rgb(255, 0, 0));
					break;
				case R.id.color2:		//當選擇綠顏色時
					tv.setTextColor(Color.rgb(0, 255, 0));
					break;
				case R.id.color3:		//當選擇藍顏色時
					tv.setTextColor(Color.rgb(0, 0, 255));
					break;
				case R.id.color4:		//當選擇橙色時
					tv.setTextColor(Color.rgb(255, 180, 0));
					break;
				default:
					tv.setTextColor(Color.rgb(255, 255, 255));
			}
			return true;
		}
		
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_main, container,
					false);
			return rootView;
		}
	}

}


相關文章