【问题标题】:How to change Button color when click on it [duplicate]单击按钮时如何更改按钮颜色[重复]
【发布时间】:2018-08-16 20:04:04
【问题描述】:

如何改变按钮的颜色?当我单击“设备”按钮时,颜色会发生变化。然后当我点击另一个按钮,如“技术员”按钮上一个按钮(“设备”)颜色设置为默认按钮颜色和“技术员”按钮颜色改变。

here is my code

   public void onButtonTabClick(View v)
    {
        Fragment fragment = null;
        switch (v.getId())
        {
            case R.id.button_equipment:

              fragment = new EquipmentFragment();

               break;
            case R.id.button_tech:

                fragment = new TechnicianFragment();

                break;

            case R.id.button_timeline:
                fragment = new TimeLineFragment();

                break;
        }

        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.show_fragment, fragment);
        transaction.commit();
    }

【问题讨论】:

  • 你试过了吗?你的代码在哪里?
  • 基本上我想改变片段加按钮的颜色......所以我只改变片段......但我也尝试改变颜色但我无法得到我想要的结果
  • 告诉我们你有什么尝试
  • 你可以使用单选按钮
  • public void onButtonTabClick(View v) { Fragment fragment = null; switch (v.getId()) { case R.id.button_equipment: fragment = new EquipmentFragment();休息;案例 R.id.button_tech: 片段 = 新的 TechnicianFragment();休息; case R.id.button_timeline: fragment = new TimeLineFragment();休息; } }

标签: java android


【解决方案1】:

您只需要编写特定的代码来设置按钮的背景颜色。

public void onEquipmentPressed(){
    equipmentButton.setBackgroundColor(getResources().getColor(R.color.color_id));
    technicans.setBackgroundColor(getResources().getColor(R.color.default_color_id));
    timeline.setBackgroundColor(getResources().getColor(R.color.default_color_id));

}


public void onTechnicansPressed(){
    equipmentButton.setBackgroundColor(getResources().getColor(R.color.default_color_id));
    technicans.setBackgroundColor(getResources().getColor(R.color.color_id));
    timeline.setBackgroundColor(getResources().getColor(R.color.default_color_id));

}

等等

【讨论】:

    【解决方案2】:

    您不需要像@Asset Bekbossynov 那样声明很多方法来处理按钮的状态。可以这样写代码:

    private View mLastClickView;
    public void onButtonTabClick(View v)
    {
        // add these code
        if (mLastClickView != null) {
            mLastClickView.setBackgroundColor(getResources().getColor(R.color.unselected));
        }
        v.setBackgroundColor(getResources().getColor(R.color.selected));
        mLastClickView = v;
    
        Fragment fragment = null;
        switch (v.getId())
        {
            case R.id.button_equipment:
                fragment = new EquipmentFragment();
    
                break;
            case R.id.button_tech:
    
                fragment = new TechnicianFragment();
    
                break;
    
            case R.id.button_timeline:
                fragment = new TimeLineFragment();
    
                break;
        }
    
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.show_fragment, fragment);
        transaction.commit();
    }
    

    【讨论】:

    • 为什么要使用片段?是他问的还是我理解错了?
    • 他想点击按钮改变fragment,改变按钮的状态,比如tablayout。你可以看看他的问题,他刚才加了代码。
    • 非常感谢这段代码...这对我有用