res/menu/contextmenu.xml
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/menu_delete" android:title="Delete Item"> </item> <item android:id="@+id/menu_copy" android:title="Copy Item"> </item> <item android:id="@+id/menu_edit" android:title="Edit Item"> </item> </menu>
Activity.java
package com.sqlendia.android; import android.app.Activity; import android.os.Bundle; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; public class Android28Activity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Button button = new Button(this); button.setText("Click for Options"); button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { openContextMenu(v); } }); registerForContextMenu(button); setContentView(button); } @Override public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu_delete: return true; case R.id.menu_copy: return true; case R.id.menu_edit: return true; default: break; } return super.onContextItemSelected(item); } @Override public void onContextMenuClosed(Menu menu) { super.onContextMenuClosed(menu); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); getMenuInflater().inflate(R.menu.contextmenu, menu); menu.setHeaderTitle("Choose an Option"); } }
0 件のコメント:
コメントを投稿