【Android筆記】ContextMenu上下文菜單

ContextMenu上下文菜單

1.ContextMenu菜單的組成部分

  • 標題及標題圖標
  • 菜單內容
  • 菜單內容的點擊響應事件

2.ContextMenu與OptionMenu的區別

  • OptionMenu對應的是activity,一個activity只能擁有一個選項菜單
  • ContextMenu對應的是View,每一個View均可以設置上下文菜單

3.ContextMenu的建立方法

  • 首先給View註冊上下文菜單registerForContextMenu()
  • 添加上下文菜單內容onCreateContextMenu()
  • 設置菜單點擊後響應事件onContextItemSelected()

實例一:動態添加

package com.example.demo16;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showListView();
    }

    /**
     * 設置ListView顯示內容
     */
    private void showListView(){
        ListView listView = (ListView) findViewById(R.id.listView);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                                            android.R.layout.simple_list_item_1, getData());
        listView.setAdapter(adapter);
        this.registerForContextMenu(listView);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        //設置Menu顯示內容
        menu.setHeaderTitle("文件操做");
        menu.setHeaderIcon(R.drawable.file);
        menu.add(1, 100, 1, "複製");
        menu.add(1, 101, 1, "剪切");
        menu.add(1, 102, 1, "粘貼");
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case 100:
                Toast.makeText(MainActivity.this, "點擊了複製", Toast.LENGTH_SHORT).show();
                break;
            case 101:
                Toast.makeText(MainActivity.this, "點擊了剪切", Toast.LENGTH_SHORT).show();
                break;
            case 102:
                Toast.makeText(MainActivity.this, "點擊了粘貼", Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onContextItemSelected(item);
    }


    /**
     * 構造ListView顯示的數據
     * @return
     */
    private ArrayList<String> getData(){
        ArrayList<String> list = new ArrayList<String>();
        for(int i=0; i<5; ++i) {
            list.add("文件" + i);
        }
        return list;
    }
}

運行效果:java

實例二:使用xml添加

1.在資源目錄res下建立menu資源文件夾

2.在menu文件夾下建立main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/action_item1"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="複製"/>

    <item
        android:id="@+id/action_item2"
        android:orderInCategory="101"
        android:showAsAction="never"
        android:title="剪切"/>

    <item
        android:id="@+id/action_item3"
        android:orderInCategory="102"
        android:showAsAction="never"
        android:title="粘貼"/>
</menu>

3.建立菜單

package com.example.demo16;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showListView();
    }

    /**
     * 設置ListView顯示內容
     */
    private void showListView(){
        ListView listView = (ListView) findViewById(R.id.listView);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                                            android.R.layout.simple_list_item_1, getData());
        listView.setAdapter(adapter);
        //註冊上下文菜單
        this.registerForContextMenu(listView);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        //設置Menu顯示內容
        menu.setHeaderTitle("文件操做");
        menu.setHeaderIcon(R.drawable.file);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
    }


    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_item1:
                Toast.makeText(MainActivity.this, "點擊了複製", Toast.LENGTH_SHORT).show();
                break;
            case R.id.action_item2:
                Toast.makeText(MainActivity.this, "點擊了剪切", Toast.LENGTH_SHORT).show();
                break;
            case R.id.action_item3:
                Toast.makeText(MainActivity.this, "點擊了粘貼", Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onContextItemSelected(item);
    }


    /**
     * 構造ListView顯示的數據
     * @return
     */
    private ArrayList<String> getData(){
        ArrayList<String> list = new ArrayList<String>();
        for(int i=0; i<5; ++i) {
            list.add("文件" + i);
        }
        return list;
    }
}

效果如上例android

相關文章
相關標籤/搜索