Android開發長按菜單上下文菜單


安卓開發中長按彈出菜單的創建方法:

1.首先給View注冊上下文菜單registerForContextMenu();

2.添加上下文菜單內容onCreateContextMenu():

---可以通過代碼動態添加;

---可以加載xml文件中的菜單項;

3.設置菜單點擊后響應事件onContextItemSelected();

運行效果圖如下:

 

 

 

activity_main.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.example.androidtest.MainActivity" >

    <ListView 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/listview"
        ></ListView>
</RelativeLayout>

 

MainActivity.java代碼如下:

package com.example.androidtest;

import java.util.ArrayList;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.NotificationManager;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

@SuppressLint("NewApi")
public class MainActivity extends Activity implements OnClickListener{
    Button button1;
    Button button2;
    NotificationManager manager;
    int nid;
    String[] items={"唱歌","學習","跑步"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showListView();
    }
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,ContextMenuInfo menuInfo){
        super.onCreateContextMenu(menu,v,menuInfo);
        //設置Menu顯示內容
        menu.setHeaderTitle("文件操作");
        menu.setHeaderIcon(R.drawable.ic_launcher);
        menu.add(1,1,1,"復制");
        menu.add(1,2,1,"粘貼");
        menu.add(1,3,1,"剪切");
        menu.add(1,4,1,"重命名");
    }

    @Override
    public boolean onContextItemSelected(MenuItem item){
        switch(item.getItemId()){
        case 1:
            Toast.makeText(MainActivity.this,"點擊復制",Toast.LENGTH_SHORT).show();
            break;
        case 2:
            Toast.makeText(MainActivity.this,"點擊粘貼",Toast.LENGTH_SHORT).show();    
            break;
        case 3:
            Toast.makeText(MainActivity.this,"點擊剪切",Toast.LENGTH_SHORT).show();    
            break;
        case 4:
            Toast.makeText(MainActivity.this,"點擊重命名",Toast.LENGTH_SHORT).show();    
            break;
        }
    return super.onContextItemSelected(item);
    }
    
    private ArrayList<String> getData(){
        ArrayList<String> list = new ArrayList<String>();
        for(int i=0;i<5;i++){
        list.add("文件"+(i+1));
        }
        return list;
    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        
    }
 
         

 


}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM