我是Android新手,无法在此表中获取按钮以触发OnClick事件。目标是让一个表格充满按钮,点击它们时,做一些事情。现在,我只想输出到控制台,但它不会调用OnClick方法。
我确信这是一个愚蠢的问题,并感谢您的帮助。
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 :(得分: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");
<强>更新强>
也许尝试在TableLayout和TableRow变量上调用setDescendantFocusability方法:
table.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
rowTitle.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
rowConditions.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
并在你的迭代循环中:
row.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
答案 1 :(得分:0)
如果您正在使用Eclipse,请转到Window-&gt; Show View-&gt; Other ...并在Android组中选择Logcat。正如John所说,它对Debug非常有用。
答案 2 :(得分:0)
在onClick方法之前只需要一个@Override语句:
公共类TimeEntryActivity扩展Activity 实现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");
}
}