我正在尝试检查切换按钮是否为Check(),以及其背景是否等于特定的drawable。我一直试图让这个超过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的 当活动加载时,TB状态关闭,图像设置为testimg1。当用户按下TB时,状态变为on,图像变为testimg2,并将Toast一条消息添加到(后来添加到ArrayList中)。当用户再次按下它时,应该有一个Toast表示已将其删除(来自所述ArrayList)<?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>
答案 0 :(得分:0)
尝试做这样的事情。
更新的答案:
内部活动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();
}
}
});