Android 的选项菜单OptionsMenu和上下文菜单ContextMenu的使用案例
1、选项菜单OptionsMenu和上下文菜单ContextMenu 效果如下:
2、MainActivity
package com.example.aidldemo; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.ContextMenu; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.widget.TextView; import android.widget.Toast; public class Main3Activity extends AppCompatActivity { private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main3); tv = findViewById(R.id.textView2); registerForContextMenu(tv); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater menuInflater = getMenuInflater(); menuInflater.inflate(R.menu.action_bar_menu,menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch(item.getItemId()){ case R.id.search: Toast.makeText(this,"search",Toast.LENGTH_SHORT).show(); break; case R.id.msg: // Toast.makeText(this,"msg",Toast.LENGTH_SHORT).show(); Intent intent = new Intent(this,MainActivity.class); startActivity(intent); break; case R.id.setting: Toast.makeText(this,"设置",Toast.LENGTH_SHORT).show(); break; case R.id.about : Toast.makeText(this,"关于",Toast.LENGTH_SHORT).show(); break; } return super.onOptionsItemSelected(item); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { MenuInflater menuInflater = getMenuInflater(); menuInflater.inflate(R.menu.context_menu, menu); super.onCreateContextMenu(menu, v, menuInfo); } @Override public boolean onContextItemSelected(MenuItem item) { switch(item.getItemId()){ case R.id.copy: Toast.makeText(this,"copy",Toast.LENGTH_SHORT).show(); break; case R.id.collect : Toast.makeText(this,"collect",Toast.LENGTH_SHORT).show(); break; case R.id.report : Toast.makeText(this,"report",Toast.LENGTH_SHORT).show(); break; } return super.onContextItemSelected(item); } }
3、在res目录下的menu目录下创建action_bar_menu.xml选项菜单和context_menu.xml上下文菜单:
选项菜单 action_bar_menu.xml 文件
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/search" android:icon="@android:drawable/ic_menu_search" android:title="搜索" app:actionViewClass="android.support.v7.widget.SearchView" app:showAsAction="always" /> <item android:id="@+id/msg" android:actionLayout="@layout/fragment_list" android:icon="@android:drawable/ic_menu_sort_by_size" android:title="搜索" app:showAsAction="always" /> <item android:id="@+id/setting" android:icon="@drawable/ic_looks_one" android:title="设置" /> <item android:id="@+id/about" android:icon="@drawable/ic_looks_two" android:title="关于" /> </menu>
上下文菜单 context_menu.xml 文件
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/copy" android:title="复制" /> <item android:id="@+id/collect" android:title="收藏" /> <item android:id="@+id/report" android:title="举报" /> </menu>
3、activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".Main3Activity"> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Context Menu" android:textSize="30sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout>
4、清单文件 AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.aidldemo"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".Main3Activity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".MainActivity"> <meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".Main3Activity"></meta-data> </activity> </application> </manifest>