[Android学习笔记]枚举与int的转换

package com.example.enumdemo;



import android.app.Activity;

import android.os.Bundle;

import android.widget.Toast;



public class MainActivity extends Activity {

    

    /**

     * 定义枚举

     */

    public enum MyType {

        // 初始化枚举与int的关系

        NONE(0), TYPE_1(1), TYPE_2(2);



        private int value; MyType(int value) { this.value = value; }

    }



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



        // 枚举转int

        MyType type = MyType.TYPE_1;

        Toast.makeText(MainActivity.this,

                "MyType.TYPE_1 对应的int值:" + type.value, Toast.LENGTH_SHORT)

                .show();



        // int转枚举

        int typeValue = 2;

        Toast.makeText(MainActivity.this,

                "int值对应的枚举类型:" + MyType.values()[typeValue].toString(),

                Toast.LENGTH_SHORT).show();

    }

}

你可能感兴趣的:(Android学习)