導航:首頁 > 操作系統 > androidenumint

androidenumint

發布時間:2022-12-28 18:03:22

A. android自定義控制項怎麼用

開發自定義控制項的步驟:
1、了解View的工作原理
2、 編寫繼承自View的子類
3、 為自定義View類增加屬性
4、 繪制控制項
5、 響應用戶消息
6 、自定義回調函數

一、View結構原理
Android系統的視圖結構的設計也採用了組合模式,即View作為所有圖形的基類,Viewgroup對View繼承擴展為視圖容器類。
View定義了繪圖的基本操作
基本操作由三個函數完成:measure()、layout()、draw(),其內部又分別包含了onMeasure()、onLayout()、onDraw()三個子方法。具體操作如下:
1、measure操作
measure操作主要用於計算視圖的大小,即視圖的寬度和長度。在view中定義為final類型,要求子類不能修改。measure()函數中又會調用下面的函數:
(1)onMeasure(),視圖大小的將在這里最終確定,也就是說measure只是對onMeasure的一個包裝,子類可以覆寫onMeasure()方法實現自己的計算視圖大小的方式,並通過setMeasuredDimension(width, height)保存計算結果。

2、layout操作
layout操作用於設置視圖在屏幕中顯示的位置。在view中定義為final類型,要求子類不能修改。layout()函數中有兩個基本操作:
(1)setFrame(l,t,r,b),l,t,r,b即子視圖在父視圖中的具體位置,該函數用於將這些參數保存起來;
(2)onLayout(),在View中這個函數什麼都不會做,提供該函數主要是為viewGroup類型布局子視圖用的;

3、draw操作
draw操作利用前兩部得到的參數,將視圖顯示在屏幕上,到這里也就完成了整個的視圖繪制工作。子類也不應該修改該方法,因為其內部定義了繪圖的基本操作:
(1)繪制背景;
(2)如果要視圖顯示漸變框,這里會做一些准備工作;
(3)繪制視圖本身,即調用onDraw()函數。在view中onDraw()是個空函數,也就是說具體的視圖都要覆寫該函數來實現自己的顯示(比如TextView在這里實現了繪制文字的過程)。而對於ViewGroup則不需要實現該函數,因為作為容器是「沒有內容「的,其包含了多個子view,而子View已經實現了自己的繪制方法,因此只需要告訴子view繪制自己就可以了,也就是下面的dispatchDraw()方法;
(4)繪制子視圖,即dispatchDraw()函數。在view中這是個空函數,具體的視圖不需要實現該方法,它是專門為容器類准備的,也就是容器類必須實現該方法;
(5)如果需要(應用程序調用了setVerticalFadingEdge或者setHorizontalFadingEdge),開始繪制漸變框;
(6)繪制滾動條;
從上面可以看出自定義View需要最少覆寫onMeasure()和onDraw()兩個方法。

二、View類的構造方法
創建自定義控制項的3種主要實現方式:
1)繼承已有的控制項來實現自定義控制項: 主要是當要實現的控制項和已有的控制項在很多方面比較類似, 通過對已有控制項的擴展來滿足要求。
2)通過繼承一個布局文件實現自定義控制項,一般來說做組合控制項時可以通過這個方式來實現。
注意此時不用onDraw方法,在構造廣告中通過inflater載入自定義控制項的布局文件,再addView(view),自定義控制項的圖形界面就載入進來了。
3)通過繼承view類來實現自定義控制項,使用GDI繪制出組件界面,一般無法通過上述兩種方式來實現時用該方式。

三、自定義View增加屬性的兩種方法:
1)在View類中定義。通過構造函數中引入的AttributeSet 去查找XML布局的屬性名稱,然後找到它對應引用的資源ID去找值。
案例:實現一個帶文字的圖片(圖片、文字是onDraw方法重繪實現)

public class MyView extends View {

private String mtext;
private int msrc;

public MyView(Context context) {
super(context);
}

public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
int resourceId = 0;
int textId = attrs.getAttributeResourceValue(null, "Text",0);
int srcId = attrs.getAttributeResourceValue(null, "Src", 0);
mtext = context.getResources().getText(textId).toString();
msrc = srcId;
}

