枚举类型的理解和应用

相信学习java的人,在平时开发时是很少用到枚举。说实在的,本人对枚举类型一向都是云里雾里,直到最近在看android源码时,才微探到其中的妙处。

   接下来就以一个例子结合枚举和手势知识点的应用开发来讲解枚举类型在android应用开发中的活用(很多知识点都是借鉴android源码,学习android,源码是最好的教程,对开源致敬!)。

      示例代码如下:

[java]  view plain copy
  1. package com.phicomm.hu.eagt;  
  2.   
  3. import android.app.Activity;  
  4. import android.gesture.Gesture;  
  5. import android.gesture.GestureOverlayView;  
  6. import android.gesture.GestureOverlayView.OnGestureListener;  
  7. import android.gesture.GestureOverlayView.OnGesturePerformedListener;  
  8. import android.os.Bundle;  
  9. import android.view.MotionEvent;  
  10. import android.view.View;  
  11. import android.view.View.OnClickListener;  
  12. import android.widget.Button;  
  13. import android.widget.TextView;  
  14. import android.widget.Toast;  
  15.   
  16. public class EnumAndGestureTest extends Activity implements OnGestureListener  
  17. {  
  18.       
  19.     private GestureOverlayView mGestureOverlayView;  
  20.     private Button mLeftButton, mRightButton;  
  21.     private TextView mHeaderMsg;  
  22.     /** Called when the activity is first created. */  
  23.     @Override  
  24.     public void onCreate(Bundle savedInstanceState) {  
  25.         super.onCreate(savedInstanceState);  
  26.         setContentView(R.layout.main);  
  27.         //设置ActionBar标题  
  28.         getActionBar().setTitle(R.string.draw_gesture);  
  29.           
  30.         //获取相关组件对象  
  31.         mGestureOverlayView = (GestureOverlayView) findViewById(R.id.gesture);  
  32.         mLeftButton = (Button)findViewById(R.id.left);  
  33.         mRightButton = (Button)findViewById(R.id.right);  
  34.         mHeaderMsg = (TextView)findViewById(R.id.header);  
  35.           
  36.           
  37.         //注册手势相关监听器  
  38.         mGestureOverlayView.addOnGestureListener(this);  
  39.         //mGestureOverlayView.addOnGesturePerformedListener(this);  
  40.   
  41.     }  
  42.   
  43.       
  44.     @Override  
  45.     protected void onResume() {  
  46.         // TODO Auto-generated method stub  
  47.         super.onResume();  
  48.         //程序创建时的绘制标题状态和按钮状态  
  49.         setButtunStatus(Stage.Introduction);  
  50.         monitorChange(Stage.Introduction);  
  51.     }  
  52.   
  53.   
  54.     // 定义左边按钮各状态的枚举,注:构造函数和成员变量的位置都要放在后面  
  55.     private enum LeftButtonMode {  
  56.         // 按钮显示为取消、可点击状态的枚举对象(枚举对象意味个人说法)  
  57.         Cancel(R.string.cancel, true),  
  58.         // 按钮显示为取消、不可点击状态  
  59.         CancelDisabled(R.string.cancel, false),  
  60.         // 按钮显示为重试、可点击状态  
  61.         Retry(R.string.retry, true),  
  62.         // 按钮显示为重试、不可点击状态  
  63.         RetryDisabled(R.string.retry, false);  
  64.   
  65.         LeftButtonMode(int text, boolean enabled) {  
  66.             this.text = text;  
  67.             this.enabled = enabled;  
  68.         }  
  69.   
  70.         final int text;  
  71.         final boolean enabled;  
  72.     }  
  73.   
  74.     // 定义右边按钮各状态的枚举  
  75.     private enum RightButtonMode {  
  76.         Continue(R.string.next, true),   
  77.         ContinueDisabled(R.string.next, false),   
  78.         Confirm(R.string.confirm, true),   
  79.         ConfirmDisabled(R.string.confirm,false);  
  80.   
  81.         RightButtonMode(int text, boolean enabled) {  
  82.             this.text = text;  
  83.             this.enabled = enabled;  
  84.         }  
  85.   
  86.         final int text;  
  87.         final boolean enabled;  
  88.     }  
  89.   
  90.     //统筹左右按钮状态的枚举,可以理解为枚举的复用(将左右按钮的状态枚举作为实参传递)  
  91.     private enum Stage {  
  92.   
  93.         Introduction(R.string.gesture_introdution, LeftButtonMode.Cancel,  
  94.                 RightButtonMode.Confirm),             
  95.         Ready(  
  96.                 R.string.gesture_ready, LeftButtonMode.CancelDisabled,  
  97.                 RightButtonMode.ConfirmDisabled),             
  98.         Gesturing(  
  99.                 R.string.gesture_drawing, LeftButtonMode.RetryDisabled,  
  100.                 RightButtonMode.ContinueDisabled),            
  101.         Finish(  
  102.                 R.string.gesture_finish, LeftButtonMode.Retry,  
  103.                 RightButtonMode.Continue);  
  104.   
  105.         Stage(int headerMessage, LeftButtonMode leftMode,  
  106.                 RightButtonMode rightMode) {  
  107.             this.headerMessage = headerMessage;  
  108.             this.leftMode = leftMode;  
  109.             this.rightMode = rightMode;  
  110.         }  
  111.   
  112.         final int headerMessage;  
  113.         final LeftButtonMode leftMode;  
  114.         final RightButtonMode rightMode;  
  115.     }  
  116.   
  117.     //该方法的定义重点在于说明枚举类型能在switch语句中的活用(比如,我们可以根据传递的枚举进行监听操作某些事件)  
  118.     private void monitorChange(Stage stage)  
  119.     {  
  120.         switch (stage) {  
  121.         case Introduction:  
  122.             Toast.makeText(this"App onResume", Toast.LENGTH_SHORT).show();  
  123.             break;  
  124.         case Ready:  
  125.             Toast.makeText(this"onGestureStarted", Toast.LENGTH_SHORT).show();  
  126.             break;  
  127.         case Gesturing:  
  128.             Toast.makeText(this"onGesture", Toast.LENGTH_SHORT).show();  
  129.             break;  
  130.         case Finish:  
  131.             Toast.makeText(this"onGestureEnded", Toast.LENGTH_SHORT).show();  
  132.             break;  
  133.         }  
  134.     }  
  135.       
  136.     //根据传进来的枚举Stage设置和显示按钮的状态  
  137.     private void setButtunStatus(Stage stage)  
  138.     {  
  139.         mHeaderMsg.setText(stage.headerMessage);  
  140.         mLeftButton.setText(stage.leftMode.text);  
  141.         mLeftButton.setEnabled(stage.leftMode.enabled);  
  142.         mRightButton.setText(stage.rightMode.text);  
  143.         mRightButton.setEnabled(stage.rightMode.enabled);  
  144.     }  
  145.     // 正在画手势时调用该函数  
  146.     @Override  
  147.     public void onGesture(GestureOverlayView overlay, MotionEvent event) {  
  148.         // TODO Auto-generated method stub  
  149.   
  150.         //在画手势的过程中,更新按钮的状态  
  151.         setButtunStatus(Stage.Gesturing);  
  152.         monitorChange(Stage.Gesturing);  
  153.     }  
  154.   
  155.     @Override  
  156.     public void onGestureCancelled(GestureOverlayView overlay, MotionEvent event) {  
  157.         // TODO Auto-generated method stub  
  158.     }  
  159.   
  160.     // 画完手势时调用  
  161.     @Override  
  162.     public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {  
  163.         // TODO Auto-generated method stub  
  164.         //画完手势更新按钮状态  
  165.         setButtunStatus(Stage.Finish);  
  166.         monitorChange(Stage.Finish);  
  167.   
  168.     }  
  169.   
  170.     // 开始画手势时调用(即用户手指点住屏幕还没开始滑动时调用)  
  171.     @Override  
  172.     public void onGestureStarted(GestureOverlayView overlay, MotionEvent event) {  
  173.         // TODO Auto-generated method stub  
  174.         //准备画手势时更新按钮状态  
  175.         setButtunStatus(Stage.Ready);  
  176.         monitorChange(Stage.Ready);  
  177.     }  
  178.   
  179. }  
  Main.xml文件布局如下:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <LinearLayout  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="match_parent"  
  10.         android:layout_marginTop="20dip"  
  11.         android:layout_weight="1"  
  12.         android:orientation="vertical" >  
  13.   
  14.         <TextView  
  15.             android:id="@+id/header"  
  16.             android:layout_width="wrap_content"  
  17.             android:layout_height="wrap_content"  
  18.             android:layout_marginTop="20dip"  
  19.             android:layout_gravity="center" />  
  20.   
  21.         <android.gesture.GestureOverlayView  
  22.             android:id="@+id/gesture"  
  23.             android:layout_width="fill_parent"  
  24.             android:layout_height="fill_parent"  
  25.             android:gestureStrokeType="multiple"  
  26.             android:gestureStrokeWidth="4" >  
  27.         </android.gesture.GestureOverlayView>  
  28.     </LinearLayout>  
  29.   
  30.     <LinearLayout  
  31.         android:layout_width="match_parent"  
  32.         android:layout_height="match_parent"  
  33.         android:layout_weight="10"  
  34.         android:orientation="horizontal" >  
  35.   
  36.         <Button  
  37.             android:id="@+id/left"  
  38.             android:layout_width="wrap_content"  
  39.             android:layout_height="wrap_content"  
  40.             android:layout_weight="1" />  
  41.   
  42.         <Button  
  43.             android:id="@+id/right"  
  44.             android:layout_width="wrap_content"  
  45.             android:layout_height="wrap_content"  
  46.             android:layout_weight="1" />  
  47.     </LinearLayout>  
  48.   
  49. </LinearLayout>  

  个人对枚举理解的总结:

      1. 枚举以关键字enum来定义,而不是class,千万不要认为枚举就是类,枚举是比较特殊的。(枚举:对象一枚枚的列举)

      2. 如代码中的LeftButtonMode枚举,如果把它理解为一个类,那么LeftButtonMode.Cancel的Cancel就相当于LeftButtonMode的一个成员,这样的理解是错误。

     3. LeftButtonMode.Cancel的Cancel就相当于一个对象,其他的LeftButtonMode中的CancelDisabled、Retry、RetryDisabled和Cancel是同等级的。

    4. LeftButtonMode.Cancel描述的是Cancel这个对象,LeftButtonMode就相当于是一个标签,没有什么实际意义。

    5. 我们知道枚举中的Cancel这个对象是没有经过new来创建的,可以理解为它是隐藏创建的,也就是说LeftButtonMode里列举的都是已经创建好的对象。在利用它时是通过enum关键定义的LeftButtonMode这个标签来附带它(LeftButtonMode.Cancel)使用的。

    6. 有一点值得注意,就是例子的代码中如LeftButtonMode是有构造函数的,那么这个构造函数就相当于是它列举的对象所对应的构造函数。

    ok,个人对枚举的理解和应用就到此结束,希望对大家有帮助!PS:NBA总决赛已经开打,本屌丝是詹蜜,期待迈阿密卫冕,哈哈!!

    Demo代码下载链接:http://download.csdn.net/detail/stevenhu_223/5560529


枚举类型特别实用的特性(在switch语句内使用)

 枚举类型可以在switch语句内使用,如下代码:

     1.枚举类

      

[java]  view plain copy
  1. public enum Season   
  2. {  
  3.   
  4.     SPRING, SUMMER, FALL, WINTER  
  5. }  
    

      2.switch语句内使用:

[java]  view plain copy
  1. public class EnumTest   
  2. {  
  3.   
  4.     private Season season;  
  5.       
  6.     public EnumTest(Season season)  
  7.     {  
  8.         this.season = season;  
  9.     }  
  10.       
  11.     public void displaySeason()  
  12.     {  
  13.         switch (season) {  
  14.         case SPRING:  
  15.              System.out.println("春天花会开");  
  16.             break;  
  17.         case SUMMER:  
  18.             System.out.println("炎热的夏天");  
  19.             break;  
  20.         case FALL:  
  21.             System.out.println("秋天扫落叶");  
  22.             break;  
  23.         case WINTER:  
  24.             System.out.println("寒冷的冬天");  
  25.             break;  
  26.         default:  
  27.             break;  
  28.         }  
  29.     }  
  30.     /** 
  31.      * @param args 
  32.      */  
  33.     public static void main(String[] args)   
  34.     {  
  35.         // TODO Auto-generated method stub  
  36.   
  37.         //values方法列举枚举常量,ordinal方法为枚举常量的声明顺序  
  38.         for (Season s : Season.values())  
  39.         {  
  40.             System.out.println(s + " ,ordinal " + s.ordinal());  
  41.         }  
  42.           
  43.         EnumTest   
  44.         spring = new EnumTest(Season.SPRING),  
  45.         summer = new EnumTest(Season.SUMMER),  
  46.         fall = new EnumTest(Season.FALL),  
  47.         winter = new EnumTest(Season.WINTER);  
  48.           
  49.         spring.displaySeason();  
  50.         summer.displaySeason();  
  51.         fall.displaySeason();  
  52.         winter.displaySeason();  
  53.           
  54.     }  
  55.   
  56. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值