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

\\

\\

\


第一步:

\

相关代码:


?
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实现事件监听的2种常用方式
  2. Android中点击事件之KeyListener实现步骤
  3. ANDROID图片压缩代码
  4. Android 如何在Java代码中手动设置控件的marginleft
  5. android典型代码系列(十)------获取一个应用程序的权限信息(反射
  6. android播放音乐文件代码
  7. ListView的长按键事件的处理

随机推荐

  1. Android Jetpack Compose - Button
  2. Android文字转语音TTS离线程序
  3. android自带的base64编码
  4. 记View越界绘制
  5. Android Studio插件爆红,Plugin Error :
  6. Android使用videoview播放res/raw下的视
  7. android 绘画网格图片
  8. Android自定义对话框去掉白色边框
  9. 增加滚动布局
  10. Android使用系统内置邮件发送邮件