@Override
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
paint.setColor(Color.RED);
InputStream is = getResources().openRawResource(msrc);
Bitmap mBitmap = BitmapFactory.decodeStream(is);
int bh = mBitmap.getHeight();
int bw = mBitmap.getWidth();
canvas.drawBitmap(mBitmap, 0,0, paint);
//canvas.drawCircle(40, 90, 15, paint);
canvas.drawText(mtext, bw/2, 30, paint);
}
}

布局文件:

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

<com.example.myimageview2.MyView
android:id="@+id/myView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Text="@string/hello_world"
Src="@drawable/xh"/>

</LinearLayout>

屬性Text, Src在自定義View類的構造方法中讀取。

2)通過XML為View注冊屬性。與Android提供的標准屬性寫法一樣。
案例: 實現一個帶文字說明的ImageView (ImageView+TextView組合,文字說明,可在布局文件中設置位置)

public class MyImageView extends LinearLayout {

public MyImageView(Context context) {
super(context);
}

public MyImageView(Context context, AttributeSet attrs) {
super(context, attrs);
int resourceId = -1;
TypedArray typedArray = context.obtainStyledAttributes(attrs,
R.styleable.MyImageView);
ImageView iv = new ImageView(context);
TextView tv = new TextView(context);
int N = typedArray.getIndexCount();
for (int i = 0; i < N; i++) {
int attr = typedArray.getIndex(i);
switch (attr) {
case R.styleable.MyImageView_Oriental:
resourceId = typedArray.getInt(
R.styleable.MyImageView_Oriental, 0);
this.setOrientation(resourceId == 1 ? LinearLayout.HORIZONTAL
: LinearLayout.VERTICAL);
break;
case R.styleable.MyImageView_Text:
resourceId = typedArray.getResourceId(
R.styleable.MyImageView_Text, 0);
tv.setText(resourceId > 0 ? typedArray.getResources().getText(
resourceId) : typedArray
.getString(R.styleable.MyImageView_Text));
break;
case R.styleable.MyImageView_Src:
resourceId = typedArray.getResourceId(
R.styleable.MyImageView_Src, 0);
iv.setImageResource(resourceId > 0 ?resourceId:R.drawable.ic_launcher);
break;
}
}
addView(iv);
addView(tv);
typedArray.recycle();
}
}

attrs.xml進行屬性聲明, 文件放在values目錄下

<?xml version="1.0" encoding="utf-8"?>
<resources>

<declare-styleable name="MyImageView">
<attr name="Text" format="reference|string"></attr>
<attr name="Oriental" >
<enum name="Horizontal" value="1"></enum>
<enum name="Vertical" value="0"></enum>
</attr>
<attr name="Src" format="reference|integer"></attr>
</declare-styleable>

</resources>

MainActivity的布局文件:先定義命名空間 xmlns:uview="http://schemas.android.com/apk/res/com.example.myimageview2" (com.example.myimageview2為你
在manifest中定義的包名)
然後可以像使用系統的屬性一樣使用:uview:Oriental="Vertical"

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:uview="http://schemas.android.com/apk/res/com.example.myimageview2"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />

<com.example.myimageview2.MyImageView
android:id="@+id/myImageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
uview:Text="這是一個圖片說明"
uview:Src="@drawable/tw"
uview:Oriental="Vertical">
</com.example.myimageview2.MyImageView>

</LinearLayout>

四、控制項繪制 onDraw()

五、
六:自定義View的方法
onFinishInflate() 回調方法,當應用從XML載入該組件並用它構建界面之後調用的方法
onMeasure() 檢測View組件及其子組件的大小
onLayout() 當該組件需要分配其子組件的位置、大小時
onSizeChange() 當該組件的大小被改變時
onDraw() 當組件將要繪制它的內容時
onKeyDown 當按下某個鍵盤時
onKeyUp 當松開某個鍵盤時
onTrackballEvent 當發生軌跡球事件時
onTouchEvent 當發生觸屏事件時
onWindowFocusChanged(boolean) 當該組件得到、失去焦點時
onAtrrachedToWindow() 當把該組件放入到某個窗口時
onDetachedFromWindow() 當把該組件從某個窗口上分離時觸發的方法
onWindowVisibilityChanged(int): 當包含該組件的窗口的可見性發生改變時觸發的方法

