2013-01-21 48 views
0

我有一個自定義button,它的範圍是View。在構造函數中,我設置了一個成員變量來保存XML定義中設置的自定義屬性。目前在XML中定義了5個按鈕,每個按鈕都有一個不同的int來表示一個舞臺。具有自定義屬性的Android自定義視圖按鈕引用的是同一對象

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:custom="http://schemas.android.com/apk/res/com.mycompany.stagedApp" 
android:layout_width="match_parent" 
android:orientation="horizontal" 
style="@style/stepFooter"> 

<com.mycompany.stagedApp.StepButtonView 
    android:text="@string/step_one_icon" 
    style="@style/numberedIcon" 
    android:id="@+id/step_one_button" 
    custom:stepNumber="1" 
    android:background="@drawable/circle" /> 
<com.mycompany.stagedApp.StepButtonView 
    android:text="@string/step_two_icon" 
    style="@style/numberedIcon" 
    android:id="@+id/step_two_button" 
    custom:stepNumber="2" 
    android:background="@drawable/circle" /> 
<com.mycompany.stagedApp.StepButtonView 
    android:text="@string/step_three_icon" 
    style="@style/numberedIcon" 
    android:id="@+id/step_three_button" 
    custom:stepNumber="3" 
    android:background="@drawable/circle" /> 
<com.mycompany.stagedApp.StepButtonView 
    android:text="@string/step_four_icon" 
    style="@style/numberedIcon" 
    android:id="@+id/step_four_button" 
    custom:stepNumber="4" 
    android:background="@drawable/circle" /> 
<com.mycompany.stagedApp.StepButtonView 
    android:text="@string/step_five_icon" 
    style="@style/numberedIcon" 
    android:id="@+id/step_five_button" 
    custom:stepNumber="5" 
    android:background="@drawable/circle" /> 
</LinearLayout> 

StepButtonView.java我有:

class StepButtonView extends View { 

    private static int stepNumber; 

    public StepButtonView(Context context, AttributeSet attrs) { 
    super(context, attrs); 

    TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.custom_values); 
    stepNumber = ta.getInt(R.styleable.custom_values_stepNumber, 0); 

    // This log correctly displays the numbers 1 - 5 
    Log.d("StepFromCustom in constructor", String.format("%d",StepButtonView.stepNumber)); 

    this.setOnClickListener(new OnClickListener(){ 
     @Override 
     public void onClick(View view) { 
      Log.d("Step", String.format("%d", view.getId())); 
      // This log only displays the last number 5 
      Log.d("StepFromCustom", String.format("%d",StepButtonView.stepNumber)); 
     } 
    }); 
    } 
} 

activity開始並且每個按鈕被構造1-5記錄的數字。然而點擊記錄的數字總是5,最後的stepNumber。在onClick listener它列出了一個不同的ID值,但相同stepNumber所以我最初認爲它指的是同一個對象必須是不正確的。

任何人都可以解釋發生了什麼,以及爲什麼,因爲我認爲這是一種與我明顯需要清理的語言的誤解。

+1

不使用stepNumber作爲靜態成員 –

+0

可能的重複[靜態變量在Java中的使用](http://stackoverflow.com/questions/10689934/usage-of-static-variables-in-java) –

回答

4

這是因爲您的stepNumberstatic。這意味着它是一個類變量,因此它將保持爲5,因爲它是設置的最後一個值(最後一個構造函數爲Button)。刪除它將解決問題。

讓我們通過應用程序運行:

  • 第一個構造函數:stepNumber是1
  • 第二個構造函數:stepNumber是2
  • 第三個構造函數:stepNumber爲3
  • 第四個構造函數:stepNumber是4.
  • 第五構造函數:stepNumber是5.

這沒有設置了,所以它仍將5.所以後:Log.d()將輸出5

編輯:那之後

  • onClick看到你的評論,你可能意味着一個final屬性,它不能被修改,只能在構造函數中設置。看到你已經在你的構造函數中設置它,它是完美的罰款到添加到您的屬性,使得它:

    private final int stepNumber; 
    

    EDIT2:static成員屬於類,而不是一個具體的實例。

    這意味着只有static字段的一個實例存在即使您創建了一百萬個類的實例或者您不創建任何。它將被所有實例共享。

+0

好的。我認爲每個創建的對象都有它自己的靜態成員變量,它在構造中保存了一個明確的值。我想你是說static關鍵字正在創建一個變量的單個實例,而不是包含在他們自己範圍內的5個不同的實例? – voncox

+0

@voncox。看我的編輯。你需要一個'final'屬性 – stealthjong

+0

謝謝。爲了完整性,你能夠解釋爲什麼它以這種方式工作?具體來說,如果您創建5個不同的對象並在每個對象中設置靜態變量,爲什麼它們是相同的?所有實例中的靜態類變量都是相同的? – voncox

相關問題