2016-12-27 91 views
0

我想向android中的按鈕添加一個浮動上下文菜單。我怎樣才能創建一個監聽器 我怎樣才能使用該項目知道哪個按鈕調用菜單?在按鈕上添加一個浮動上下文菜單

@Override 
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { 
    super.onCreateContextMenu(menu, v, menuInfo); 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.menu_1, menu); 
} 

@Override 
public boolean onContextItemSelected(MenuItem item) { 
    if (item.getGroupId() == R.id.button2) 
     Toast.makeText(MainActivity.this, "1", Toast.LENGTH_SHORT).show(); 
    else 
    if (item.getGroupId() == R.id.button3) 
     Toast.makeText(MainActivity.this, "2", Toast.LENGTH_SHORT).show(); 
    return true; 
} 

回答

0

這可能有幫助。

activity_main.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"> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button 1" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button 2" /> 

</LinearLayout> 

ActivityMain.java

package com.shyra.contextmenu; 

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


public class MainActivity extends AppCompatActivity { 

    Button button1, button2; 

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

     button1 = (Button) findViewById(R.id.button1); 
     button2 = (Button) findViewById(R.id.button2); 

     registerForContextMenu(button1); 
     registerForContextMenu(button2); 
    } 

    @Override 
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { 
     super.onCreateContextMenu(menu, v, menuInfo); 

     if (v.getId() == R.id.button1) { 
      menu.setHeaderTitle("Context Menu"); 
      getMenuInflater().inflate(R.menu.menu_button1, menu); 
     } else if (v.getId() == R.id.button2) { 
      menu.add(v.getId(), 0, Menu.NONE, "Option 1"); 
      menu.add(v.getId(), 1, Menu.NONE, "Option 2"); 
      menu.add(v.getId(), 2, Menu.NONE, "Option 3"); 
     } 
    } 

    @Override 
    public boolean onContextItemSelected(MenuItem item) { 
     if (item.getTitle().equals("Action 1")) { 
      Toast.makeText(getApplicationContext(), "Action 1 pressed", Toast.LENGTH_SHORT).show(); 
     } else if (item.getTitle().equals("Action 2")) { 
      Toast.makeText(getApplicationContext(), "Action 2 pressed", Toast.LENGTH_SHORT).show(); 
     } else if (item.getTitle().equals("Action 3")) { 
      Toast.makeText(getApplicationContext(), "Action 3 pressed", Toast.LENGTH_SHORT).show(); 
     } else if (item.getGroupId() == R.id.button2) { 
      if (item.getItemId() == 0) { 
       Toast.makeText(getApplicationContext(), "Option 1 pressed", Toast.LENGTH_SHORT).show(); 
      } else if (item.getItemId() == 1) { 
       Toast.makeText(getApplicationContext(), "Option 2 pressed", Toast.LENGTH_SHORT).show(); 
      } else if (item.getItemId() == 2) { 
       Toast.makeText(getApplicationContext(), "Option 3 pressed", Toast.LENGTH_SHORT).show(); 
      } 
     } 
     return true; 
    } 
} 

menu_button1.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     xmlns:tools="http://schemas.android.com/tools" 
     tools:context="com.osahub.rachit.contextmenu.MainActivity"> 
    <item 
     android:id="@+id/action_1" 
     android:title="Action 1" 
     app:showAsAction="never"/> 
    <item 
     android:id="@+id/action_2" 
     android:title="Action 2" 
     app:showAsAction="never"/> 
    <item 
     android:id="@+id/action_3" 
     android:title="Action 3" 
     app:showAsAction="never"/> 
</menu> 
+0

但如果兩個按鈕可以調用菜單,然後我怎麼能知道哪個按鈕調用菜單(如果我想對該按鈕進行一些更改)? –

+0

看看我的編輯。上述上下文菜單用於2個按鈕。 ItemSelected句柄使用GroupID和ItemID。 – Rachit

+0

我已經在一個單獨的菜單文件中使用了創建的菜單,就像在我的編輯中一樣。 –