- package com.example.contextmenucctu;
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.ContextMenu;
- import android.view.ContextMenu.ContextMenuInfo;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.LinearLayout;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- LinearLayout linear=(LinearLayout)findViewById(R.id.LinearLayout1);
- registerForContextMenu(linear);
- }
- @Override
- public void onCreateContextMenu(ContextMenu menu,View v,ContextMenuInfo of) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- }
- public boolean onContextItemSelected(MenuItem item)
- {
- String msg="";
- switch(item.getItemId())
- {
- case R.id.item1:
- msg=getString(R.string.m1);
- break;
- case R.id.item2:
- msg=getString(R.string.m2);
- break;
- case R.id.item3:
- msg=getString(R.string.m3);
- break;
- case R.id.item4:
- msg=getString(R.string.m4);
- break;
- case R.id.item5:
- finish();
- default:
- return super.onOptionsItemSelected(item);
- }
- Toast.makeText(MainActivity.this, msg, Toast.LENGTH_LONG).show();
- return true;
- }
- }
復制代碼
|