B. Android 怎麼自定義枚舉類型的值

在已知可能輸入值情況下,我們常常會用到枚舉類型。在java中,怎麼自定義枚舉類型的值呢?請參考如下代碼:

[java] view plain
public enum Point {
Satisfaction(1), Dissatisfied(-1);
private final int val;

private Point(int value) {
val = value;
}

public int getValue() {
return this.val;
}
}

在調用的時候,可以用Point.Satisfaction 和Point.Dissatisfied,如下所示:

[java] view plain
Judge(mContenxt, mGuid, Point.Dissatisfied);

在方法Judge中,可以用getValue獲取枚舉的值。如下所示:
[java] view plain
public void Judge(Context context,String logGuid, Point point){
int point = point.getValue();

}

C. 為什麼說android使用enum效率低,浪費方法數

現在不會了,新版本JDK做了優化,效率高了很多,而且使用也變方便了。

D. android 怎麼封裝jni

一、底層實現:

c文件:hardware/libhardware_legacy/power/power.c

以其中set_screen_state(int)函數為例

其Android.mk中添加:
LOCAL_MODULE:= libpower 編譯成lib
LOCAL_SRC_FILES += power.c

hardware/libhardware_legacy/power/power.c
1: int
2: set_screen_state(int on)
3: {
4: QEMU_FALLBACK(set_screen_state(on));
5:
6: LOGI("*** set_screen_state %d", on);
7:
8: initialize_fds();
9:
10: //LOGI("go_to_sleep eventTime=%lld now=%lld g_error=%s\n", eventTime,
11: // systemTime(), strerror(g_error));
12:
13: if (g_error)
14: goto failure;
15:
16: char buf[32];
17: int len;
18: if(on)
19: len = snprintf(buf, sizeof(buf), "%s", on_state);
20: else
21: len = snprintf(buf, sizeof(buf), "%s", off_state);
22:
23: buf[sizeof(buf) - 1] = '\0';
24: len = write(g_fds[REQUEST_STATE], buf, len);
25: if(len < 0) {
26: failure:
27: LOGE("Failed setting last user activity: g_error=%d\n", g_error);
28: }
29: return 0;
30: }

其頭文件power.h中:
1: #if__cplusplus
2: extern "C" { //注1
3: #endif
4: enum {
5: PARTIAL_WAKE_LOCK = 1, // the cpu stays on, but the screen is off
6: FULL_WAKE_LOCK = 2 // the screen is also on
7: };
8:
9: // while you have a lock held, the device will stay on at least at the
10: // level you request.
11: int acquire_wake_lock(int lock, const char* id);
12: int release_wake_lock(const char* id);
13:
14: // true if you want the screen on, false if you want it off
15: int set_screen_state(int on);
16:
17: // set how long to stay awake after the last user activity in seconds
18: int set_last_user_activity_timeout(int64_t delay);
19:
20:
21: #if __cplusplus
22: } // extern "C"
23: #endif

注1:

注1:extern表示其他的類已經定義了這段代碼裡面的內容,這里只是做聲明。
"C」表示的一種編譯和連接規約,這里為下一步c++調用其做准備.
比如void foo(int,int);該函數被C編譯器編譯後在庫中的名字為_foo,
而C++編譯器則會產生像_foo_int_int之類的名字用來支持函數重載和類型安全連接。
由於編譯後的名字不同,C++程序不能直接調用C函數。
因此C++提供了一個C連接交換指定符號extern「C」來解決這個問題而不是一種語言。
C表示這段代碼可以是符合C語言的編譯和連接規約的任何語言,如Fortran、assembler等。

二、cpp構成jni橋梁

