2011-06-17 67 views
0

我在for循環中以編程方式創建了一堆ImageButtons。他們工作得很好,數據顯示在HorizontalScrollView。現在我需要每個人點擊時變暗或明亮。首先點擊setAlpha(45);第二次點擊將setAlpha(255);.我需要將onClickListener添加到一堆ImageButton中

我不認爲我完全理解Views和onClickListener的工作原理。看來onClick函數的例子我找了一個View。該功能如何知道哪個按鈕被點擊?也許有一種更簡單的方法來做我想做的事情?

這裏是ImageButtons

TableRow tr0 = new TableRow(this); 
tr0.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 

for(int but=0; but<ClueList.size(); but++){ 
    ImageButton clueBut = new ImageButton(this); 
     clueBut.setBackgroundResource(0); 
     clueBut.setImageBitmap(ClueList.get(but).btmp); 
     //clueBut.setOnClickListener(this); 

     tr0.addView(clueBut); 
} 

有什麼我需要做的,使按鈕可識別?那麼如何傳入onClick函數才能使用?

- :增加的信息: - 我開始懷疑問題是不是與按鈕,但與我建立屏幕的方式。更多信息添加。

遊戲活動是主要的遊戲,它使用PuzzleView拿着遊戲網格的屏幕上半部分。下半部分是ImageButton的位置,我在Game類中構建它們。

public class Game extends Activity{ 
    //various variables and stuff 

    private PuzzleView puzzleView; // The PuzzleView is from another .java file 
            // public class PuzzleView extends View 

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

     LinearLayout mainPanel = new LinearLayout(this); 
     mainPanel.setOrientation(LinearLayout.VERTICLE); 

     puzzleView = new PuzzleView(this); 
     mainPanel.addView(puzzleView); 

     HorizontalScrollView bottom = new HorizontalScrollView(this); 
     mainPanel.addView(bottom); 

     TableLayout clues = new TableLayout(this); 
     bottom.addView(clues); 

     TableRow tr0 = new TableRow(this); 
     for(int but=0; but<ClueList.size(); but++){ 
     ImageButton clueBut = new ImageButton(this); 
     clueBut.setImageBitmap(ClueList.get(but).btmp); 

     tr0.addView(clueBut); 
     } 

當我嘗試添加ClickListener(這個)我得到this不能夠成爲一個Game錯誤。在涉及ViewonClick(View v)函數中,我遇到了類似的問題。這些問題是因爲我在Game Activity而不是View類中構建按鈕嗎?

感謝

回答

4

當您設置一個OnClickListener和落實onClick(View v)回調,這是Dalvik虛擬機一個將每次單擊查看時調用該方法,它將通過查看實例作爲參數。因此,您在該方法中編寫的代碼將僅應用於接收了點擊的視圖,而不應用於任何其他視圖。加入這樣的事情你的循環:

clueBut.setOnClickListener(new OnClickListener() { 
    public void onClick (View v) { 
    if (v.getAlpha() == 1f) 
     v.setAlpha(0.2f); 
    else 
     v.setAlpha(1f); 
    } 
}); 
+0

我已經加入上述的詳細信息視圖的ID。這些建議看起來與我在網上找到的相似,但是我得到了有關在'onClick'中使用'View'或在'onClickListener'中使用'this'的錯誤。也許我正在構建屏幕錯誤? – Agent0013 2011-06-17 15:20:49

+0

你的Activity沒有實現OnClickListener接口,所以你不能使用'clueBut.setOnClickListener(this);'。要麼實現它('public class Game extends Activity implements OnClickListener {'),或者使用我發佈的代碼片段。如果您發現任何錯誤,請將其發佈,以便我瞭解您的具體問題。 – Aleadam 2011-06-17 15:41:23

+0

我已經嘗試在遊戲類中添加'implements OnClickListener',並且我得到相同的錯誤。 '在類型視圖的方法setOnClickListener(View.OnClickListener)不適用於參數(新DialogInterface.OnClickListener(){})'看來對話框彈出我有當遊戲做的是與我有什麼干擾試圖用這些按鈕做。解決這個問題最簡單的方法是什麼?我開始嘗試將滾動按鈕移動到另一個類中,這是PuzzleView從另一個類中調用的方式,但它無法訪問Clues鏈接列表。 – Agent0013 2011-06-18 03:24:49

1

在onClick事件:

public void onClick(View currentView) 
{ 
    Button currentButton = (Button)CurrentView; 
    //Do whatever you need with that button here. 
} 
+0

這看起來簡單,類似於我在網上找到。我想我可能是建立在屏幕錯誤作爲視圖currentView給出錯誤。 – Agent0013 2011-06-17 15:19:02

+0

你什麼樣的錯誤? – 2011-06-17 23:40:52

0

識別每個視圖唯一使用屬性

查看。 SETID(INT)

在你的情況下,代碼會是這個樣子

for(int but=0; but<ClueList.size(); but++){ 
    ImageButton clueBut = new ImageButton(this); 
     clueBut.setBackgroundResource(0); 
     clueBut.setImageBitmap(ClueList.get(but).btmp); 
     clueBut.setId(but); 
     //clueBut.setOnClickListener(this); 

     tr0.addView(clueBut); 
} 

內部的onclick監聽器匹配使用findViewByID()

+0

我也嘗試添加SETID(但)進入循環。我仍然有錯誤與setOnClickListener(本)。或許有是我做錯了有關屏幕的佈局。我沒有添加更多的信息上面。 – Agent0013 2011-06-17 15:17:56

+0

我沒有看到一個setContentView()在你的代碼:( – 2011-06-17 17:55:05

+0

好點。它在那裏,我只是錯過了把它放在這裏。後for循環做的按鈕,有'clues.addView(TR0,新TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));'然後'的setContentView(mainPanel中);' – Agent0013 2011-06-17 18:01:19

相關問題