Android應用開發筆記 – 項目代碼2

1.2 菜單
    MainActivity.java
[java]
package com.example.testmenu; 
 
import android.os.Bundle; 
import android.app.Activity; 
import android.view.ContextMenu; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ContextMenu.ContextMenuInfo; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.Toast; 
 
public class MainActivity extends Activity { 
 
    private static final int MENU_FILE_1 = Menu.FIRST; 
    private static final int MENU_EXIT_2 = Menu.FIRST + 1; 
     
    String[] phones = { 
            "HTC Rezound",  "Samsung Galaxy S II Skyrocket",  
            "Samsung Galaxy Nexus", "Motorola Droid Razr",  
            "Samsung Galaxy S", "Samsung Epic Touch 4G",  
            "iPhone 4S", "HTC Titan" 
            }; 
     
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
         
        ListView listView = (ListView) findViewById(R.id.list01); 
         
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.listitem, 
                phones); 
         
        listView.setAdapter(adapter); 
         
        registerForContextMenu(listView); 
    } 
 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
        //getMenuInflater().inflate(R.menu.activity_main, menu);  
        MenuInflater menuInflater = getMenuInflater(); 
        menuInflater.inflate(R.menu.my_menu, menu); 
        return true; 
    } 
     
    @Override 
    public void onCreateContextMenu(ContextMenu menu, View v, 
            ContextMenuInfo menuInfo) { 
        // TODO Auto-generated method stub  
        if (v.getId() == R.id.list01) { 
            MenuInflater popmenuInflater = getMenuInflater(); 
            popmenuInflater.inflate(R.menu.pop_menu, menu); 
            super.onCreateContextMenu(menu, v, menuInfo); 
        } 
    } 
     
    @Override 
    public boolean onContextItemSelected(MenuItem item) { 
        // TODO Auto-generated method stub  
        TextView text = (TextView) findViewById(R.id.footer); 
         
        switch (item.getItemId()) { 
        case R.id.edt01: 
            text.setText("Edit Selected"); 
             
            return true; 
 
        case R.id.del01: 
            text.setText("Delete Selected"); 
             
            return true; 
             
        case R.id.about01: 
            Toast.makeText(this, "This is software author: xiaobin.", Toast.LENGTH_SHORT).show(); 
             
            return true; 
             
        default: 
             
            return super.onContextItemSelected(item); 
        } 
    } 
     
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
        // TODO Auto-generated method stub  
        switch (item.getItemId()) { 
        case MENU_FILE_1: 
            fileMenu(); 
             
            break; 
 
        case MENU_EXIT_2: 
            exitMenu(); 
 
            break; 
             
        default: 
            break; 
        } 
         
        return false; 
    } 
     
    private void fileMenu() { 
        // TODO My-generated method stub  
    } 
     
    private void exitMenu() { 
        // TODO My-generated method stub  
    } 
     

package com.example.testmenu;

import android.os.Bundle;
import android.app.Activity;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

 private static final int MENU_FILE_1 = Menu.FIRST;
 private static final int MENU_EXIT_2 = Menu.FIRST + 1;
 
 String[] phones = {
   "HTC Rezound",  "Samsung Galaxy S II Skyrocket",
      "Samsung Galaxy Nexus", "Motorola Droid Razr",
      "Samsung Galaxy S", "Samsung Epic Touch 4G",
      "iPhone 4S", "HTC Titan"
      };
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        ListView listView = (ListView) findViewById(R.id.list01);
       
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.listitem,
          phones);
       
        listView.setAdapter(adapter);
       
        registerForContextMenu(listView);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //getMenuInflater().inflate(R.menu.activity_main, menu);
     MenuInflater menuInflater = getMenuInflater();
     menuInflater.inflate(R.menu.my_menu, menu);
        return true;
    }
   
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
      ContextMenuInfo menuInfo) {
     // TODO Auto-generated method stub
     if (v.getId() == R.id.list01) {
      MenuInflater popmenuInflater = getMenuInflater();
      popmenuInflater.inflate(R.menu.pop_menu, menu);
      super.onCreateContextMenu(menu, v, menuInfo);
     }
    }
   
    @Override
    public boolean onContextItemSelected(MenuItem item) {
     // TODO Auto-generated method stub
     TextView text = (TextView) findViewById(R.id.footer);
     
     switch (item.getItemId()) {
  case R.id.edt01:
   text.setText("Edit Selected");
   
   return true;

  case R.id.del01:
   text.setText("Delete Selected");
   
   return true;
   
  case R.id.about01:
   Toast.makeText(this, "This is software author: xiaobin.", Toast.LENGTH_SHORT).show();
   
   return true;
   
  default:
   
   return super.onContextItemSelected(item);
  }
    }
   
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
     // TODO Auto-generated method stub
     switch (item.getItemId()) {
  case MENU_FILE_1:
   fileMenu();
   
   break;

  case MENU_EXIT_2:
   exitMenu();

   break;
   
  default:
   break;
  }
     
     return false;
    }
    www.aiwalls.com
    private void fileMenu() {
     // TODO My-generated method stub
    }
   
    private void exitMenu() {
     // TODO My-generated method stub
    }
   
}

You May Also Like