我的android 第18天 - 选择菜单

我的android 第18天 - 选择菜单

 介绍:菜单是在开发中常用的一个控件,它分为选择菜单和上下文菜单,可以通过菜单设计器实现菜单的设计,也可以通过代码手动装配菜单,一个Activity只能有一个选择菜单,但是可以有多个上下文菜单,一个控制只能拥有一个上下文菜单

 案例:通过选择菜单和上下文菜单实现对图书的管理

  (该项目涉及三个知识点:选择菜单、上下文菜单、自定义适配器)

一、界面设计及案例分析



   二、实现流程

1、构建选择菜单

•在菜单设计器设计选择菜单
•通过代码添加选择菜单
•groupId : 组别(不会影响菜单项的排序顺序)
•itemId : 菜单项的唯一标识
•order: 菜单项的排列顺序(数值越小,排在越前面)
•title:标题
•MenuItem item = menu.add(groupId, itemId, order, title); 创建菜单项
•item.setIcon(R.drawable.icon); 设置图标
•onCreateOptionsMenu :在第一次点击Menu的时候调用,在这里进行菜单的初始化
•getMenuInflater().inflate(R.menu.book, menu);装配菜单 

2、监听选择菜单项的点击:onOptionsItemSelected :Activity方法

/**
     * 第一次点击Menu键的时触发(在这里创建选择菜单)
     */
    public boolean onCreateOptionsMenu(Menumenu) {
    // 把功能类似的菜单项分到同组
   menu.add(0, R.id.menu_add, 0, R.string.add).setIcon(R.drawable.menu_add);
    menu.add(0, R.id.menu_delete, 0, R.string.delete).setIcon(R.drawable.menu_delete);
    menu.add(1, R.id.menu_good_look, 0, R.string.good_look).setIcon(R.drawable.menu_good_look);
    menu.add(1, R.id.menu_not_good_look, 0, R.string.not_good_look).setIcon(R.drawable.menu_not_good_look);
    // menu.setGroupEnabled(0,false);
//装配菜单
    getMenuInflater().inflate(R.menu.menu_book_options,menu);
    return super.onCreateOptionsMenu(menu);
    }
 /**
     * 监听选择菜单的点击     */
    public boolean onOptionsItemSelected(MenuItemitem) {
    int itemId = item.getItemId();
    switch (itemId) {
case R.id.menu_add: // 添加
menuEidtOrAdd(null);
break;
case R.id.menu_delete: // 删除
optionsMenuDelete();
break;
case R.id.menu_good_look: // 好看
case R.id.menu_not_good_look: // 不好看
break;
}
    return super.onOptionsItemSelected(item);
    }

   二、实现流程

       3、在OnCreate()中装配ListView数据,实现图书列表的显示

          . ListView 数据初始化:ArrayList<Map>

          . ListView 添加适配器,装配数据

 

4、选择菜单功能实现

  

•添加图书
•自定义对话框
•实现添加图书到数据列表中
•通知适配器数据已改变,需重新刷新列表
/**
* 给列表框装配数据,在Activity中显示图书列表信息
*/   
ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String,Object>>();
        for (int i = 0; i < 20; i++) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put(NAME, "5分钟突破Java "+ (i+1) +".0");
        map.put(GOOG_LOOK, i%2==0);
        data.add(map);
}
        this.data = data;
        listView.setAdapter( adapter = new BookAdapter(this) );



下载视频代码


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
多按钮选择Android UI设计中经常用到的一种控件,它通常用于让用户从多个选项中选择一个或多个选项。在本文中,我将为您介绍如何在Android应用程序中实现多按钮选择。 1. RadioButton RadioButton是一种常见的多按钮选择控件,它通常与RadioGroup一起使用。RadioGroup可以将多个RadioButton组合在一起,并自动控制只有一个RadioButton被选中。 以下是实现多按钮选择的RadioButton示例代码: ```xml <RadioGroup android:id="@+id/radio_group" android:layout_width="wrap_content" android:layout_height="wrap_content"> <RadioButton android:id="@+id/radio_button_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 1"/> <RadioButton android:id="@+id/radio_button_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 2"/> <RadioButton android:id="@+id/radio_button_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 3"/> </RadioGroup> ``` 2. CheckBox CheckBox是另一种常见的多按钮选择控件,它通常用于选择多个选项。与RadioButton不同,CheckBox可以同时选择多个选项。 以下是实现多按钮选择的CheckBox示例代码: ```xml <CheckBox android:id="@+id/checkbox_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 1"/> <CheckBox android:id="@+id/checkbox_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 2"/> <CheckBox android:id="@+id/checkbox_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 3"/> ``` 3. ToggleButton ToggleButton是一种可以在两个状态之间切换的按钮,它通常用于打开或关闭某些功能。与RadioButton和CheckBox不同,ToggleButton只有两个状态。 以下是实现多按钮选择的ToggleButton示例代码: ```xml <ToggleButton android:id="@+id/toggle_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOn="On" android:textOff="Off"/> ``` 这些是Android中实现多按钮选择的常见控件,您可以根据自己的需求选择适合您应用程序的控件。希望本文能够对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值