2011-12-02 41 views
0

我是Android新手,並且無法獲取此表中的按鈕來觸發OnClick事件。我們的目標是要有一個充滿按鈕的表格,當他們被點擊時,做一些事情。現在,我只想輸出到控制檯,但不會調用OnClick方法。按鈕OnClick不在Android上發射

我相信這是一個愚蠢的問題,並感謝幫助。

public class HiThereActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     ArrayList<String> names = new ArrayList<String>(); 

     names.add("Michael"); 
     names.add("John"); 
     names.add("Mike"); 
     names.add("Tom"); 
     names.add("Steve"); 

     ArrayList<String> codes = new ArrayList<String>(); 

     codes.add("abcde"); 
     codes.add("fghij"); 
     codes.add("klmno"); 
     codes.add("pqrst"); 
     codes.add("uvwxy"); 
     codes.add("12345"); 
     codes.add("67890"); 



     TableLayout table = new TableLayout(this); 

     table.setStretchAllColumns(true); 
     table.setShrinkAllColumns(true); 

     TableRow rowTitle = new TableRow(this); 
     rowTitle.setGravity(Gravity.CENTER_HORIZONTAL); 

     TableRow rowConditions = new TableRow(this); 
     rowConditions.setGravity(Gravity.CENTER); 


     // title column/row 
     TextView title = new TextView(this); 
     title.setText("IH10 Katy Extension - 11/7/2011"); 

     title.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18); 
     title.setGravity(Gravity.CENTER); 
     title.setTypeface(Typeface.SERIF, Typeface.BOLD); 

     TableRow.LayoutParams params = new TableRow.LayoutParams(); 
     params.span = (codes.size() + 1); 

     rowTitle.addView(title, params); 

     Iterator<String> nameItr = names.iterator(); 
     while(nameItr.hasNext()){ 
      TableRow row = new TableRow(this); 
      TextView nameLable = new TextView(this); 
      nameLable.setText(nameItr.next()); 
      nameLable.setTypeface(Typeface.DEFAULT_BOLD); 
      row.addView(nameLable); 

      Iterator<String> codeItr = codes.iterator(); 
      while(codeItr.hasNext()){ 
       /*TextView codeLabel = new TextView(this); 
       codeLabel.setText(codeItr.next()); 
       codeLabel.setTypeface(Typeface.SERIF, Typeface.BOLD); 

       row.addView(codeLabel); 
       */ 
       codeItr.next(); 
       Button rowButton = new Button(this); 
       rowButton.setText("8"); 
       //rowButton.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT)); 
       rowButton.setOnClickListener(btnListener); 


       row.addView(rowButton); 


      } 
      table.addView(row); 
     } 


     setContentView(table); 
    } 

    private OnClickListener btnListener = new OnClickListener() { 
     public void onClick(View v) { 
      System.out.println("TEST"); 
     } 
    }; 


} 

編輯#1: 我做了一些嘗試,這樣做,很簡單的例子中的onClick不叫。我知道這真的很愚蠢。有任何想法嗎?

package com.bordeloniphone.timeentry; 

import android.app.Activity; 
import android.os.Bundle; 

import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 

public class TimeEntryActivity extends Activity implements OnClickListener{ 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     //setContentView(R.layout.main); 

     Button okButton = new Button(this); 
     okButton.setText(":)"); 
     okButton.setOnClickListener(this); 
     setContentView(okButton); 

    } 

public void onClick(View v) { 
    Log.d("TEST", "TEST"); 


} 

} 
+0

檢查logcat中,改變了我的答案.... – Mateusz

回答

0

Android的印刷與System.out.println()控制檯將無法工作,因爲控制檯是內部的電話。

而不是打印到控制檯,儘量呈現出麪包來代替(其更容易地看到):

private OnClickListener btnListener = new OnClickListener() { 
    public void onClick(View v) { 
     Toast.makeText(this, "TEST", Toast.LENGTH_SHORT).show(); 
    } 
}; 

或者,如果你真的想打印到控制檯,打印,而不是logcat的:

Log.d("HiThereActivity", "THIS IS DEBUG OUTPUT TO LOGCAT"); 

更新:

也許嘗試調用setDescendantFocusability方法對你TableLayout和的TableRow變量:

table.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS); 
rowTitle.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS); 
rowConditions.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS); 

,並在迭代循環:

row.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS); 
+0

Darnit。我感覺很蠢。我嘗試了Toast和Log,並且放入了斷點,並且OnCLick永遠不會被調用。我想知道該表是否以某種方式攔截事件。 –

+0

@MichaelBordelon檢查我的更新後的帖子是否有新的建議。 –

+0

謝謝你的幫助約翰。我檢查了LogCat,沒有任何記錄。另外,當我進入調試階段時,它無法使用OnClick方法。我也嘗試了Focusability設置。這是一個好主意。我做了很多iOS開發,從來沒有遇到過這樣的事情。看起來好像在表格行中有按鈕是不奇怪的。 –

0

如果您使用的是Eclipse去窗口 - >顯示視圖>其他...並在Android的組中選擇logcat的。正如約翰所說,它對調試非常有用。

+0

感謝Sanandrea。我能夠看到Logcat並進行了測試,以確保它獲取我的日誌消息,但onclick方法未被調用。 –

+0

你看到按鈕了嗎? – Sanandrea

0

你只需要一個@Override聲明的onClick方法之前:

公共類TimeEntryActivity擴展活動工具OnClickListener {

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    //setContentView(R.layout.main); 

    Button okButton = new Button(this); 
    okButton.setText(":)"); 
    okButton.setOnClickListener(this); 
    setContentView(okButton); 

} 

**@Override** 

public void onClick(View v) { 
    Log.d("TEST", "TEST"); 


} 

}