【问题标题】:change image on button when pressed按下按钮时更改图像
【发布时间】:2013-11-19 07:18:49
【问题描述】:

我有一个按钮可以在按下时更改图像,问题是我想在不止 2 个之间更改它。

该按钮设置为用作常规图像,当按下它时,它会被同一图像的稍暗版本所取代。

很遗憾,我想拥有多个基本图像。我想实现 9 个不同的基本图像(将由代码更改),以及在按下按钮时显示 9 个相应的图像。可绘制的 XML 文件如下所示

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/pressedbrownie" android:state_pressed="true"/>
<item android:drawable="@drawable/brownie0"/>
</selector>

但我想将 @drawable/pressedbrownie 设置为 9 个不同的值之一,这取决于程序决定的内容,并且与 @drawable/brownie0 相同。

primaryPage.java 文件可以检测到按钮何时被按下,还是我需要学习一些 XML 魔法?

【问题讨论】:

  • 按下按钮时是否要随机显示 9 张图片中的任何一张??

标签: android image button


【解决方案1】:

想到了两种方法:

  1. 如果您更愿意坚持使用 XML,您可以创建 9 不同的状态列表选择器 XML(每个图像对一个), 然后将按钮正在使用的选择器文件交换为 需要。
  2. 您可以通过编程方式创建一个 StateListDrawable 以分配给 按钮。

选项 1:
创建一系列状态列表。

brownie_selector0.XML:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/pressedbrownie0" android:state_pressed="true"/>
<item android:drawable="@drawable/brownie0"/>
</selector>

brownie_selector1.XML:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/pressedbrownie1" android:state_pressed="true"/>
<item android:drawable="@drawable/brownie1"/>
</selector>

...等等。

然后更改分配给按钮的状态列表。例如使用开关:

...
Button brownieButton = (Button)findViewById(R.id.myBrownieButton);
switch (brownieType) {  // brownieType would be an int indicating which State List is needed
case 0:
    brownieButton.setBackground(getResources().getDrawable(R.id.brownie_selector0));
    break;
case 1:
    brownieButton.setBackground(getResources().getDrawable(R.id.brownie_selector1));
    break;
...
}

选项 2
全部在代码中完成。
首先创建可绘制对象的 SparseIntArrays:

int drawableId;
String drawableName;
SparseIntArray brownieNormal = new SparseIntArray(totalNumberOfBrownies);
SparseIntArray browniePressed = new SparseIntArray(totalNumberOfBrownies);
// iterate through all the required brownie pairs
for (int i = 0; i < totalNumberOfBrownies; i++) {
    // generate the required file name
    drawableName = "brownie" + i;  
    // find the id of the required drawable using the generated file name
    drawableId = getResources().getIdentifier(drawableName, "drawable", getPackageName());
    // put the resulting drawable id into the SparseIntArray
    brownieNormal.put(n, drawableId);
    // repeat for the pressed images
    drawableName = "pressedBrownie" + i;
    drawableId = getResources().getIdentifier(drawableName, "drawable", getPackageName());
    browniePressed.put(n, drawableId);
}  

然后根据需要创建并分配一个合适的 StateListDrawable。

private StateListDrawable mBrownieStates;
...

// method to deal with when mBrownieType changes
private void changeBrownieButtonStateListDrawable(int brownieType);
    // reset mBrownieStates to a fresh StateListDrawable
    mBrownieStates = new StateListDrawable();
    // add the drawable for the pressed state
    mBrownieStates.addState(new int[] {android.R.attr.state_pressed}, browniePressed.get(brownieType));
    // add the drawable for the default state
    mBrownieStates.addState(new int[] {}, brownieNormal.get(brownieType));

    // update the button with the newly defined StateListDrawable
    Button brownieButton = (Button)findViewById(R.id.myBrownieButton);
    brownieButton.setBackground(mBrownieStates);
}

【讨论】:

  • 以下选项 1:当我到达实现 switch case 的部分时,brownieButton.setBackground(getResources().getDrawable(R.id.brownie_selector0)); 抛出一个错误,brownie_selector(X) 没有得到解决。自然地,XML 文件在可绘制 DPI 文件夹中的名称相同。有趣的是,它们仍然被视为有效资源。尝试使用brownieButton.setBackgroundResource(R.drawable.brownie_selector0); 只会导致运行时崩溃并导致堆栈溢出
  • 为了帮助查明发生了什么,请更新您的问题,以包括您尝试过的修改后的代码和生成的 LogCats。信息越多越好。我在自己的问题中发现,问题通常不是我认为的问题,其他人会在额外信息中发现它。
  • 感谢您的提议,但我发现了错误。事实证明,我的变量命名错误和 IDE 重命名错误导致 XML 文件在无限循环中调用自身
  • 不用担心。很高兴你成功了。
【解决方案2】:

你可以在res/drawable文件夹下定义9个不同的selectors(xml drawables),就像你现在的selector(xml drawable)一样。将不同的drawables 设置为它们的按下状态。

并通过以编程方式将它们设置为背景来使用这些选择器之一。

// I assume you have a button an instance of Button in your layout with id button.
Button button = (Button)findViewById(R.id.button);
// choose on of the selectors with an if/else or switch/case statement 
// and set as the background of your button.
button.setBackgroundResource(R.drawable.button_bg_1);
// or 
button.setBackgroundResource(R.drawable.button_bg_9);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-06
    • 2011-01-11
    • 2013-06-25
    • 1970-01-01
    • 1970-01-01
    • 2020-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多