我在android中制作了一个自定义按钮控件,其中一个左侧图像,右侧箭头图像和中间的textview。我想在按钮向下箭头的同时点击右箭头图像。为此,我在自定义按钮类中编写了以下代码自定义按钮单击不在android中工作
public class ButtonWithTwoImagesAndOneText extends LinearLayout{
private ImageView _image, _arrow;
private TextView _text;
private View _viewupper, _viewlower, _viewlowersmall;
public ButtonWithTwoImagesAndOneText(Context context,AttributeSet attrs) {
super(context, attrs, 0);
Init(context);
RelativeLayout nameLayout= (RelativeLayout)findViewById(R.id.lytcontrollayout);
nameLayout.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
_arrow.setImageDrawable(getResources().getDrawable(R.drawable.arrowdown));
}
});
}
我在这样的布局中使用了此按钮控件。
<com.example.Controls.ButtonWithTwoImagesAndOneText
android:id="@+id/btnaccountsettings"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</com.example.Controls.ButtonWithTwoImagesAndOneText>
并在该布局的类我添加按钮单击btnaccountsettings。
btnAccountSettings.setOnClickListener(new RelativeLayout.OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(Settings.this, "Account Settings", Toast.LENGTH_SHORT).show();
}
});
现在在运行应用程序时,向下箭头正在发生,但烤面包不起作用。请帮忙解决这个问题。
可以使用xml'android:onClick'成功吗? – Doomsknight 2014-10-29 11:37:45
您无法为按钮注册两个onClickListener! – 2014-10-29 11:39:36
尝试编写this.setOnClickListener而不是nameLayout.setOnClickListener – meh 2014-10-29 11:46:46