2014-10-29 82 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