2012-06-10 200 views
0

我在這裏是一個自定義視圖,然後我想要一些小部件去與它。自定義視圖按鈕

<com.zone.manager.Tab3 
    android:id="@+id/tab3_display" 
    android:layout_width="fill_parent" 
    android:layout_height="620dp" > 

<Button 
    android:id="@+id/addZone" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Add Zone" /> 

</com.zone.manager.Tab3> 

現在我想有一個按鈕設置爲onclicklistener,所以我可以做的東西與它在視圖類,所以我做了這個......

addZone = (Button) findViewById(R.id.addZone); 


    addZone.setOnClickListener(this); 

我設置的是,在

public class Tab3 extends ViewGroup implements OnTouchListener, OnClickListener 
    Public Tab3(Context context, AttributeSet attrs) 
    { 
    super (context, attrs); 
    // here 
    } 

當我伸出的ViewGroup它讓我實現這個

@Override 
    protected void onLayout(boolean changed, int l, int t, int r, int b) 
    { 
     // TODO Auto-generated method stub 

    } 

有什麼,我suppsot爲了使這項工作放在這裏?

但是,當我嘗試運行應用程序崩潰,但是如果我// addZone.setOnClickListener(this);應用程序運行良好,對我有幫助嗎?

標籤

 th.setup(); 
     TabSpec specs = th.newTabSpec("tag0"); 
     specs.setContent(R.id.connecttionTab); 
     specs.setIndicator("Connection Tab"); 
     th.addTab(specs); 
     specs = th.newTabSpec("tag1"); 
     specs.setContent(R.id.tab1); 
     specs.setIndicator("Zone Manager"); 
     th.addTab(specs); 
     specs = th.newTabSpec("tag2"); 
     specs.setContent(R.id.tab2); 
     specs.setIndicator("",res.getDrawable(R.drawable.ic_tab_vaccontrol)); 
     th.addTab(specs); 
     //this is the tab that has all this vvv 
     specs = th.newTabSpec("tag3"); 
     specs.setContent(R.id.tab3); 
     specs.setIndicator("Graphical Layout"); 
     th.addTab(specs); 
+0

當您更改爲ViewGroup時,您是否還更改了xml,以便Button是嵌套在Tab3元素內的子元素?如果它只是像你發佈的xml一樣,它仍然不會被'findViewById'找到。而且,是的,您需要實現'onLayout'來決定每個子元素(在這種情況下,只是按鈕)應該放在父Tab3元素的範圍內。 –

+0

IM抱歉,但我不明白這是什麼意思。你能幫我解釋一下嗎? –

+0

在我的答案中查看我的第一個示例xml代碼。請注意,第一個標籤不會結束'Tab3'元素。 (它以'>'而不是'/>'結尾,'Button'標籤後面有一個''標籤。)'Button'元素是一個孩子,而不是'Tab3'的兄弟姐妹。 –

回答

2

在你的XML,您的自定義視圖不包含按鈕;這是兄弟姐妹的觀點。您的應用程序崩潰的原因是findViewById(R.id.addZone)正在返回null,因此當您撥打addZone.setOnClickListener(this)時,您會收到NullPointerException。如果您希望自定義視圖包含按鈕,XML必須是這個樣子:

<com.zone.manager.Tab3 
    android:id="@+id/tab3_display" 
    android:layout_width="fill_parent" 
    android:layout_height="620dp" > 

    <Button 
     android:id="@+id/addZone" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Add Zone" /> 

</com.zone.manager.Tab3> 

此外,您Tab3類將不得不延長ViewGroup,不View。這可能會有點複雜,因爲您還需要編寫代碼來完成佈局。該按鈕也將顯示在Tab3視圖內。

編輯

根據你的你正在嘗試做的意見,我不建議以上做法。相反,您應該將自定義視圖和按鈕包裝在LinearLayout中。例如,下面的佈局將會把按鈕在屏幕的左下方,並且將有一個TAB3視圖填充它上面的區域:

RES/layout.tab3.xml

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <com.zone.manager.Tab3 
     android:id="@+id/tab3_display" 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" /> 

    <Button 
     android:id="@+id/addZone" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="onAddZone" 
     android:text="Add Zone" /> 

</LinearLayout> 

然後,請將點擊處理邏輯移至Tab3中的一個單獨方法,該方法對您的Activity類可見。 (我們假設它被稱爲addZone()。)Tab3類不應該實現OnClickListener並且應該延伸View(不是ViewGroup如上)。通過將屬性添加到按鈕,您不需要將OnClickListener添加到按鈕。相反,你需要實現該名稱的點擊方法的活動:

private Tab3 mTab3; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.tab3); 
    mTab3 = (Tab3) findViewById(R.id.tab3_display); 
    // . . . 
} 

@Override 
/** 
* Called when a view with attribute android:onClick="onAddZone" 
* is clicked. 
* 
* @param view the view that was clicked. 
*/ 
public void onAddZone(View view) { 
    mTab3.addZone(); 
} 

雖然沒有在你的代碼有關按鈕,框架會自動線的一切行動使用反射,這樣的的onAddZone方法活動將在按鈕被點擊時被調用。

+0

我更新了我的問題,我做了你說的,但是如果我有'addZone.setOnClickListener',並且如果我沒有在我的繪圖部分不會出現,它仍然崩潰?有任何想法嗎? –

+0

@MichaelZeuner - 我根據您的更新和評論更新了我的答案。 –

+0

當我點擊按鈕的應用程序崩潰。 –