`
byandby
  • 浏览: 1688551 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

android 菜单例子

阅读更多
     也没什么了  就是一个 android菜单的 小例子 实现步骤如下:
     (1)创建一个包含文本视图的XML布局文件。
     (2)创建一个Activity类,其中包含在第一步中定义的布局。
     (3)设置菜单
     (4)向菜单添加一些常规菜单项
     (5)向菜单添加一些辅助菜单项
     (6)响应菜单项
     (7)修改 AndroidManifest.xml文件,以显示应用程序正确的标题
  
   布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
	android:id="@+id/textViewId"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Debugging Scratch Pad"
    />
</LinearLayout>


    Activity类
package xiaohang.zhimeng;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class SampleMenusActivity extends Activity {
	
	Menu myMenu = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
    //重写onCreateOptionsMenu 并以编程方式设置菜单
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	// call the parent to attach any system level menus
    	 super.onCreateOptionsMenu(menu);
    	 this.myMenu = menu;
    	 
    	 //add a few normal menus
    	 addRegularMenuItems(menu);
    	 
    	 //add a few secondary menus
    	 add5SecondaryMenuItems(menu);
    	 
    	 //it must return true to show the menu
    	 //if it is false menu won't show
    	 return true;
    	
    }
    
    //添加常规菜单项
    private void addRegularMenuItems(Menu menu){
    	int base = Menu.FIRST; // value is 1
    	
    	menu.add(base, base, base, "append");
    	menu.add(base, base+1, base+1, "item 2");
    	menu.add(base, base+2, base+2, "clear");
    	
    	menu.add(base, base+3, base+3, "hide secondary");
    	menu.add(base, base+4, base+4, "show secondary");
    	
    	menu.add(base, base+5, base+5, "enable secondary");
    	menu.add(base, base+6, base+6, "disable secondary");
    	
    	menu.add(base, base+7, base+7, "check secondary");
    	menu.add(base, base+8, base+8, "uncheck secondary");
    }
    
    //添加辅助菜单项
    private void add5SecondaryMenuItems(Menu menu){
    	//Secondary items are shown just like everything else
    	int base=Menu.CATEGORY_SECONDARY;
    	
    	menu.add(base, base+1, base+1, "sec. item 1");
    	menu.add(base, base+2, base+2, "sec. item 2");
    	menu.add(base, base+3, base+3, "sec. item 3");
    	menu.add(base, base+4, base+4, "sec. item 4");
    	menu.add(base, base+5, base+5, "sec. item 5");
    	
    }
    
    //响应菜单项单击
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	if (item.getItemId() == 1) {
			appendText("\nhello");
		}
    	else if (item.getItemId() ==2) {
			appendText("\nitem2");
		}
    	else if (item.getItemId() == 3) {
			emptyText();
		}
    	else if (item.getItemId() ==4) {
			//hide secondary
    		this.appendMenuItemText(item);
    		this.myMenu.setGroupVisible(Menu.CATEGORY_SECONDARY, false);
		}
    	else if(item.getItemId() == 5){
    		//show secondary
    		this.appendMenuItemText(item);
    		this.myMenu.setGroupVisible(Menu.CATEGORY_SECONDARY, true);
    	}
    	else if (item.getItemId() == 6) {
			//enable secondary
    		this.appendMenuItemText(item);
    		this.myMenu.setGroupEnabled(Menu.CATEGORY_SECONDARY, true);
		}
    	else if(item.getItemId() == 7){
    		//disable secondary
    		this.appendMenuItemText(item);
    		this.myMenu.setGroupEnabled(Menu.CATEGORY_SECONDARY, false);
    	}
    	else if (item.getItemId() == 8) {
			//check secondary
    		this.appendMenuItemText(item);
    		myMenu.setGroupCheckable(Menu.CATEGORY_SECONDARY, true, false);
		}
    	else if (item.getItemId() == 9) {
			//uncheck secondary
    		this.appendMenuItemText(item);
    		myMenu.setGroupCheckable(Menu.CATEGORY_SECONDARY, false, false);
		}
    	else {
			this.appendMenuItemText(item);
		}
    	//should return true if the menu item
    	//is handled
    	return true;
    	
    }
    
    
    //向调试TextView 写入数据的实用程序函数
    //Given  a string of text append it to the TextView
    private void appendText(String text){
    	TextView tv = (TextView)this.findViewById(R.id.textViewId);
    	tv.setText(tv.getText() + text);
    }
    
    //Given a menu item append its title to the TextView
    private void appendMenuItemText(MenuItem menuItem){
    	String title = menuItem.getTitle().toString();
    	TextView tv = (TextView)this.findViewById(R.id.textViewId);
    	tv.setText(tv.getText() + "\n" + title);
    }
    
    //Empty the TextView of its contents
    private void emptyText(){
    	TextView tv = (TextView)this.findViewById(R.id.textViewId);
    	tv.setText("");
    }
}


   AndroidManifest.xml文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="xiaohang.zhimeng"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".SampleMenusActivity"
                  android:label="Sample Menus Application">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
    <uses-sdk android:minSdkVersion="9" />

</manifest> 


    运行效果如下






   源码附件 
  • 大小: 34.4 KB
  • 大小: 40.6 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics