更改片段中的按钮

change button in fragment

我在一个片段中有两个按钮,当我点击第一个按钮时,我想要这个按钮变成白色,第二个按钮变成黑色,如果点击第二个按钮,第二个按钮变成白色,第一个按钮变为黑色 这是我在 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)));
    }

但无法正常工作 你有什么建议?

可能是这段代码有问题

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

这样试试

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

这是解决方案 Activity :

@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" />

你的错误是你把

 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 中删除它们

希望它能解决您的问题并对您有所帮助(: