zoukankan      html  css  js  c++  java
  • 11 ContextMenu 上下文菜单按钮

    ContextMenu 上下文菜单

    1. 在res下的menu里写菜单项
    2. 在逻辑代码中 写OnCreateContextMenu() 方法 将菜单项添加到菜单
    3. 对菜单项进行监听 onContextItemSelected()
      • //获取ListView 当前item的下标
      • AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
      • 4,note:registerForContextMenu(tv);进行注册上下文菜单给控件

    图标不显示参考不显示菜单按钮解决方法


    • 案例1 :给普通TextView设置
    • xml代码:
    <RelativeLayout 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.fmy.context.MainActivity" >
    
        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />
    
    </RelativeLayout>
    
    • java代码:
    package com.fmy.context;
    
    import android.app.Activity;
    import android.graphics.Color;
    import android.os.Bundle;
    import android.view.ContextMenu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ContextMenu.ContextMenuInfo;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
    
        private TextView tv;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            tv = (TextView) findViewById(R.id.tv);
            registerForContextMenu(tv);
        }
    
        @Override
        public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
            super.onCreateContextMenu(menu, v, menuInfo);
            getMenuInflater().inflate(R.menu.main, menu);
        }
    
        @Override
        public boolean onContextItemSelected(MenuItem item) {
            switch (item.getItemId()) {
            case R.id.green:
                tv.setTextColor(Color.GREEN);
                break;
            case R.id.red:
                tv.setTextColor(Color.RED);
                break;
    
            default:
                break;
            }
            return super.onContextItemSelected(item);
        }
    }
    
    • 效果:
    • 长按TextView 弹出菜单
      这里写图片描述

    • 案例二: listview和按钮
      xml代码:
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        tools:context="com.fmy.context.MainActivity" >
    
           <item
            android:id="@+id/action_delete"
            android:orderInCategory="100"
            android:showAsAction="never"
            android:title="删除信息"/>
    
    
    </menu>
    <RelativeLayout 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.fmy.context.MainActivity" >
    
        <ListView
            android:id="@+id/lv"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </ListView>
    
    </RelativeLayout>

    java代码:

    package com.fmy.context;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.ContextMenu;
    import android.view.ContextMenu.ContextMenuInfo;
    import android.view.LayoutInflater;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.AdapterView.AdapterContextMenuInfo;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
    
        private TextView tv;
        private ListView lv;
        private int pos;
        private List<String> list;
        private ArrayAdapter<String> adapter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            lv = (ListView) findViewById(R.id.lv);
            list = new ArrayList<String>();
            for (int i = 0; i < 50; i++) {
                list.add("" + i);
            }
            adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
            lv.setAdapter(adapter);
            registerForContextMenu(lv);
        }
    
        @Override
        public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
            // TODO Auto-generated method stub
            super.onCreateContextMenu(menu, v, menuInfo);
            getMenuInflater().inflate(R.menu.main, menu);
            AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
            pos = info.position;
        }
    
        @Override
        public boolean onContextItemSelected(MenuItem item) {
    
            list.remove(pos);
    
            adapter.notifyDataSetChanged();
    
            return super.onContextItemSelected(item);
        }
    }
    

    效果图:

    listview和菜单按钮

  • 相关阅读:
    @atcoder
    @loj
    @AGC037
    @uoj
    @uoj
    @loj
    @loj
    @loj
    @loj
    @loj
  • 原文地址:https://www.cnblogs.com/muyuge/p/6152271.html
Copyright © 2011-2022 走看看