contextmenu的使用

昨天学习了popmenu和optionmenu的使用,今天学习的是contentmenu的使用,下面的代码是作者在昨天的基础上进行添加,但是为了方便读者们运行,作者会在最下方贴出全部的代码,大家可以免费下载,若不能下载,可以私信作者。

contentmenu的使用方法

  • Step 1:重写onCreateContextMenu()方法
  • Step 2:为view组件注册上下文菜单,使用registerForContextMenu()方法,参数是View
  • Step 3:重写onContextItemSelected()方法为菜单项指定事件监听器

编写contextmenu.xml

之前我们是在activity中之间创建的菜单,这里我们使用XML进行创建,原因在于,使用XML便于后期的维护和修改,不需要每次都重新分配ID

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="none">
    <item android:id="@+id/blue" android:title="蓝色"/>
        <item android:id="@+id/red" android:title="红色"/>
        <item android:id="@+id/green" android:title="绿色"/>
    </group>
</menu>

添加一个Textview

在acticity_miain.xml中添加一个textview

 <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/view2"
        android:layout_marginTop="100dp"
        android:text="使用contentmenu变色"
        />

编写activity的逻辑

这里贴出来的是所有的代码

package com.example.popmenu;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private Button menu1;
    final private int red = 1;
    final private int blue = 2;
    final private int green = 3;
    private TextView view1, view2;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        menu1 = findViewById(R.id.menu1);
        view1 = findViewById(R.id.view);
        view2 = findViewById(R.id.view2);
        registerForContextMenu(view2);


        menu1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PopupMenu popup = new PopupMenu(MainActivity.this, menu1);
                popup.getMenuInflater().inflate(R.menu.popmenu, popup.getMenu());
                popup.show();
            }

        });
    }

    public boolean onCreateOptionsMenu(Menu meun) {
        meun.add(1, red, 1, "红色");
        meun.add(1, blue, 2, "蓝色");
        meun.add(1, green, 3, "绿色");
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case red:
                view1.setTextColor(Color.RED);
                break;
            case blue:
                view1.setTextColor(Color.BLUE);
                break;
            case green:
                view1.setTextColor(Color.GREEN);
                break;

        }
        return super.onOptionsItemSelected(item);
    }

    //重写上下文菜单的创建方法
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        MenuInflater inflater = new MenuInflater(this);
        inflater.inflate(R.menu.contextmenu, menu);
        super.onCreateContextMenu(menu, v, menuInfo);
    }
    //上下文菜单被点击触发

    @Override
    public boolean onContextItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            //这里的ID是写在contentmenu.xml中的
            case R.id.blue:
                view2.setTextColor(Color.BLUE);
                Log.d("color","blue");
                break;
            case R.id.red:
                view2.setTextColor(Color.RED);
                Log.d("color","red");
                break;
            case R.id.green:
                view2.setTextColor(Color.GREEN);
                Log.d("color","green");
                break;
        }
        return true;
    }
}

运行效果

完整的代码链接

https://download.csdn.net/download/zijindanshutong/86743126

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值