一個CPP文件調用之,則需添加其頭文件,比如frameworks/base/core/jni/android_os_Power.cpp.
1: #include "JNIHelp.h"
2: #include "jni.h"
3: #include "android_runtime/AndroidRuntime.h"
4: #include <hardware_legacy/power.h>
5: namespace android{
6: ....
7:
8: //定義函數:
9: static int setScreenState(JNIEnv *env, jobject clazz, jboolean on)
10: {
11: return set_screen_state(on);//以此實現cpp到c的調用
12: }
13:
14: static JNINativeMethod method_table[] = {//此處實現java對cpp的調用轉化 注2
15: { "acquireWakeLock", "(ILjava/lang/String;)V", (void*)acquireWakeLock },
16: { "releaseWakeLock", "(Ljava/lang/String;)V", (void*)releaseWakeLock },
17: { "setLastUserActivityTimeout", "(J)I", (void*)setLastUserActivityTimeout },
18: { "setScreenState", "(Z)I", (void*)setScreenState },
19: { "shutdown", "()V", (void*)android_os_Power_shutdown },
20: { "rebootNative", "(Ljava/lang/String;)V", (void*)android_os_Power_reboot },
21: };
22: int register_android_os_Power(JNIEnv *env) //此處注冊jni
23: { //向VM(即AndroidRuntime)登記 gMethods[]表格所含的本地函數
24: return AndroidRuntime::registerNativeMethods(
25: env, "android/os/Power",
26: method_table, NELEM(method_table));
27: }
28: };
注2:

