Android创建Menu菜单实例
本文实例讲述了android创建menu菜单的方法。分享给大家供大家参考。具体方法如下:
import android.app.activity;
import android.content.intent;
import android.os.bundle;
import android.provider.settings;
import android.view.menu;
import android.view.menuinflater;
import android.view.menuitem;
public class testandroidactivity extends activity {
@override
public boolean oncreateoptionsmenu(menu menu) {
// todo auto-generated method stub
super.oncreateoptionsmenu(menu);
menuinflater inflater = getmenuinflater();
inflater.inflate(r.menu.menu, menu);
return true;
}
/** called when the activity is first created. */
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
}
@override
public boolean onoptionsitemselected(menuitem item) {
// todo auto-generated method stub
switch(item.getitemid())
{
case r.id.settings:
startactivity(new intent(this,com.test.settings.class));
return true;
}
return false;
}
}
import android.os.bundle;
import android.preference.preferenceactivity;
public class settings extends preferenceactivity {
@override
protected void oncreate(bundle savedinstancestate) {
// todo auto-generated method stub
super.oncreate(savedinstancestate);
addpreferencesfromresource(r.xml.settings);
}
}
相关配置文件如下:
<preferencescreen xmlns:android="http://schemas.android.com/apk/res/android" >
<checkboxpreference android:key="music" android:summary="@string/music_summary" android:defaultvalue="true" android:title="@string/music_title"/>
<checkboxpreference android:key="hints" android:summary="@string/hints_summary" android:title="@string/hints_title"/>
</preferencescreen>
希望本文所述对大家的android程序设计有所帮助。