【发布时间】:2015-03-09 00:04:47
【问题描述】:
我为我的自定义 LinearLayout 创建了这个类:
public class TestViewButton extends LinearLayout {
TextView text;
View line;
private String sText;
private boolean showLine;
public TestViewButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public TestViewButton(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.it_button, this, true);
text = (TextView) findViewById(R.id.text);
line = (View) findViewById(R.id.line);
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.testViewButton);
sText = a.getString(R.styleable.testViewButton_buttonText);
showLine = a.getBoolean(R.styleable.testViewButton_showLine, true);
a.recycle();
text.setText(sText);
if(showLine){
line.setVisibility(View.VISIBLE);
}
}
public TestViewButton(Context context) {
super(context);
}
}
从我的Activity 中,我为它设置了一个setOnClickListener:
TestViewButton testViewButton b1 = (TestViewButton) findViewById(R.id.btn1);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startActivity(new Intent(ActivityA.this, ActivityB.class));
}
});
但它不起作用并且什么也没有发生 - 当我认为它应该调用回调时没有调用。该类执行回调动作的大部分工作;但不幸的是,setOnClickListener 不会调用我实施的这些操作。
【问题讨论】:
-
显示布局xml文件,它可能提供线索
标签: android class onclicklistener