typedef struct {
const char* name; //Java中函數的名字
const char* signature; //用字元串是描述了函數的參數和返回值
void* fnPtr; //函數指針,指向C函數
} JNINativeMethod;
其中比較難以理解的是第二個參數,例如
"()V"
"(II)V"
"(Ljava/lang/String;Ljava/lang/String;)V"
實際上這些字元是與函數的參數類型一一對應的。
"()" 中的字元表示參數,後面的則代表返回值。例如"()V" 就表示void Func();
"(II)V" 表示 void Func(int, int);
具體的每一個字元的對應關系如下
字元 Java類型 C類型
V void void
Z jboolean boolean
I jint int
J jlong long
D jdouble double
F jfloat float
B jbyte byte
C jchar char
S jshort short
數組則以"["開始,用兩個字元表示
[I jintArray int[]
[F jfloatArray float[]
[B jbyteArray byte[]
[C jcharArray char[]
[S jshortArray short[]
[D jdoubleArray double[]
[J jlongArray long[]
[Z jbooleanArray boolean[]
上面的都是基本類型。如果Java函數的參數是class,則以"L"開頭,以";"結尾中間是用"/" 隔開的包及類名。而其對應的C函數名的參數則為jobject. 一個例外是String類,其對應的類為jstring
Ljava/lang/String; String jstring
Ljava/net/Socket; Socket jobject
如果JAVA函數位於一個嵌入類,則用$作為類名間的分隔符。
例如 "(Ljava/lang/String;Landroid/os/FileUtils$FileStatus;)Z"

三、java的封裝實現

frameworks/base/core/java/android/os/Power.java //此處路徑跟cpp中注冊jni處的路徑是一致的.待細研究是否有關系
1: package android.os;
2: public class Power
3: {
4: ...
5: public static native int setScreenState(boolean on); //被native修飾的表示調用了非java語言的本地方法
6: ...
7: }

四、java中對其調用

frameworks/base/services/java/com/android/server/PowerManagerService.java
import android.os.Power;
public class PowerManagerService extends IPowerManager.Stub
implements LocalPowerManager, Watchdog.Monitor {
...
int err = Power.setScreenState(on);
...
}

E. android中怎麼在View構造的attrs中拿到android給的屬性

//Android原生的屬性,都是提供方法可以獲得的,當然也可以通過
attrs獲得,而自定義的屬性獲得值方式如下,當然原生的也是一樣,只需要把attr name該成系統的。

一、 首先要在res/values目錄下建立一個attrs.xml(名字可以自己定義)的文件,並在此文件中增加對控制項的屬性的定義.其xml文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="my_text_view">
<attr name="text_size" format="float"></attr>
<attr name="text_color" format="color"></attr>
<attr name="text_back_ground" format="color|reference"></attr>

</declare-styleable>
</resources>

在這里,需要補充attrs屬性的相關知識,即Attr屬性是如何在XML中定義的,自定義屬性的Value值可以有10種類型以及其類型的組合值,其具體使用方法如下:

1. reference:參考某一資源ID。

(1)屬性定義:

<declare-styleable name = "名稱">

<attr name = "background" format = "reference" />

</declare-styleable>

(2)屬性使用:

<ImageView

android:layout_width = "42dip"

android:layout_height = "42dip"

android:background = "@drawable/圖片ID"

/>

2. color:顏色值。

(1)屬性定義:

<declare-styleable name = "名稱">

<attr name = "textColor" format = "color" />

</declare-styleable>

(2)屬性使用:

<TextView

android:layout_width = "42dip"

android:layout_height = "42dip"

android:textColor = "#00FF00"

/>

3. boolean:布爾值。

(1)屬性定義:

<declare-styleable name = "名稱">

<attr name = "focusable" format = "boolean" />

</declare-styleable>

(2)屬性使用:

<Button

android:layout_width = "42dip"

android:layout_height = "42dip"

android:focusable = "true"

/>

4. dimension:尺寸值。

(1)屬性定義:

<declare-styleable name = "名稱">

<attr name = "layout_width" format = "dimension" />

</declare-styleable>

(2)屬性使用:

<Button

android:layout_width = "42dip"

android:layout_height = "42dip"

/>

5. float:浮點值。

(1)屬性定義:

<declare-styleable name = "AlphaAnimation">

<attr name = "fromAlpha" format = "float" />

<attr name = "toAlpha" format = "float" />

</declare-styleable>

(2)屬性使用:

<alpha

android:fromAlpha = "1.0"

android:toAlpha = "0.7"

/>

6. integer:整型值。

(1)屬性定義:

<declare-styleable name = "AnimatedRotateDrawable">

<attr name = "visible" />

<attr name = "frameDuration" format="integer" />

<attr name = "framesCount" format="integer" />

<attr name = "pivotX" />

<attr name = "pivotY" />

<attr name = "drawable" />

</declare-styleable>

(2)屬性使用:

<animated-rotate

xmlns:android = "http://schemas.android.com/apk/res/android"

android:drawable = "@drawable/圖片ID"

android:pivotX = "50%"

android:pivotY = "50%"

android:framesCount = "12"

android:frameDuration = "100"

/>

7. string:字元串。

(1)屬性定義:

<declare-styleable name = "MapView">

<attr name = "apiKey" format = "string" />

</declare-styleable>

(2)屬性使用:

<com.google.android.maps.MapView

android:layout_width = "fill_parent"

android:layout_height = "fill_parent"

android:apiKey = "_bc_g"

/>

8. fraction:百分數。

(1)屬性定義:

<declare-styleable name="RotateDrawable">

<attr name = "visible" />

<attr name = "fromDegrees" format = "float" />

<attr name = "toDegrees" format = "float" />

<attr name = "pivotX" format = "fraction" />

<attr name = "pivotY" format = "fraction" />

<attr name = "drawable" />

</declare-styleable>

(2)屬性使用:

<rotate

xmlns:android = "http://schemas.android.com/apk/res/android"

android:interpolator = "@anim/動畫ID"

android:fromDegrees = "0"

android:toDegrees = "360"

android:pivotX = "200%"

android:pivotY = "300%"

android:ration = "5000"

android:repeatMode = "restart"

android:repeatCount = "infinite"

/>

9. enum:枚舉值。

(1)屬性定義:

<declare-styleable name="名稱">

<attr name="orientation">

<enum name="horizontal" value="0" />

<enum name="vertical" value="1" />

</attr>

</declare-styleable>

(2)屬性使用:

<LinearLayout

xmlns:android = "http://schemas.android.com/apk/res/android"

android:orientation = "vertical"

android:layout_width = "fill_parent"

android:layout_height = "fill_parent"

>

</LinearLayout>

10. flag:位或運算。

(1)屬性定義:

<declare-styleable name="名稱">

<attr name="windowSoftInputMode">

<flag name = "stateUnspecified" value = "0" />

<flag name = "stateUnchanged" value = "1" />

<flag name = "stateHidden" value = "2" />

<flag name = "stateAlwaysHidden" value = "3" />

<flag name = "stateVisible" value = "4" />

<flag name = "stateAlwaysVisible" value = "5" />

<flag name = "adjustUnspecified" value = "0x00" />

<flag name = "adjustResize" value = "0x10" />

<flag name = "adjustPan" value = "0x20" />

<flag name = "adjustNothing" value = "0x30" />

</attr>

</declare-styleable>

(2)屬性使用:

<activity

android:name = ".StyleAndThemeActivity"

android:label = "@string/app_name"

android:windowSoftInputMode = "stateUnspecified | stateUnchanged|stateHidden">

<intent-filter>

<action android:name = "android.intent.action.MAIN" />

<category android:name = "android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

注意:

屬性定義時可以指定多種類型值。

(1)屬性定義:

<declare-styleable name = "名稱">

<attr name = "background" format = "reference|color" />

</declare-styleable>

(2)屬性使用:

<ImageView

android:layout_width = "42dip"

android:layout_height = "42dip"

android:background = "@drawable/圖片ID|#00FF00"

/>

二、接下來實現自定義View的類,其中下面的構造方法是重點,在代碼中獲取自定義屬性,其代碼如下:

package com.example.CustomAttr;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.TextView;

/**
* Created with IntelliJ IDEA.
* User: wen.nan
* Date: 13-9-28
* Time: 下午10:00
* To change this template use File | Settings | File Templates.
*/
public class CustomTextView extends TextView {
private TypedArray mTypedArray;
private Paint mPaint;

public CustomTextView(Context context) {
super(context);
}

public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
inial(context,attrs);
}

public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
inial(context,attrs);
}

private void inial(Context context,AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.my_text_view);

float textsize = typedArray.getFloat(R.styleable.my_text_view_text_size,14) ;
int textColor = typedArray.getColor(R.styleable.my_text_view_text_color,0xFFFFFF) ;
int bgColor = typedArray.getColor(R.styleable.my_text_view_text_back_ground,0xFFFFFF) ;

super.setTextColor(textColor);
super.setTextSize(textsize);
super.setBackgroundColor(bgColor);

typedArray.recycle();

}

}

三、接下來在XML布局中引用自定義View控制項,其XML代碼如下:
?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app = "http://schemas.android.com/apk/res/com.example.CustomAttr"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<com.example.CustomAttr.CustomTextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, MainActivity"
app:text_size ="20"
app:text_color ="#00FF00"
app:text_back_ground="#ffffff"

/>
</LinearLayout>

注意上面XML中代碼: xmlns:app = "http://schemas.android.com/apk/res/com.example.CustomAttr",是自定義的app命名空間,res後面是應用程序包名,然後可以直接使用app:text_size,等屬性,其值類型要和attrs.xml定義的屬性Value值相對應。
四、總結:
注意該例子中是使用app:text_size = "20 和app:text_color="#00FF00定義TextView的顏色和textView的字體大小,而不是使用系統的屬性android:textsize等。該例子中只是起到拋磚引玉的作用,你可以自定義其他屬性,來實現你想要的自定義View效果。

F. android intent 可以傳遞enum 嗎

可以使用序列化對象保存enum對象,如下內容:
bundle.putSerializable(String key , Seralizable data) //向Bundle放入一個可序列化的對象,例如:enum123123

示例:
Intent intent = new Intent(MainActivity.this,OtherActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("enum", YourEnum.TYPE1);
intent.putExtras(bundle); //將bundle傳入intent中。12341234

get
bundle.getXxx(String key);//從Bundle取出Int、String等各種類型的數據
bundle.gutSerializable(String key ) //從Bundle取出一個可序列化的對象,例如:enum1212

示例:
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
YourEnum TYPE1 = (YourEnum ) bundle.get("enum");//這時使用get()取出一個Object類型的對象,可以進行強制類型轉化。

G. 在Android中要實現圖表統計該怎麼做

package com.yzxy.draw;

import java.util.ArrayList;
import java.util.HashMap;
import com.yzxy.draw.tools.Tools;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.RectF;
import android.graphics.Paint.Style;
import android.graphics.Typeface;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

@SuppressLint("ViewConstructor")
class MyTuView extends View{

public static final int RECT_SIZE = 10;
private Point mSelectedPoint = null;
public static enum Mstyle
{
Line,scroll
}

private Mstyle mstyle=Mstyle.Line;
private Point[] mPoints = new Point[8];

Context context;
Activity act;
int bheight=0;
Tools tool=new Tools();
HashMap map;
ArrayList dlk;
int totalvalue=30;
int pjvalue=5;
String xstr,ystr;
int margint=15;
int marginb=40;
int c=0;
int resid=0;
Boolean isylineshow;

public MyTuView(Context context,HashMap map,int totalvalue,int pjvalue,String xstr,String ystr,Boolean isylineshow)
{
super(context);
this.context=context;
this.act = (Activity)context;
this.map=map;
this.totalvalue=totalvalue;
this.pjvalue=pjvalue;
this.xstr=xstr;
this.ystr=ystr;
this.isylineshow=isylineshow;
act.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}

@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// canvas.drawColor(Color.GRAY);
if(c!=0)
this.setbg(c);
if(resid!=0)
this.setBackgroundResource(resid);
dlk=tool.getintfrommap(map);
int height=getHeight();
if(bheight==0)
bheight=height-marginb;

int width=getWidth();

Log.i("w", getWidth()+":"+getHeight());
int blwidh=tool.dip2px(context,50);
int pjsize=totalvalue/pjvalue;

Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.GRAY);
paint.setStrokeWidth(1);
paint.setStyle(Style.STROKE);

// 畫直線(橫向)
for(int i=0;i xlist=new ArrayList();//記錄每個x的值
//畫直線(縱向)
for(int i=0;i dlk,HashMap map,ArrayList xlist,int max,int h)
{
Point[] points=new Point[dlk.size()];
for(int i=0;i getMap() {
return map;
}

public void setMap(HashMap map) {
this.map = map;
}

public int getTotalvalue() {
return totalvalue;
}

public void setTotalvalue(int totalvalue) {
this.totalvalue = totalvalue;
}

public int getPjvalue() {
return pjvalue;
}

public void setPjvalue(int pjvalue) {
this.pjvalue = pjvalue;
}

public String getXstr() {
return xstr;
}

public void setXstr(String xstr) {
this.xstr = xstr;
}

public String getYstr() {
return ystr;
}

public void setYstr(String ystr) {
this.ystr = ystr;
}

public int getMargint() {
return margint;
}

public void setMargint(int margint) {
this.margint = margint;
}

public Boolean getIsylineshow() {
return isylineshow;
}

public void setIsylineshow(Boolean isylineshow) {
this.isylineshow = isylineshow;
}

public int getMarginb() {
return marginb;
}

public void setMarginb(int marginb) {
this.marginb = marginb;
}

public Mstyle getMstyle() {
return mstyle;
}

public void setMstyle(Mstyle mstyle) {
this.mstyle = mstyle;
}

public int getBheight() {
return bheight;
}

public void setBheight(int bheight) {
this.bheight = bheight;
}

public int getC() {
return c;
}

public void setC(int c) {
this.c = c;
}

public int getResid() {
return resid;
}

public void setResid(int resid) {
this.resid = resid;
}

}

代碼片段
package com.yzxy.draw.tools;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Environment;

public class Tools
{
private final static String ALBUM_PATH = Environment.getExternalStorageDirectory() + "/yueqiu/";

public int dip2px(Context context, float dpValue)
{
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}

public int px2dip(Context context, float pxValue)
{
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}

public ArrayList getintfrommap(HashMap map)
{
ArrayList dlk=new ArrayList();
int position=0;
@SuppressWarnings("rawtypes")
Set set= map.entrySet();
@SuppressWarnings("rawtypes")
Iterator iterator = set.iterator();

while(iterator.hasNext())
{
@SuppressWarnings("rawtypes")
Map.Entry mapentry = (Map.Entry)iterator.next();
dlk.add((Double)mapentry.getKey());
}
for(int i=0;i

代碼片段
package com.yzxy.draw;

import java.io.IOException;
import java.util.HashMap;

import com.yzxy.draw.MyTuView.Mstyle;
import com.yzxy.draw.tools.Tools;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;

import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.Toast;
import android.app.Activity;
import android.content.res.XmlResourceParser;
import android.graphics.Bitmap;
import android.graphics.Color;

public class MainActivity extends Activity {

MyTuView tu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

HashMap map=new HashMap();
map.put(1.0, 8.0);
map.put(7.0, 130.0);
map.put(2.0, 4.0);
map.put(3.0, 1.0);
map.put(4.0, 18.0);
map.put(5.0, 160.0);
map.put(6.0, 180.0);
map.put(8.1, 133.5);
tu=new MyTuView(this,map,200,50,"x","y",false);
// tu.setC(Color.CYAN);
tu.setResid(R.drawable.bg);
// tu.setBheight(200);
tu.setTotalvalue(200);

tu.setPjvalue(50);
tu.setXstr("x");
tu.setYstr("y");
tu.setMargint(20);
tu.setBackgroundColor(Color.WHITE);
tu.setMarginb(50);
tu.setMstyle(Mstyle.scroll);
RelativeLayout rela=getlayout(R.layout.activity_main);
rela.addView(tu);
LayoutParams parm=new LayoutParams(1200,400);
parm.setMargins(50, 50, 50, 100);
tu.setLayoutParams(parm);
setContentView(rela);

}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// TODO Auto-generated method stub
if(item.getItemId() == R.id.menu_settings)
{
if (false == tu.isDrawingCacheEnabled())
{
tu.setDrawingCacheEnabled(true);
}
Bitmap bitmap = tu.getDrawingCache();
Tools tool=new Tools();
try {
Boolean b=tool.saveFile(bitmap, "aaaa.png");
if(b)
Toast.makeText(this, "success", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(item.getItemId() == R.id.menu_ch)
{
HashMap map=new HashMap();
map.put(1.0, 21.0);
map.put(3.0, 25.0);
map.put(4.0, 32.0);
map.put(5.0, 31.0);
map.put(6.0, 26.0);

tu.setTotalvalue(40);
tu.setPjvalue(10);
tu.setMap(map);
tu.setIsylineshow(true);
tu.postInvalidate();
}
if(item.getItemId() == R.id.menu_ch2)
{
HashMap map=new HashMap();
map.put(1.0, 41.0);
map.put(3.0, 25.0);
map.put(4.0, 32.0);
map.put(5.0, 41.0);
map.put(6.0, 16.0);
map.put(7.0, 36.0);
map.put(8.0, 26.0);
tu.setTotalvalue(50);
tu.setPjvalue(10);
tu.setMap(map);
tu.setMstyle(Mstyle.Line);
tu.setIsylineshow(false);
tu.postInvalidate();
}
return true;
}

public RelativeLayout getlayout(int r)
{
LayoutInflater inflater = (LayoutInflater)getSystemService(android.content.Context.LAYOUT_INFLATER_SERVICE );
try {
XmlResourceParser parser = getResources().getLayout(r);
RelativeLayout layout = (RelativeLayout) inflater.inflate(parser, null);
return layout;
}catch (Exception e) {
// TODO: handle exception
}
return null;
}
}

閱讀全文

與androidenumint相關的資料

熱點內容
吉利車解壓 瀏覽:248
java輸入流字元串 瀏覽:341
安卓軟體沒網怎麼回事 瀏覽:785
dvd壓縮碟怎麼導出電腦 瀏覽:274
冒險島什麼伺服器好玩 瀏覽:541
如何在伺服器上做性能測試 瀏覽:793
命令序列錯 瀏覽:259
javaif的條件表達式 瀏覽:576
手機app上傳的照片怎麼找 瀏覽:531
雲伺服器面臨哪些威脅 瀏覽:748
c語言各種編譯特點 瀏覽:177
路由器多種加密方法 瀏覽:604
程序員阻止電腦自動彈出定位 瀏覽:168
如何做伺服器服務商 瀏覽:761
su剖切命令 瀏覽:726
devc編譯背景 瀏覽:211
學習單片機的意義 瀏覽:51
音頻演算法AEC 瀏覽:911
加密貨幣容易被盜 瀏覽:82
蘋果平板如何開啟隱私單個app 瀏覽:704