2014-10-29 84 views
0

我在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(); 
     } 
    }); 

现在在运行应用程序时,向下箭头正在发生,但烤面包不起作用。请帮忙解决这个问题。

+0

可以使用xml'android:onClick'成功吗? – Doomsknight 2014-10-29 11:37:45

+0

您无法为按钮注册两个onClickListener! – 2014-10-29 11:39:36

+0

尝试编写this.setOnClickListener而不是nameLayout.setOnClickListener – meh 2014-10-29 11:46:46

回答

1

RelativeLayout.OnClickListener应该是View.OnClickListener。

btnAccountSettings.setOnClickListener(new View.OnClickListener() {    
     @Override 
     public void onClick(View arg0) { 
      Toast.makeText(Settings.this, "Account Settings", Toast.LENGTH_SHORT).show(); 
     } 
    }); 
+0

谢谢你这么快的回复。我做了这个更正。向下的箭头显示正常工作。吐司也显示,但只有有时。我有一个自定义按钮4的屏幕,显示不同消息的烤面包。其实我想要这个自定义控件在不同的屏幕上使用,每次箭头下来,然后我必须显示不同的意见。 – 2014-10-29 12:20:50