代码之家  ›  专栏  ›  技术社区  ›  Maarten

Android:无法更改片段中按钮的背景(可绘制)

  •  0
  • Maarten  · 技术社区  · 6 年前

    我有以下问题无法解决:

    我正在构建一个包含多个片段的android应用程序。在第二个片段上有一个按钮,每次按下该按钮时,需要更改其背景(即可绘制)。在我添加更多逻辑之前,我希望这能起作用,但我无法让它起作用。非常感谢您的帮助!

    round\u按钮\u f2\u减少。xml

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list
        xmlns:android="http://schemas.android.com/apk/res/android">
        <item>
            <shape android:shape="oval">
                <solid android:color="@color/colorButtonF1Decrease" />
            </shape>
        </item>
        <item
            android:drawable="@drawable/ic_trending_down_white_24px"
            android:gravity="center" />
    </layer-list>
    

    round\u按钮\u f2\u增加。xml

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item>
            <shape android:shape="oval">
                <solid android:color="@color/colorButtonF1Increase" />
            </shape>
        </item>
        <item
            android:drawable="@drawable/ic_trending_up_white_24px"
            android:gravity="center" />
    </layer-list>
    

    碎片2。xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:id="@+id/f2_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:text="Placeholder"
            android:textAlignment="center"
            android:textSize="40sp"
            android:textStyle="bold"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <Button
            android:id="@+id/f2_button"
            android:layout_width="56dp"
            android:layout_height="56dp"
            android:layout_marginBottom="24dp"
            android:layout_marginEnd="24dp"
            android:elevation="2dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent" />
    
    </android.support.constraint.ConstraintLayout>
    

    碎片二。JAVA

    import android.graphics.drawable.Drawable;
    import android.support.annotation.NonNull;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.Toast;
    
    public class FragmentTwo extends Fragment {
        private static final String TAG = "FragmentTwo";
    
        private Button mbtnSort;
    
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    
            View view = inflater.inflate(R.layout.fragment_two, container, false);
    
            mbtnSort = view.findViewById(R.id.f2_button);
            mbtnSort.setBackground(getResources().getDrawable(R.drawable.round_button_f2_decrease));
            mbtnSort.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Drawable btnBackground = mbtnSort.getBackground();
                    if (btnBackground.equals(getResources().getDrawable(R.drawable.round_button_f2_decrease))) {
                        Toast.makeText(getActivity(), "IF", Toast.LENGTH_LONG).show();
                        mbtnSort.setBackground(getResources().getDrawable(R.drawable.round_button_f2_increase));
                    } else {
                        Toast.makeText(getActivity(), "ELSE", Toast.LENGTH_LONG).show();
                        mbtnSort.setBackground(getResources().getDrawable(R.drawable.round_button_f2_decrease));
                    }
                }
            });
            return view;
        }
    }
    
    3 回复  |  直到 6 年前
        1
  •  0
  •   Taranmeet Singh    6 年前

    使用

    mbtnSort.setBackground(R.drawable.round_button_f2_decrease);
    
        2
  •  0
  •   Maarten    6 年前

    现在它可以工作了:

    public class FragmentThree extends Fragment {
        private static final String TAG = "FragmentThree";
    
        private Button mbtnSort;
        private boolean mbtnPressed = false;
    
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    
            View view = inflater.inflate(R.layout.fragment_three, container, false);
    
            mbtnSort = view.findViewById(R.id.f3_button);
            mbtnSort.setBackgroundResource(R.drawable.round_button_f2_decrease);
            mbtnSort.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mbtnPressed == true) {
                        Toast.makeText(getActivity(), "IF", Toast.LENGTH_LONG).show();
                        mbtnSort.setBackgroundResource(R.drawable.round_button_f2_decrease);
                        mbtnPressed = false;
                    } else {
                        Toast.makeText(getActivity(), "ELSE", Toast.LENGTH_LONG).show();
                        mbtnSort.setBackgroundResource(R.drawable.round_button_f2_increase);
                        mbtnPressed = true;
                    }
                }
            });
            return view;
        }
    }
    
        3
  •  0
  •   Hassan Badawi    6 年前

    我想我理解你在不改变背景的情况下试图解决你的问题 你的背景问题本身

    我找了你很多东西,想知道如何回到地面,并用你的round\u button\u f2\u decrease检查它。xml

    所以这很难

    如果您使用flag或其他东西进行此操作,现在将对您的问题有帮助

    我发现这可能有用 Comparing two drawables in android

    还有这个 Comparing resources within two drawables