枚举类型的理解和应用

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

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

      示例代码如下:

package com.phicomm.hu.eagt;

import android.app.Activity;
import android.gesture.Gesture;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGestureListener;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class EnumAndGestureTest extends Activity implements OnGestureListener
{
	
	private GestureOverlayView mGestureOverlayView;
    private Button mLeftButton, mRightButton;
    private TextView mHeaderMsg;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		//设置ActionBar标题
		getActionBar().setTitle(R.string.draw_gesture);
        
		//获取相关组件对象
		mGestureOverlayView = (GestureOverlayView) findViewById(R.id.gesture);
		mLeftButton = (Button)findViewById(R.id.left);
		mRightButton = (Button)findViewById(R.id.right);
		mHeaderMsg = (TextView)findViewById(R.id.header);
		
		
		//注册手势相关监听器
		mGestureOverlayView.addOnGestureListener(this);
		//mGestureOverlayView.addOnGesturePerformedListener(this);

	}

	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		//程序创建时的绘制标题状态和按钮状态
		setButtunStatus(Stage.Introduction);
		monitorChange(Stage.Introduction);
	}


	// 定义左边按钮各状态的枚举,注:构造函数和成员变量的位置都要放在后面
	private enum LeftButtonMode {
		// 按钮显示为取消、可点击状态的枚举对象(枚举对象意味个人说法)
		Cancel(R.string.cancel, true),
		// 按钮显示为取消、不可点击状态
		CancelDisabled(R.string.cancel, false),
		// 按钮显示为重试、可点击状态
		Retry(R.string.retry, true),
		// 按钮显示为重试、不可点击状态
		RetryDisabled(R.string.retry, false);

		LeftButtonMode(int text, boolean enabled) {
			this.text = text;
			this.enabled = enabled;
		}

		final int text;
		final boolean enabled;
	}

	// 定义右边按钮各状态的枚举
	private enum RightButtonMode {
		Continue(R.string.next, true), 
		ContinueDisabled(R.string.next, false), 
		Confirm(R.string.confirm, true), 
		ConfirmDisabled(R.string.confirm,false);

		RightButtonMode(int text, boolean enabled) {
			this.text = text;
			this.enabled = enabled;
		}

		final int text;
		final boolean enabled;
	}

	//统筹左右按钮状态的枚举,可以理解为枚举的复用(将左右按钮的状态枚举作为实参传递)
	private enum Stage {

		Introduction(R.string.gesture_introdution, LeftButtonMode.Cancel,
				RightButtonMode.Confirm), 			
		Ready(
				R.string.gesture_ready, LeftButtonMode.CancelDisabled,
				RightButtonMode.ConfirmDisabled), 			
		Gesturing(
				R.string.gesture_drawing, LeftButtonMode.RetryDisabled,
				RightButtonMode.ContinueDisabled), 			
		Finish(
				R.string.gesture_finish, LeftButtonMode.Retry,
				RightButtonMode.Continue);

		Stage(int headerMessage, LeftButtonMode leftMode,
				RightButtonMode rightMode) {
			this.headerMessage = headerMessage;
			this.leftMode = leftMode;
			this.rightMode = rightMode;
		}

		final int headerMessage;
		final LeftButtonMode leftMode;
		final RightButtonMode rightMode;
	}

	//该方法的定义重点在于说明枚举类型能在switch语句中的活用(比如,我们可以根据传递的枚举进行监听操作某些事件)
	private void monitorChange(Stage stage)
	{
		switch (stage) {
		case Introduction:
			Toast.makeText(this, "App onResume", Toast.LENGTH_SHORT).show();
			break;
		case Ready:
			Toast.makeText(this, "onGestureStarted", Toast.LENGTH_SHORT).show();
			break;
		case Gesturing:
			Toast.makeText(this, "onGesture", Toast.LENGTH_SHORT).show();
			break;
		case Finish:
			Toast.makeText(this, "onGestureEnded", Toast.LENGTH_SHORT).show();
			break;
		}
	}
	
	//根据传进来的枚举Stage设置和显示按钮的状态
	private void setButtunStatus(Stage stage)
	{
		mHeaderMsg.setText(stage.headerMessage);
		mLeftButton.setText(stage.leftMode.text);
		mLeftButton.setEnabled(stage.leftMode.enabled);
		mRightButton.setText(stage.rightMode.text);
		mRightButton.setEnabled(stage.rightMode.enabled);
	}
	// 正在画手势时调用该函数
	@Override
	public void onGesture(GestureOverlayView overlay, MotionEvent event) {
		// TODO Auto-generated method stub

		//在画手势的过程中,更新按钮的状态
		setButtunStatus(Stage.Gesturing);
		monitorChange(Stage.Gesturing);
	}

	@Override
	public void onGestureCancelled(GestureOverlayView overlay, MotionEvent event) {
		// TODO Auto-generated method stub
	}

	// 画完手势时调用
	@Override
	public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {
		// TODO Auto-generated method stub
		//画完手势更新按钮状态
		setButtunStatus(Stage.Finish);
		monitorChange(Stage.Finish);

	}

	// 开始画手势时调用(即用户手指点住屏幕还没开始滑动时调用)
	@Override
	public void onGestureStarted(GestureOverlayView overlay, MotionEvent event) {
		// TODO Auto-generated method stub
		//准备画手势时更新按钮状态
		setButtunStatus(Stage.Ready);
		monitorChange(Stage.Ready);
	}

}
  Main.xml文件布局如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="20dip"
        android:layout_weight="1"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/header"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dip"
            android:layout_gravity="center" />

        <android.gesture.GestureOverlayView
            android:id="@+id/gesture"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gestureStrokeType="multiple"
            android:gestureStrokeWidth="4" >
        </android.gesture.GestureOverlayView>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="10"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <Button
            android:id="@+id/right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    </LinearLayout>

</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.youkuaiyun.com/detail/stevenhu_223/5560529

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值