Android浣跨敤娉ㄨВ浠f浛鏋氫妇浠庤€岃妭鐪佺郴缁熶娇鐢ㄧ殑鍐呭瓨寮€閿€
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android浣跨敤娉ㄨВ浠f浛鏋氫妇浠庤€岃妭鐪佺郴缁熶娇鐢ㄧ殑鍐呭瓨寮€閿€相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/%e6%9c%80%e5%a4%a7' title='鏈€澶?>鏈€澶?/a> protect 鍐呭瓨 nbsp 缁胯壊 sda 鏁存暟 缃戜笂 interface
銆€銆€Java5浠ュ悗寮€濮嬫敮鎸佹灇涓剧被鍨嬶紝鏋氫妇绫诲瀷浣跨敤璧锋潵闈炲父鏂逛究锛屽叾閲嶈鐨勪綔鐢ㄦ槸浣滀负绫诲瀷瀹夊叏浣跨敤鐨勩€傚鏋滃湪涓嶈€冭檻绯荤粺鍐呭瓨寮€閿€鐨勬儏鍐典笅澶ч噺鐨勪娇鐢ㄦ灇涓句篃涓嶄細鏈変粈涔堥棶棰樸€備絾鏄Щ鍔ㄧ杩樻槸闇€瑕佹敞鎰忕殑銆?/p>
銆€銆€android绯荤粺鍦ㄥ簲鐢ㄥ惎鍔ㄥ悗浼氱粰搴旂敤鍗曠嫭鍒嗛厤涓€鍧楀唴瀛樸€傚簲鐢ㄧ殑dex code銆乭eap浠ュ強杩愯鏃剁殑鍐呭瓨鍒嗛厤閮戒細鍦ㄨ繖鍧楀唴瀛樹腑銆傝€屼娇鐢ㄦ灇涓剧被鍨嬬殑dex size鏄櫘閫氬父浜殑13鍊嶄互涓娿€傚湪杩愯鏃剁殑鍐呭瓨鍒嗛厤锛屼竴涓猠num鐨勫0鏄庤嚦灏戣娑堣€楁帀20涓猙ytes銆備粠杩欎袱鐐瑰氨鍙互璇存槑锛屽湪app涓ぇ閲忎娇鐢ㄦ灇涓剧殑鍐呭瓨寮€閿€鏄潪甯稿ぇ鐨勩€?/p>
銆€銆€鏋氫妇鏈€澶х殑浼樺娍鏄被鍨嬪畨鍏ㄣ€傞偅涔堟槸涓嶆槸鍙堝姙娉曞湪涓嶄娇鐢ㄦ灇涓剧殑鎯呭喌涓嬪仛鍒扮被鍨嬪畨鍏ㄥ憿锛熺瓟妗堟槸鑲畾鐨勶紝涓嬮潰鐪嬬湅鎬庝箞骞层€?/p>
銆€銆€goodle瀹樻柟寰堟棭灏卞彂鐜颁簡鏋氫妇瀵筧ndroid绯荤粺甯︽潵鐨勬€ц兘寮€閿€锛屾墍浠ュ湪鍏跺畼缃戜笂灏卞凡缁忔彁閱掍簡寮€鍙戣€呭敖閲忓皯鐨勪娇鐢ㄦ灇涓撅紝鑰屼笖杩樻彁渚涗簡娉ㄨВ鐨勬柟寮忔潵妫€鏌ョ被鍨嬪畨鍏ㄣ€傜洰鍓嶅叾鎻愪緵浜唅nt鍜宻tring涓ょ娉ㄨВ鏂瑰紡銆傚垎鍒槸锛欼ntDef鍜孲tringDef銆?/p>
銆€銆€璇濅笉澶氳锛岀洿鎺ヤ笂渚嬪瓙銆俻s锛歞emo閲岄潰鏈夎缁嗙殑娉ㄩ噴銆?/p>
涓€銆両nt绫诲瀷娉ㄨВ妫€鏌ョ被鍨嬪畨鍏?/p>
package com.yw.enumproxylib; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import androidx.annotation.IntDef; /** * 瀹氫箟涓€涓猧nt绫诲瀷鐨勬敞瑙? * 鐢ㄦ敞瑙d唬鏇挎灇涓撅紝鍋氬埌绫诲瀷瀹夊叏 * create by yangwei * on 2020-01-30 21:34 */ public class EnumProxy { public static final int READ_CAR = 0;//绾㈣壊鐨勬苯杞?/span> public static final int GREEN_CAR = 1;//缁胯壊鐨勬苯杞?/span> public static final int YELLOW_CAR = 2;//榛勮壊鐨勬苯杞?/span> @IntDef({READ_CAR, GREEN_CAR, YELLOW_CAR}) @Retention(RetentionPolicy.SOURCE)//婧愮爜绾у埆 public @interface CAR_Enum { } /** * 鑾峰彇姹借溅绫诲瀷 * * @param car * @return */ public static int getCar(@CAR_Enum int car) { switch (car) { case READ_CAR: return 0; case GREEN_CAR: return 1; case YELLOW_CAR: return 2; } return -1; } }
浜屻€丼tring绫诲瀷娉ㄨВ妫€鏌ョ被鍨嬪畨鍏?/p>
package com.yw.enumproxylib; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import androidx.annotation.StringDef; /** * 瀹氫箟涓€涓猻tring绫诲瀷鐨勬敞瑙o紝鐢ㄤ簬妫€鏌ユ槦鏈熺殑绫诲瀷瀹夊叏銆? * create by yangwei * on 2020-01-30 22:04 week */ public class StringEnum { public static final String MONDAY = "monday"; public static final String TUESDAY = "tuesday"; public static final String WENDESDAY = "wendesday"; public static final String THURSDAY = "thursday"; public static final String FRIDAY = "friday"; public static final String SATURDAY = "saturday"; public static final String WEEKDAY = "weekday"; @StringDef({MONDAY, TUESDAY, WENDESDAY, THURSDAY, FRIDAY, SATURDAY, WEEKDAY})//姝ゅ瀹氫箟娉ㄨВ鐨勫€煎彧鑳芥槸杩欏嚑涓?/span> @Retention(RetentionPolicy.SOURCE)//缂栬瘧鏃舵敞瑙o紝婧愮爜绾ф鏌?/span> public @interface Week { } /** * 鑾峰彇鏃ユ湡 * * @param week * @return */ public static String getWeek(@Week String week) { switch (week) { case MONDAY: return "鏄熸湡涓€"; case TUESDAY: return "鏄熸湡浜?; case WENDESDAY: return "鏄熸湡涓?; case THURSDAY: return "鏄熸湡鍥?; case FRIDAY: return "鏄熸湡浜?; case SATURDAY: return "鏄熸湡鍏?; case WEEKDAY: return "鏄熸湡鏃?; } return null; } }
涓夈€佺敤鏉ユ鏌ョ被鍨嬪畨鍏ㄧ殑MainActivity锛屼粠涓嬮潰鍙互鐪嬪埌锛屽綋浣跨敤EnumProxy.getCar杈撳叆鍙傛暟鐨勬椂鍊欙紝濡傛灉鐩存帴杈撳叆涓€涓暣鏁板垯缂栬瘧涓嶉€氳繃銆傚綋杈撳叆鎴戜滑瀹氫箟鐨凟numProxy.GREEN_CAR缂栬瘧鎵嶉€氳繃銆?/p>
銆€銆€鍚岀悊StringEnum涔熻兘妫€鏌ョ被鍨嬪畨鍏ㄣ€?/p>
package com.yw.enumproxy; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import com.yw.enumproxylib.EnumProxy; import com.yw.enumproxylib.StringEnum; /** * 娴嬭瘯渚嬪瓙 */ public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); int car_black = 10; int bk = EnumProxy.getCar(car_black);//姝ゅ绫诲瀷妫€鏌ヤ笉閫氳繃 int ck = EnumProxy.getCar(EnumProxy.GREEN_CAR);//绫诲瀷妫€鏌ラ€氳繃 String str = "friday"; String week1 = StringEnum.getWeek(str);//鐩存帴璧嬪€糞tring鏃剁被鍨嬫鏌ヤ笉閫氳繃 String week2 = StringEnum.getWeek(StringEnum.FRIDAY);//璧嬪€煎畾涔夊ソ鐨勫€兼椂绫诲瀷妫€鏌ラ€氳繃 } }
缁撴潫璇細鍏堝啓杩欎箞澶氾紝鏄庡ぉ鍦ㄦ妸demo鍙戝埌github涓娿€傜柅鎯呮湡闂村ぇ瀹朵笉瑕佷贡璺戯紝鍑洪棬璁板緱鎴村彛缃┿€備腑鍥藉姞娌广€佹姹夊姞娌广€?/p>
以上是关于Android浣跨敤娉ㄨВ浠f浛鏋氫妇浠庤€岃妭鐪佺郴缁熶娇鐢ㄧ殑鍐呭瓨寮€閿€的主要内容,如果未能解决你的问题,请参考以下文章