`
shenjichao2009
  • 浏览: 94404 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Android中ContextMenu的使用

阅读更多

package com.baosight.menu;

 

import android.app.Activity;

import android.graphics.Color;

import android.os.Bundle;

import android.view.ContextMenu;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.view.ContextMenu.ContextMenuInfo;

import android.widget.TextView;

 

/**

 * 上下文菜单

 * 

 * @author shyboy(897948924@qq.com)

 * 

 */

public class ContextMenuActivity extends Activity {

 

/**

* 菜单项ID常量

*/

private static final int ITEM1 = Menu.FIRST;

private static final int ITEM2 = Menu.FIRST + 1;

private static final int ITEM3 = Menu.FIRST + 2;

 

private TextView contextTextView;// 声明视图控件

 

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

 

this.setContentView(R.layout.context);

 

contextTextView = (TextView) findViewById(R.id.contextMenu);

 

this.registerForContextMenu(contextTextView);// 为视图控件注册上下文菜单

}

 

/**

* 覆盖该方法添加上下文菜单项

*/

@Override

public void onCreateContextMenu(ContextMenu menu, View v,

ContextMenuInfo menuInfo) {

 

menu.add(0, ITEM1, 0, "红色背景");

menu.add(0, ITEM2, 0, "绿色背景");

menu.add(0, ITEM3, 0, "黄色背景");

 

}

 

/**

* 覆盖该方法,响应菜单选项被单击事件

*/

@Override

public boolean onContextItemSelected(MenuItem item) {

 

switch (item.getItemId()) {

/**

* 当“红色背景”菜单被选择时

*/

case ITEM1:

contextTextView.setBackgroundColor(Color.RED);// 设置背景颜色为红色

break;

 

/**

* 当“绿色背景”菜单被选择时

*/

case ITEM2:

contextTextView.setBackgroundColor(Color.GREEN);// 设置背景颜色为绿色

break;

 

/**

* 当“黄色背景”菜单被选择时

*/

case ITEM3:

contextTextView.setBackgroundColor(Color.YELLOW);// 设置背景颜色为黄色

 

}

 

return true;

 

}

 

}


  • 大小: 14.8 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics