Android 检查切换按钮状态和背景图像
我正在尝试检查切换按钮是否已选中(),以及其背景是否等于特定的可绘制按钮。我已经试了两个多小时了,但我还是找不到解决办法 切换按钮设置了一个xml,其中包含更改按钮状态时应该设置的图像 这就是我到目前为止所做的Android 检查切换按钮状态和背景图像,android,android-togglebutton,Android,Android Togglebutton,我正在尝试检查切换按钮是否已选中(),以及其背景是否等于特定的可绘制按钮。我已经试了两个多小时了,但我还是找不到解决办法 切换按钮设置了一个xml,其中包含更改按钮状态时应该设置的图像 这就是我到目前为止所做的 tbTest1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {
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状态为off,映像设置为testimg1。当用户按下TB键时,状态变为on,图像变为testimg2,并将发送一条已添加的消息(到稍后添加的ArrayList)。当用户再次按下它时,应该会有一个祝酒词,表明它已被删除(从所述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;
}
}
});
将数据发送到活动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.
}
}
getBackground()返回一个可绘制的对象。我不确定它是如何转换为字符串的,但我假设它不会生成名称。R.drawable.testing是一个分配给给定可绘制资源的数字(int),它不是可绘制的对象。xml根据按钮的状态将切换按钮的背景设置为两个不同的图像是否重要?请查看此解决方案是否有效,并发布xml。你打算用你正在实现的代码做什么?也许有更好的方法可以做到这一点。如果你真的想,你可以调用setBackground()并在onClick()中手动设置背景图像。我认为我的解决方案不起作用?原来的解决方案也失败了,那么您是否正在尝试将图像添加到ArrayList?或者您是否希望添加/删除与切换按钮关联的项目?我只是想弄清楚您试图验证后台资源的目的是什么,特别是如果您知道选择器会导致什么情况。我更新了答案,以提供一些关于我是如何思考的见解。
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.
}
}
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();
}
}
});