2. 需覆写onCreateContextMenu及onContextItemSelected这二个方法
3. MainActivity.java
package org.me.android_contextmenu; import android.app.Activity; import android.os.Bundle; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.MenuItem; import android.view.View; import android.widget.LinearLayout; import android.widget.Toast; public class MainActivity extends Activity { @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); LinearLayout windowLayout = (LinearLayout) findViewById(R.id.windowLayout); //注册长按选单 this.registerForContextMenu(windowLayout); } @Override public boolean onContextItemSelected(MenuItem item) { //当使用者点选项目时,所需的动作 Toast.makeText(this, "您选择的是"+item.getTitle(), Toast.LENGTH_SHORT).show(); return super.onContextItemSelected(item); } @Override public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenuInfo menuInfo) { //设定选单内容 super.onCreateContextMenu(contextMenu, view, menuInfo); contextMenu.add(0, 0, 0, "红茶"); contextMenu.add(0, 1, 0, "奶茶"); contextMenu.add(0, 2, 0, "绿茶"); contextMenu.add(0, 3, 0, "青茶"); } }
3. main.xml(Layout)
4. 执行之後的画面。
沒有留言:
張貼留言