android ContextMenu 上下文菜单示例

Wesley13
• 阅读 1014

ch2_contextmenu.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" >
    
    <TextView android:id="@+id/tv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="这是一个上下文菜单ContextMenu的示例"/>
    
    <EditText android:id="@+id/myEd"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

ContextMenuActivity.java :

package com.example.ch7;



import com.example.baseexample.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.EditText;
import android.widget.TextView;

public class ContextMenuActivity extends Activity {

    private String tempStr;
    private TextView tv;
    private EditText myEd;
    
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ch7_contextmenu);
        this.registerForContextMenu(findViewById(R.id.tv));
        this.registerForContextMenu(findViewById(R.id.myEd));
    }
    
    public void onCreateContextMenu(ContextMenu menu,View v,ContextMenuInfo menuInfo){
        menu.setHeaderIcon(R.drawable.ic_launcher);
        if(v==findViewById(R.id.tv)){
            menu.add(0,1,0,"复制");
            menu.add(0,2,0,"剪切");
            menu.add(0,3,0,"删除");
        }
        if(v==findViewById(R.id.myEd)){
            menu.add(0,4,0,"粘贴");
            menu.add(0,5,0,"删除");
        }
    }
    
    public boolean onContextItemSelected(MenuItem item){
        tv = (TextView)findViewById(R.id.tv);
        myEd = (EditText)findViewById(R.id.myEd);
        switch(item.getItemId()){
        case 1:
            tempStr = tv.getText().toString();
            break;
        case 2:
            tempStr = tv.getText().toString();
            tv.setText("");
            break;
        case 3:
            tv.setText("");
            break;
        case 4:
            myEd.setText(tempStr);
            break;
        case 5:
            myEd.setText("");
            break;
        }
        return true;
    }
    
    
}
点赞
收藏
评论区
推荐文章
blmius blmius
2年前
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
东方客主 东方客主
3年前
Android输入法遮挡了输入框,使用android:fitsSystemWindows="true"后界面顶部出现白条
问题1、页面布局文件:<LinearLayoutxmlns:android"http://schemas.android.com/apk/res/android"android:id"@id/layoutorderdetail"android:layoutwidth"matchparent"android:layoutheigh
Wesley13 Wesley13
2年前
android Notification 状态栏通知使用示例
ch7\_notification.xml:<?xmlversion"1.0"encoding"utf8"?<LinearLayoutxmlns:android"http://schemas.android.com/apk/res/android"android:layout_width"match_
Wesley13 Wesley13
2年前
android WebView 使用实例
主布局文件:<?xmlversion"1.0"encoding"utf8"?<LinearLayoutxmlns:android"http://schemas.android.com/apk/res/android"android:layout_width"match_parent"
Stella981 Stella981
2年前
Android 复选框 以及回显
activity\_main.xml<?xmlversion"1.0"encoding"utf8"?<LinearLayoutxmlns:android"http://schemas.android.com/apk/res/android"xmlns:app"http:/
Stella981 Stella981
2年前
Android控件ListView简易使用(使用ArrayAdapter)
<?xmlversion"1.0"encoding"utf8"?<TextViewxmlns:android"http://schemas.android.com/apk/res/android"android:id"@id/tv"android:la
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Android选项卡TabHost功能和用法
1、布局文件<TabHostxmlns:android"http://schemas.android.com/apk/res/android"xmlns:tools"http://schemas.android.com/tools"android:id"@android:id/tabhost"
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这