我是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");
}
}
檢查logcat中,改變了我的答案.... – Mateusz