按下时更改按钮上的图像

时间:2013-11-18 21:17:47

标签: android image button

我有一个按钮可以在按下时更改图像,问题是我想要在超过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魔法需要学习?

2 个答案:

答案 0 :(得分:0)

您可以在selectors(xml drawables)文件夹下定义9个不同的res/drawable,就像您当前的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);

答案 1 :(得分:0)

我想到了两种方法:

  1. 如果你更习惯坚持使用XML,你可以创建9 不同的状态列表选择器XML(每个图像对一个), 然后将按钮使用的选择器文件交换为 需要的。
  2. 您可以以编程方式创建要分配的StateListDrawable 根据需要按钮。
  3. 选项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
    在代码中全部完成。
    首先创建drawables的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);
    }