android ContextMenu 上下文菜单示例

ch2_contextmenu.xml:

1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <TextView android:id="@+id/tv" 8 android:layout_width="fill_parent" 9 android:layout_height="wrap_content" 10 android:text="这是一个上下文菜单ContextMenu的示例"/> 11 12 <EditText android:id="@+id/myEd" 13 android:layout_width="fill_parent" 14 android:layout_height="wrap_content"/> 15 16</LinearLayout>

ContextMenuActivity.java :

1package com.example.ch7; 2 3 4 5import com.example.baseexample.R; 6 7import android.app.Activity; 8import android.os.Bundle; 9import android.view.ContextMenu; 10import android.view.MenuItem; 11import android.view.View; 12import android.view.ContextMenu.ContextMenuInfo; 13import android.widget.EditText; 14import android.widget.TextView; 15 16public class ContextMenuActivity extends Activity { 17 18 private String tempStr; 19 private TextView tv; 20 private EditText myEd; 21 22 public void onCreate(Bundle savedInstanceState){ 23 super.onCreate(savedInstanceState); 24 setContentView(R.layout.ch7_contextmenu); 25 this.registerForContextMenu(findViewById(R.id.tv)); 26 this.registerForContextMenu(findViewById(R.id.myEd)); 27 } 28 29 public void onCreateContextMenu(ContextMenu menu,View v,ContextMenuInfo menuInfo){ 30 menu.setHeaderIcon(R.drawable.ic_launcher); 31 if(v==findViewById(R.id.tv)){ 32 menu.add(0,1,0,"复制"); 33 menu.add(0,2,0,"剪切"); 34 menu.add(0,3,0,"删除"); 35 } 36 if(v==findViewById(R.id.myEd)){ 37 menu.add(0,4,0,"粘贴"); 38 menu.add(0,5,0,"删除"); 39 } 40 } 41 42 public boolean onContextItemSelected(MenuItem item){ 43 tv = (TextView)findViewById(R.id.tv); 44 myEd = (EditText)findViewById(R.id.myEd); 45 switch(item.getItemId()){ 46 case 1: 47 tempStr = tv.getText().toString(); 48 break; 49 case 2: 50 tempStr = tv.getText().toString(); 51 tv.setText(""); 52 break; 53 case 3: 54 tv.setText(""); 55 break; 56 case 4: 57 myEd.setText(tempStr); 58 break; 59 case 5: 60 myEd.setText(""); 61 break; 62 } 63 return true; 64 } 65 66 67}
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

android Notification 状态栏通知使用示例

ch7\_notification.xml:<?xmlversion"1.0"encoding"utf8"?<LinearLayoutxmlns:android"http://schemas.android.com/apk/res/android"android:layout_width"match_

android WebView 使用实例

主布局文件:<?xmlversion"1.0"encoding"utf8"?<LinearLayoutxmlns:android"http://schemas.android.com/apk/res/android"android:layout_width"match_parent"

Android输入法遮挡了输入框,使用android:fitsSystemWindows="true"后界面顶部出现白条

问题1、页面布局文件:<LinearLayoutxmlns:android"http://schemas.android.com/apk/res/android"android:id"@id/layoutorderdetail"android:layoutwidth"matchparent"android:layoutheigh