简体   繁体   English

检查切换按钮状态和背景图片

[英]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. 我正在尝试检查切换按钮isChecked()及其背景是否等于特定的可绘制对象。 I've been trying to get this for over 2hrs and the solution eludes me. 我已经尝试了2个多小时,而解决方案使我望而却步。

The toggle button has an xml set to it with the images its supposed to set when the state of the button is changed 切换按钮具有一个xml设置,其图像应在按钮状态更改时设置

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 这直接跳到ELSE语句

Here is the xml for testimg 这是testimg的XML

<?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. 活动加载时,TB状态为关闭,并且映像设置为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). 当用户按下TB时,状态更改为on,图像更改为testimg2,并将向Toast一条消息,告知其已添加(添加到稍后添加的ArrayList中)。 When the user presses it again, there should be a Toast that says it was removed(from said ArrayList) 当用户再次按下它时,应该有一个Toast,它表示已被删除(从ArrayList中)

Try doing something like this. 尝试做这样的事情。

Updated Answer: 更新的答案:

INSIDE ACTIVITY A 内在活动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. 填充ArrayList<String> mArray ,可能会有一些按钮将您带到活动B。

Create a Bundle and store the ArrayList<String> mArray; 创建一个Bundle并存储ArrayList<String> mArray;

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

Sending the data to Activity B 将数据发送到活动B

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

INSIDE ACTIVITY B 内在活动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: 更新2:

INSIDE ACTIVITY B 内在活动B

You should be able to access the Drawable this way, you would 您应该能够以这种方式访问​​Drawable,

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();
                }
            }
        });

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

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