android 菜单实例
来源:技术人生 责任编辑:栏目编辑 发表时间:2013-07-01 16:56 点击:次
import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.TextView;
import android.widget.Toast;
// 演示两种菜单的实现方式:上下文菜单(通过在某元素上长按,来呼出菜单)和选项菜单(通过按手机上的菜单按钮,来呼出菜单)
public class layoutMain extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 为 R.id.txt1 注册一个上下文菜单(在此 TextView 上长按,则会呼出上下文菜单)
// 具体呼出的菜单内容需要重写 onCreateContextMenu 来创建
TextView txt1 = (TextView) this.findViewById(R.id.txt1);
this.registerForContextMenu(txt1);
public class layoutMain extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 为 R.id.txt1 注册一个上下文菜单(在此 TextView 上长按,则会呼出上下文菜单)
// 具体呼出的菜单内容需要重写 onCreateContextMenu 来创建
TextView txt1 = (TextView) this.findViewById(R.id.txt1);
this.registerForContextMenu(txt1);
// 为 R.id.txt2 注册一个上下文菜单
TextView txt2 = (TextView) this.findViewById(R.id.txt2);
this.registerForContextMenu(txt2);
}
// 重写 onCreateOptionsMenu 用以创建选项菜单
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem menuItem = menu.add(0, 0, 0, "菜单");
menuItem.setIcon(R.drawable.icon01);
menuItem.setAlphabeticShortcut('a');
menu.add(0, 1, 1, "菜单2").setIcon(R.drawable.icon02);
menu.add(0, 2, 2, "菜单3").setIcon(R.drawable.icon03);
menu.add(0, 4, 4, "菜单7").setIcon(R.drawable.icon04);
menu.add(0, 3, 3, "菜单8").setIcon(R.drawable.icon05);
return super.onCreateOptionsMenu(menu);
}
TextView txt2 = (TextView) this.findViewById(R.id.txt2);
this.registerForContextMenu(txt2);
}
// 重写 onCreateOptionsMenu 用以创建选项菜单
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem menuItem = menu.add(0, 0, 0, "菜单");
menuItem.setIcon(R.drawable.icon01);
menuItem.setAlphabeticShortcut('a');
menu.add(0, 1, 1, "菜单2").setIcon(R.drawable.icon02);
menu.add(0, 2, 2, "菜单3").setIcon(R.drawable.icon03);
menu.add(0, 4, 4, "菜单7").setIcon(R.drawable.icon04);
menu.add(0, 3, 3, "菜单8").setIcon(R.drawable.icon05);
return super.onCreateOptionsMenu(menu);
}
// 重写 onOptionsItemSelected 用以响应选项菜单
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
Toast.makeText(layoutMain.this, "被单击的菜单项为:" + String.valueOf(item.getItemId()), Toast.LENGTH_SHORT).show();
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
Toast.makeText(layoutMain.this, "被单击的菜单项为:" + String.valueOf(item.getItemId()), Toast.LENGTH_SHORT).show();
return true;
}
}
}
}
本文出自 “技术人生” 博客,请务必保留此出处http://zhaohaiyang.blog.51cto.com/2056753/487016
相关新闻>>
最新推荐更多>>>
- 发表评论
-
- 最新评论 更多>>