本文介绍: 1、选项菜单OptionsMenu和上下文菜单ContextMenu 效果如下:2、MainActivitypackage com.example.aidldemo;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;…
1、选项菜单OptionsMenu和上下文菜单ContextMenu 效果如下:
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上下文菜单:
<?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>
<?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>
<?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>
<?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>
原文地址:https://blog.csdn.net/u014056045/article/details/126708065
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_46484.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。