阅读之后简单写其中的某些功能,先贴图:


第一步:

相关代码:


?
1
第二步:


代码中的实现:


?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 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<string> list = new ArrayList<string>(); for ( int i = 0 ; i < 20 ; i++) { list.add(i + ); } listview.setAdapter( new ArrayAdapter<string>( 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); } } } </string></string></string>

第三步;附上布局文件:



?
1 2 3 4 5 6 <relativelayout android:layout_height= "match_parent" android:layout_width= "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= ".MainActivity" xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http://schemas.android.com/tools" > <textview android:id= "@+id/textView1" android:layout_height= "wrap_content" android:layout_width= "wrap_content" android:text= "@string/hello_world" ><button android:id= "@+id/showpop" android:layout_below= "@+id/textView1" android:layout_height= "wrap_content" android:layout_margintop= "16dp" android:layout_width= "wrap_content" android:onclick= "showpop" android:text= "show" popmenu= "" > <listview android:id= "@+id/listView1" android:layout_alignleft= "@+id/showpop" android:layout_below= "@+id/showpop" android:layout_height= "wrap_content" android:layout_margintop= "40dp" android:layout_width= "match_parent" > </listview></button></textview></relativelayout>

备注;


我使用的



android:minSdkVersion=8
android:targetSdkVersion=18 />



更多相关文章

  1. Android中点击事件之KeyListener实现步骤
  2. unity和Android交互(内容部分为转载)
  3. android截图代码
  4. H5调android 的方法修改UI无效
  5. android实现事件监听的2种常用方式
  6. Android(安卓)读取安装的非系统应用程序
  7. android 手势识别(一)
  8. android:软件的安装和卸载源代码
  9. android 获得 View

随机推荐

  1. Xamarin.Android中使用android:onClick="
  2. android tab上显示数字
  3. Android视图篇之一:Android常见基本布局
  4. android之知识点小结二
  5. CheckedTextView的显示问题
  6. android 环境变量搭建
  7. Android(安卓)系统广播机制
  8. android中自定义控件
  9. Android获取WIFI状态下的IP地址以及MAC地
  10. android web services