简体   繁体   中英

Checking toggle button state and background image

I'm attemping to check if a toggle button isChecked() and if its background equals a specific drawable. I've been trying to get this for over 2hrs and the solution eludes me.

The toggle button has an xml set to it with the images its supposed to set when the state of the button is changed

This is what I've attemped so far

tbTest1.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (tbTest1.isChecked())
                {
                    if(tbTest1.getBackground().equals("testimg.png")) {
                        Toast.makeText(Test.this, "Test Image Added", Toast.LENGTH_LONG).show();
                    }
                }
                else
                {
                    //fileNames.add("testimg.png");
                    //isChecked--;
                    Toast.makeText(Test.this, "Test Image Removed", Toast.LENGTH_LONG).show();
                }
            }
        });

This gives no results

I've also tried this

tbTest1.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (tbTest1.isChecked() && tbTest1.getBackground().equals(R.drawable.testimg))
                {
                        Toast.makeText(Test.this, "Test Image Added", Toast.LENGTH_LONG).show();
                }
                else
                {
                    Toast.makeText(Test.this, "Test Image Removed", Toast.LENGTH_LONG).show();
                }
            }
        });

This skips straight to the ELSE statement

Here is the xml for testimg

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/testimg1"
    android:state_checked="false" />
<item android:drawable="@drawable/testimg2"
    android:state_checked="true"/>

</selector>

When the activity loads, the TB state is off and the image is set to testimg1. When a user presses the TB, the state changes to on and the image changes to testimg2 and will Toast a message that it was added(to an ArrayList thats being added later). When the user presses it again, there should be a Toast that says it was removed(from said ArrayList)

Try doing something like this.

Updated Answer:

INSIDE ACTIVITY A

tbTest1.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (tbTest1.isChecked()){
                //Button is checked, add image String name into ArrayList<String> mArray;                   

                }
                else
                {
                 //Button is not checked, remove the image String name from ArrayList<String> mArray;
                }
            }
        });

After you have populated the ArrayList<String> mArray , you will probably have some button that will move you to Activity B.

Create a Bundle and store the ArrayList<String> mArray;

    Bundle mBundle = new Bundle();
    mBundle.putStringArrayList("ARRAYLIST", mArray);

Sending the data to Activity B

Intent intent = new Intent();
intent.setClass(this, ACTIVITY_B.class);
intent.putExtra("BUNDLE", mBundle);
startActivity(intent);

INSIDE ACTIVITY B

ArrayList<String> mArray = new ArrayList<String>();


@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  Bundle extras = getIntent().getExtras();
  if (extras != null) {
   Bundle mBundle = extras.getBundleExtra("BUNDLE");
   if (mBundle != null) {
        // do stuff
        //pull the ArrayList out of the Bundle and create a new ArrayList<String>

        mArray = mBundle.getStringArrayList("ARRAYLIST")   ; 

        // You now have the ArrayList<String> from Activity A, you can create and if statement, or some type of logic to handle generating pictures based on the String Names.


   }        
}

UPDATE 2:

INSIDE ACTIVITY B

You should be able to access the Drawable this way, you would

tbTest1.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (tbTest1.isChecked(){
                        Toast.makeText(Test.this, "Test Image Added", Toast.LENGTH_LONG).show();

                        Drawable currentDrawable = tbTest1.getBackground();
                        //set currentDrawable to the background of a temporary image view

                         mImageView.setBackground(currentDrawable);

                }
                else
                {
                    Toast.makeText(Test.this, "Test Image Removed", Toast.LENGTH_LONG).show();
                }
            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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