文件结构:
java代码:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private static final int upload = 1;
private static final int download = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.text_view);
registerForContextMenu(textView);
//弹出式菜单
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
PopupMenu popupMenu = new PopupMenu(MainActivity.this,view);
popupMenu.inflate(R.menu.main_menu);
popupMenu.show();
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()){
case R.id.upload:
Toast.makeText(MainActivity.this,"你选择了" + getResources().getString(R.string.upload),Toast.LENGTH_SHORT).show();
break;
case R.id.download:
Toast.makeText(MainActivity.this,"你选择了" + getResources().getString(R.string.download),Toast.LENGTH_SHORT).show();
break;
case R.id.stop:
Toast.makeText(MainActivity.this,"你选择了" + getResources().getString(R.string.stop),Toast.LENGTH_SHORT).show();
break;
case R.id.quit:
Toast.makeText(MainActivity.this,"你选择了" + getResources().getString(R.string.quit),Toast.LENGTH_SHORT).show();
break;
}
return false;
}
});
}
});
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
menu.setHeaderIcon(R.mipmap.ic_launcher);
menu.setHeaderTitle("我是上下文菜单");
getMenuInflater().inflate(R.menu.main_menu,menu);
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.upload:
Toast.makeText(this,"你选择了" + getResources().getString(R.string.upload),Toast.LENGTH_SHORT).show();
break;
case R.id.download:
Toast.makeText(this,"你选择了" + getResources().getString(R.string.download),Toast.LENGTH_SHORT).show();
break;
case R.id.stop:
Toast.makeText(this,"你选择了" + getResources().getString(R.string.stop),Toast.LENGTH_SHORT).show();
break;
case R.id.quit:
Toast.makeText(this,"你选择了" + getResources().getString(R.string.quit),Toast.LENGTH_SHORT).show();
break;
}
return super.onContextItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//1.组id 、2.菜单id 、 3. 排序id 、 4.菜单显示内容
// menu.add(0,upload,upload,"上传");
// menu.add(0,download,download,"下载");
getMenuInflater().inflate(R.menu.main_menu,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.upload:
Toast.makeText(this,"你选择了" + getResources().getString(R.string.upload),Toast.LENGTH_SHORT).show();
break;
case R.id.download:
Toast.makeText(this,"你选择了" + getResources().getString(R.string.download),Toast.LENGTH_SHORT).show();
break;
case R.id.stop:
Toast.makeText(this,"你选择了" + getResources().getString(R.string.stop),Toast.LENGTH_SHORT).show();
break;
case R.id.quit:
Toast.makeText(this,"你选择了" + getResources().getString(R.string.quit),Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
}
菜单layout:
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/upload"
android:icon="@mipmap/ic_launcher_round"
android:title="@string/upload" />
<item
android:id="@+id/download"
android:title="@string/download" />
<item
android:id="@+id/stop"
android:title="@string/stop" />
<menu >
<item android:title="Item" />
</menu>
<item
android:id="@+id/quit"
android:title="@string/quit" />
</menu>