更改片段中的按钮

问题描述 投票:0回答:3

单击第一个按钮时,我想要一个片段中的两个按钮,此按钮变为白色,第二个按钮变为黑色,如果单击第二个按钮,则第二个按钮变为白色,第一个按钮变为黑色这是我在mainActivity中的代码:

public void buttonOne(View view) {
    Button1 = findViewById(R.id.tg_btn2);
    Button2 = findViewById(R.id.tg_btn1);
    //If the Button is off
    if (!btnOneOn) {

        Button2.setBackgroundColor(getResources().getColor(R.color.md_blue_grey_900));
        Button1.setBackgroundColor(getResources().getColor((R.color.md_white_1000)));
        Button1.setTextColor(getResources().getColor(R.color.md_blue_grey_100));
        Button2.setTextColor(getResources().getColor(R.color.md_black_1000));
        btnTwoOn = false;
        btnOneOn = true;
        Log.i("Salam",btnOneOn.toString());
    }
    //If it is is clicked while on
    else {
        btnTwoOn = false;
        Button2.setBackgroundColor(getResources().getColor((R.color.md_blue_grey_900)));
    }
}

public void buttonTwo(View view) {
    Button2 = findViewById(R.id.tg_btn2);
    Button1 = findViewById(R.id.tg_btn1);
    //If the Button is off
    if (!btnTwoOn) {

        Button1.setBackgroundColor(getResources().getColor(R.color.md_blue_grey_900));
        Button2.setBackgroundColor(getResources().getColor((R.color.md_white_1000)));
        btnOneOn = false;
        btnTwoOn = true;
    }
    //If it is is clicked while on
    else {
        btnOneOn = false;
        Button1.setBackgroundColor(getResources().getColor((R.color.md_blue_grey_900)));
    }

但它没有正常工作你的建议是什么?

java android android-studio android-fragmentactivity
3个回答
0
投票

这是解决方案活动:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.activity_main2, container, false);

    final Button button1 = v.findViewById(R.id.button);
    final Button button2 = v.findViewById(R.id.button2);
    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            button1.setBackgroundResource(R.color.gray);
            button2.setBackgroundResource(R.color.black);
        }
    });

    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            button1.setBackgroundResource(R.color.black);
            button2.setBackgroundResource(R.color.gray);

        }
    });
    return v;
}

activity_main2.xml:

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button1"
    android:background="@color/black"
    android:textColor="@color/white" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button2"
    android:background="@color/black"
    android:textColor="@color/white" />

0
投票

也许问题在于这段代码

setBackgroundColor(getResources().getColor(R.color.md_blue_grey_900))

试试这样吧

setBackgroundColor(ContextCompat.getColor(context, R.color.md_blue_grey_900))

0
投票

你犯的错误就是你

 Button1 = findViewById(R.id.tg_btn2);
Button2 = findViewById(R.id.tg_btn1);

在buttonOne和buttonTwo方法中,它就像你制作新按钮并处理它们将这两行放在一起并放入这样的onStart方法

@Override
public void onStart() {
    super.onStart();
    Button1 = getActivity().findViewById(R.id.tg_btn2);
    Button2 = getActivity().findViewById(R.id.tg_btn1);
}

并从buttonOne和buttonTwo OnClick中删除它们

我希望它能解决你的问题并提供帮助(:

© www.soinside.com 2019 - 2024. All rights reserved.