閱讀之後簡單寫其中的某些功能,先貼圖:
第一步:
相關代碼:
第二步:
代碼中的實現:
package com.jiaruihuademo.testmenu; import java.util.ArrayList; import java.util.List; import android.os.Bundle; import android.annotation.SuppressLint; import android.app.Activity; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.View.OnCreateContextMenuListener; import android.widget.AdapterView; import android.widget.AdapterView.AdapterContextMenuInfo; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.PopupMenu; import android.widget.PopupMenu.OnMenuItemClickListener; import android.widget.Toast; /** * * menu 的簡單使用 * * @author JiaRH * * @date 2013-12-30 上午9:55:29 */ @SuppressLint(NewApi) public class MainActivity extends Activity implements OnMenuItemClickListener, OnCreateContextMenuListener, OnItemClickListener { private ListView listview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listview = (ListView) findViewById(R.id.listView1); List list = new ArrayList(); for (int i = 0; i < 20; i++) { list.add(i + ); } listview.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, list)); listview.setOnItemClickListener(this); } /** * 創建popviewMenu * * @param v */ @SuppressLint(NewApi) public void showpop(View v) { PopupMenu popup = new PopupMenu(this, v); MenuInflater inflater = popup.getMenuInflater(); inflater.inflate(R.menu.main, popup.getMenu()); popup.setOnMenuItemClickListener(this); popup.show(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } /* * (non-Javadoc) * * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem) */ @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.item1: Toast.makeText(this, you have clicked 1 , 0).show(); break; case R.id.item2: Toast.makeText(this, you have clicked 2 , 0).show(); break; case R.id.item3: Toast.makeText(this, you have clicked 3 , 0).show(); break; case R.id.item4: Toast.makeText(this, you have clicked 4 , 0).show(); break; default: break; } return super.onOptionsItemSelected(item); } /** * 響應popmenu的點擊事件 */ @Override public boolean onMenuItemClick(MenuItem item) { switch (item.getItemId()) { case R.id.item1: Toast.makeText(this, you have clicked 1 , 0).show(); break; case R.id.item2: Toast.makeText(this, you have clicked 2 , 0).show(); break; case R.id.item3: Toast.makeText(this, you have clicked 3 , 0).show(); break; case R.id.item4: Toast.makeText(this, you have clicked 4 , 0).show(); break; default: break; } return false; } /** * listview響應事件 */ @Override public void onItemClick(AdapterView parent, View view, int position, long id) { // TODO Auto-generated method stub registerForContextMenu(view); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main, menu); } @Override public boolean onContextItemSelected(MenuItem item) { AdapterContextMenuInfo info = (AdapterContextMenuInfo) item .getMenuInfo(); switch (item.getItemId()) { case R.id.item1: Toast.makeText(this, you have clicked 1 , 0).show(); return true; case R.id.item2: Toast.makeText(this, you have clicked 2 , 0).show(); return true; case R.id.item3: Toast.makeText(this, you have clicked 3 , 0).show(); return true; case R.id.item4: Toast.makeText(this, you have clicked 4 , 0).show(); return true; default: return super.onContextItemSelected(item); } } }
第三步;附上佈局文件:
備註;
我使用的
<uses-sdk
android:minSdkVersion=8
android:targetSdkVersion=18 /></uses-sdk