Android 之ContextMenu上下文菜单
Android 之ContextMenu上下文菜单
主程序如下:
- package com.cloay;
- import Android.app.Activity;
- import android.os.Bundle;
- import android.view.ContextMenu;
- import android.view.ContextMenu.ContextMenuInfo;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.TextView;
- import android.widget.Toast;
- /**
- *
- * MyContextMenuActivity.java
- * @author cloay
- * 2011-10-18
- */
- public class MyContextMenuActivity extends Activity {
- private TextView textView = null;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- textView = (TextView) findViewById(R.id.text);
- registerForContextMenu(textView); //<span style="font-family: arial; line-height: 18px;font-size:13px; ">为视图注册上下文菜单</span>
- }
- @Override
- public void onCreateContextMenu(ContextMenu menu, View v,
- ContextMenuInfo menuInfo) {
- menu.setHeaderIcon(android.R.drawable.ic_menu_share);
- menu.setHeaderTitle("Share to friends");
- menu.add(Menu.NONE, 0, 0, "发送");
- menu.add(Menu.NONE, 1, 1, "分享");
- super.onCreateContextMenu(menu, v, menuInfo);
- }
- @Override
- public boolean onContextItemSelected(MenuItem item) {
- switch(item.getItemId()){
- case 0:
- Toast.makeText(this, "发送给手机联系人!", Toast.LENGTH_SHORT).show();
- break;
- case 1:
- Toast.makeText(this, "分享到微博!", Toast.LENGTH_SHORT).show();
- break;
- }
- return super.onContextItemSelected(item);
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:paddingTop="10dip"
- >
- <TextView android:id="@+id/text"
- android:paddingTop="10dip"
- android:textColor="#FFA07A"
- android:background="@drawable/bg_normal"
- android:text="@string/hello"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
- </LinearLayout>
运行效果图如下:
评论暂时关闭