繁体   English   中英

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

[英]Checking toggle button state and background image

我正在尝试检查切换按钮isChecked()及其背景是否等于特定的可绘制对象。 我已经尝试了2个多小时,而解决方案使我望而却步。

切换按钮具有一个xml设置,其图像应在按钮状态更改时设置

这是我到目前为止尝试过的

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

这没有结果

我也试过了

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

这直接跳到ELSE语句

这是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>

活动加载时,TB状态为关闭,并且映像设置为testimg1。 当用户按下TB时,状态更改为on,图像更改为testimg2,并将向Toast一条消息,告知其已添加(添加到稍后添加的ArrayList中)。 当用户再次按下它时,应该有一个Toast,它表示已被删除(从ArrayList中)

尝试做这样的事情。

更新的答案:

内在活动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;
                }
            }
        });

填充ArrayList<String> mArray ,可能会有一些按钮将您带到活动B。

创建一个Bundle并存储ArrayList<String> mArray;

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

将数据发送到活动B

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

内在活动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.


   }        
}

更新2:

内在活动B

您应该能够以这种方式访问​​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