0

我實現了一個浮動動作按鈕,它具有使用下面的代碼的5個子動作按鈕,如何將一個onClickListener添加到每個此子動作按鈕,以便當你點擊是應該打開不同的活動。如何將onClicklistener附加到子動作按鈕

SubActionButton.Builder rLSubBuilder = new SubActionButton.Builder(this); 
    ImageView rlIcon1 = new ImageView(this); 
    ImageView rlIcon2 = new ImageView(this); 
    ImageView rlIcon3 = new ImageView(this); 
    ImageView rlIcon4 = new ImageView(this); 
    ImageView rlIcon5 = new ImageView(this); 
    //ImageView rlIcon6 = new ImageView(this); 

    rlIcon1.setImageDrawable(getResources().getDrawable(R.drawable.ic_action_contact)); 
    rlIcon2.setImageDrawable(getResources().getDrawable(R.drawable.ic_action_currency_info)); 
    rlIcon3.setImageDrawable(getResources().getDrawable(R.drawable.ic_action_exhibition)); 
    rlIcon4.setImageDrawable(getResources().getDrawable(R.drawable.ic_action_faq)); 
    rlIcon5.setImageDrawable(getResources().getDrawable(R.drawable.ic_action_info_desk)); 
    //rlIcon6.setImageDrawable(getResources().getDrawable(R.drawable.ic_action_passport)); 



    // Build the menu with default options: light theme, 90 degrees, 72dp radius. 
    // Set 4 default SubActionButtons 
    final FloatingActionMenu rightLowerMenu = new FloatingActionMenu.Builder(this) 
      .addSubActionView(rLSubBuilder.setContentView(rlIcon1).build()) 
      .addSubActionView(rLSubBuilder.setContentView(rlIcon2).build()) 
      .addSubActionView(rLSubBuilder.setContentView(rlIcon3).build()) 
      .addSubActionView(rLSubBuilder.setContentView(rlIcon4).build()) 
      .addSubActionView(rLSubBuilder.setContentView(rlIcon5).build()) 
      //.addSubActionView(rLSubBuilder.setContentView(rlIcon6).build()) 
      .attachTo(rightLowerButton) 
      .build(); 

    // Listen menu open and close events to animate the button content view 
    rightLowerMenu.setStateChangeListener(new FloatingActionMenu.MenuStateChangeListener() { 
     @Override 
     public void onMenuOpened(FloatingActionMenu menu) { 
      // Rotate the icon of rightLowerButton 45 degrees clockwise 
      fabIconNew.setRotation(0); 
      PropertyValuesHolder pvhR = PropertyValuesHolder.ofFloat(View.ROTATION, 90); 
      ObjectAnimator animation = ObjectAnimator.ofPropertyValuesHolder(fabIconNew, pvhR); 
      animation.start(); 
     } 

     @Override 
     public void onMenuClosed(FloatingActionMenu menu) { 
      // Rotate the icon of rightLowerButton 45 degrees counter-clockwise 
      fabIconNew.setRotation(90); 
      PropertyValuesHolder pvhR = PropertyValuesHolder.ofFloat(View.ROTATION, 0); 
      ObjectAnimator animation = ObjectAnimator.ofPropertyValuesHolder(fabIconNew, pvhR); 
      animation.start(); 
     } 
    }); 

} 

回答

1

嘗試:

SubActionButton button1 = rLSubBuilder.setContentView(rlIcon1).build(); 
SubActionButton button2 = rLSubBuilder.setContentView(rlIcon2).build(); 
[...] 
SubActionButton buttonN = {...} 

button1.setOnClickListener(...); 
button2.setOnClickListener(...); 
[...] 

final FloatingActionMenu rightLowerMenu = new FloatingActionMenu.Builder(this) 
      .addSubActionView(button1) 
      .addSubActionView(button2) 
      .addSubActionView(rLSubBuilder.setContentView(rlIcon3).build()) 
      .addSubActionView(rLSubBuilder.setContentView(rlIcon4).build()) 
      .addSubActionView(rLSubBuilder.setContentView(rlIcon5).build()) 
      //.addSubActionView(rLSubBuilder.setContentView(rlIcon6).build()) 
      .attachTo(rightLowerButton) 
      .build(); 
+0

謝謝。這工作! – Omega

0

我假設你使用這個library。查看source,您可以看到setContentView只是將視圖添加到從FrameLayout派生的CircularActionMenu。因此,將onClickListeners設置爲ImageViews以開啓新活動。這是你的onClickListeners應該是什麼樣子

rIIcon1.setOnClickListener(new View.OnCLickListener(){ 
    @Override 
    public void onClick(View view){ 
     Intent intent = new Intent(getApplicationContext(),ActivityName.class) 
     startActivity(intent) 
    } 
}) 

同樣附加偵聽到其他imageviews

+0

是的,你是對的,但我是菜鳥,我該如何設置onClickListeners到我的ImageViews。我會很感激編輯我的代碼 – Omega

+0

@Omega檢查編輯 – akash93