繁体   English   中英

如何在片段内单击按钮时更改按钮可绘制色调颜色

[英]How to change Button drawable tint color on button click inside fragment

我正在尝试将按钮单击时的按钮可绘制色调颜色更改为我想要的颜色,单击时无法更改,请帮助我希望它是按钮而不是 ImageButton。

在下面,我将发布我使用的选项卡布局的代码,其中两个选项卡是单向的并返回片段。 在 onewayfragment 中遇到了问题 当我从(经济、商务和头等舱)中选择类型时,当我选择经济按钮时,经济按钮中的可绘制对象应更改为我想要的颜色。

1) 主页

2) 家庭适配器

3) OneWayFragment

4)返回片段

当我选择经济按钮时,这就是我想要做的,经济按钮内的可绘制应将可绘制色调更改为我想要的颜色

下面是我使用过的文件的编码

主页.java

package com.example.gomerry.Home_Page;
import android.app.DatePickerDialog;
import android.os.Bundle;
import android.widget.Adapter;
import android.widget.TextView;
import androidx.viewpager.widget.ViewPager;
import com.example.gomerry.Menu;
import com.example.gomerry.R;
import com.google.android.material.tabs.TabLayout;

public class HomePage extends Menu {


TextView date;
TextView month;
TextView dayofWeek,day;
DatePickerDialog datePickerDialog1;
DatePickerDialog datePickerDialog2;
Adapter adapter;


TabLayout tabLayout;
ViewPager viewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home_page);

    tabLayout=(TabLayout)findViewById(R.id.tabLayout);
    viewPager=(ViewPager)findViewById(R.id.viewPager);

    tabLayout.addTab(tabLayout.newTab().setText("One Way"));
    tabLayout.addTab(tabLayout.newTab().setText("Return"));
    // tabLayout.addTab(tabLayout.newTab().setText("Movie"));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

    final HomeAdapter adapter = new HomeAdapter(this,getSupportFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(adapter);

    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));



    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {
        }
    });
}
}//end of code

这是 HomeAdapter.java

package com.example.gomerry.Home_Page;

import android.content.Context;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import com.example.gomerry.CancelBooking.CancellationFragment;
import com.example.gomerry.CancelBooking.ChargesFragment;

public class HomeAdapter extends FragmentPagerAdapter {

private Context myContext;
int totalTabs;

public HomeAdapter(Context context, FragmentManager fm, int totalTabs) {
    super(fm);
    myContext = context;
    this.totalTabs = totalTabs;
}

// this is for fragment tabs
@Override
public Fragment getItem(int position) {
    switch (position) {
        case 0:
            OneWayFragment oneWayFragment = new OneWayFragment();
            return oneWayFragment;
        case 1:
            ReturnFragment returnFragment = new ReturnFragment();
            return returnFragment;

        default:
            return null;
    }
}
// this counts total number of tabs
@Override
public int getCount() {
    return totalTabs;
}
}//end of code

这是 OneWayFragment.java

package com.example.gomerry.Home_Page;

import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.example.gomerry.R;

public class OneWayFragment extends Fragment {


Button economy;

public OneWayFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
   // return inflater.inflate(R.layout.one_way_fragment, container, false);

    Log.d("link_fly",Data_URL);
    final View view = inflater.inflate(R.layout.one_way_fragment, container, false);

    economy =(Button) view.findViewById(R.id.button11);

    economy.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            economy.setTextColor(R.color.cyan92a6);
            ImageButton button = (ImageButton) this.findViewById(R.id.button_i_want_to_modify);
            economy.setColorFilter(Color.argb(255, 255, 255, 255)); // White Tint
            economy.setColorFilter(ContextCompat.getColor(getApplicationContext(), R.color.cyanBcD4), PorterDuff.Mode.MULTIPLY);
            economy.setCompoundDrawableTintMode();
            binding.myButton.compoundDrawables.first().setTint(Color.RED);
            view.economy.compoundDrawables.first().setTint(Color.RED);
            economy.compoundDrawables.first().setTint(Color.RED);
            economy.getBackground().setColorFilter();
        }
    });

    return view;
}
}//end of code

这是按钮xml

这是按钮xml

这是我尝试过的代码图像,但出现错误,但无法正常工作,我还没有一次尝试所有代码,我正在此图像中一一尝试,我取消了所有注释以表明它们显示错误。
在此处输入图片说明

我希望这是对代码的清晰解释:) 和我遇到的问题。 请帮我解决这个错误。 提前致谢

要更改 Button 的 drawable,您必须了解一些事情。 显然,您的按钮上可以有多个可绘制的复合,但是如果您只有一个,您可以这样做

    Button economy = findViewById(R.id.button11)
    economy.setOnClickListener {
        economy.compoundDrawables.first().setTint(Color.RED)
    }

正如您所看到的, compundDrawables返回一个可绘制列表,因此如果您只想编辑一个,您只需调用first() ,然后您就可以根据您的需要相应地设置色调。 干杯!

您可以使用setBackgroundTintList()方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM