我有一个名为 LabelButton 的自定义 LinearLayout 类,其中包含一个按钮和一个 TextView。我想让按钮的 onclick 监听器删除 LabelButton。如何将 LabelButton 对象中的某些内容传递给我的 Activity 类,告诉我的主布局删除该 LabelButton?
public class LabelButton extends LinearLayout {
private OnClickListener onClick;
public LabelButton(Context context, final String text) {
super(context);
LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View labelView = inflater.inflate( R.layout.button_label, this );
TextView textView = (TextView) labelView.findViewById( R.id.textLabelText);
textView.setText( text );
Button button = (Button) labelView.findViewById( R.id.buttonCancelLabel );
onClick = new OnClickListener( ) {
public void onClick( View v ) {
System.out.println("Button: " + text + " clicked");
// do something here to remove this button
}
};
button.setOnClickListener( onClick );
}
@Override
public void onFinishInflate() {
super.onFinishInflate();
}
}
在我的 Activity 类中,我将 LabelButtons 添加到这样的列表中......
//labelButtons is a List of LabelButtons
LabelButton labelButton = new LabelButton( getApplicationContext( ),
txtBagLabel.getText( ).toString( ) );
labelButtons.add( labelButton );