2012-07-27 38 views
-2

這是我的佈局:的Android的ImageButton不工作

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@color/white" > 

    <ImageButton 
     android:id="@+id/one_button" 
     android:layout_width="125dp" 
     android:layout_height="125dp" 
     android:layout_alignParentRight="true" 
     android:layout_marginRight="20dp" 
     android:layout_marginTop="20dp" 
     android:background="@drawable/1" 
     android:contentDescription="@string/by1" /> 

    <ImageButton 
     android:id="@+id/two_button" 
     android:layout_width="125dp" 
     android:layout_height="125dp" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginLeft="20dp" 
     android:layout_marginTop="20dp" 
     android:background="@drawable/2" 
     android:contentDescription="@string/by2" /> 

    <ImageButton 
     android:id="@+id/three_button" 
     android:layout_width="125dp" 
     android:layout_height="125dp" 
     android:layout_alignParentLeft="true" 
     android:layout_marginLeft="20dp" 
     android:layout_marginTop="185dp" 
     android:background="@drawable/3" 
     android:contentDescription="@string/by3" /> 

    <ImageButton 
     android:id="@+id/four_button" 
     android:layout_width="125dp" 
     android:layout_height="125dp" 
     android:layout_alignParentRight="true" 
     android:layout_marginRight="20dp" 
     android:layout_marginTop="185dp" 
     android:background="@drawable/4" 
     android:contentDescription="@string/by4" /> 

</RelativeLayout> 

這是我的代碼:

@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 

    switch (v.getId()) { 
     case R.id.one_button : 
      startActivity(new Intent("android.intent.action.GROUP")); 
      break; 
     case R.id.two_button : 
      startActivity(new Intent("android.intent.action.ATOZ")); 
      break; 
     case R.id.three_button : 
      startActivity(new Intent("android.intent.action.ATOZ")); 
      break; 
     default : 
      break; 
    } 

} 

現在,如果我按one_buttontwo_button所有的作品如預期,但如果我按three_button什麼發生。

我在做什麼錯?

P.S. - 清單是好的,因爲two_button開始所需的活動。

感謝,

favolas

+4

我很驚訝地看到,你的第2個按鈕甚至工作,這應該拋出編譯錯誤,因爲按鈕ID不能以數字開頭。 – Sudhaker 2012-07-27 11:16:28

+0

這僅僅是一個例子。編輯我的問題。沒有收到編譯器錯誤,甚至沒有警告 – Favolas 2012-07-27 11:18:31

+1

你有沒有爲onClickListener分配第三個按鈕? – 2012-07-27 11:20:37

回答

3

你有沒有設置onClickListener上3_button。

ImageButton btn=(ImageButton)findViewById(R.id.3_button); 
btn.setOnClickListener(this); 
+0

是的。作爲我編輯的答案,公共類MyClass擴展了Activity實現OnClickListener – Favolas 2012-07-27 11:20:35

+0

你做完了嗎? – jeet 2012-07-27 11:22:49

+0

謝謝。忘了這麼做:(很簡單,但有時,應該在眼前的東西往往是不可見的。) – Favolas 2012-07-27 11:26:00

4

你不能有按鈕名稱與數字開始

嘗試將Button Ids改變的東西作爲button_1而不是1_button

它會工作。

可能是你沒有第三個按鈕添加監聽器...

+0

對不起。這只是例子而已。編輯我的問題 – Favolas 2012-07-27 11:18:02

+2

+1爲什麼downvote? – 2012-07-27 11:18:09

+0

@ Eng.Fouad有些人傾向於懲罰簡單的錯誤...... – Favolas 2012-07-27 11:29:31