Here's the code for it:

    /**
     * Return whether the state matches the desired stateSpec.
     *
     * @param stateSpec an array of required (if positive) or
     *        prohibited (if negative) {@link android.view.View} states.
     * @param state a {@link android.view.View} state
     */
    public static boolean stateSetMatches(int[] stateSpec, int state) {
        int stateSpecSize = stateSpec.length;
        for (int i = 0; i < stateSpecSize; i++) {
            int stateSpecState = stateSpec[i];
            if (stateSpecState == 0) {
                // We've reached the end of the cases to match against.
                return true;
            }
            if (stateSpecState > 0) {
                if (state != stateSpecState) {
                   return false;
                }
            } else {
                // We use negative values to indicate must-NOT-match states.
                if (state == -stateSpecState) {
                    // We matched a must-not-match case.
                    return false;
                }
            }
        }
        return true;
    }



On Wednesday, February 27, 2013 4:05:39 AM UTC-6, skink wrote:
>
> i have tried to use StateSet.stateSetMatches(int[], int) and frankly 
> have no idea what is it for. consider the following: 
>
> int[] stateSpec = {1, 1, 1}; 
> int state = 1; 
> // stateSpec == {1, 1, 1} 
> Log.d(TAG, "onCreate " + testStateSetMatches(stateSpec, state)); 
>
> stateSpec[2] = 2; 
> // stateSpec == {1, 1, 2} 
> Log.d(TAG, "onCreate " + testStateSetMatches(stateSpec, state)); 
>
> stateSpec[2] = 0; 
> // stateSpec == {1, 1, 0} 
> Log.d(TAG, "onCreate " + testStateSetMatches(stateSpec, state)); 
>
> and the testStateSetMatches method: 
> private String testStateSetMatches(int[] stateSpec, int state) { 
>     StringBuilder sb = new StringBuilder("{"); 
>     for (int i = 0; i < stateSpec.length; i++) { 
>         int item = stateSpec[i]; 
>         sb.append(item); 
>         if (i + 1 < stateSpec.length) { 
>             sb.append(", "); 
>         } 
>     } 
>     boolean match = StateSet.stateSetMatches(stateSpec, state); 
>     sb.append("} vs ").append(state).append(" == ").append(match); 
>     return sb.toString(); 
> } 
>
> the logcat says: 
> D/TestTag ( 1069): onCreate {1, 1, 1} vs 1 == true 
> D/TestTag ( 1069): onCreate {1, 1, 2} vs 1 == false 
> D/TestTag ( 1069): onCreate {1, 1, 0} vs 1 == true 
>
> so it seems that it returns true if every element in stateSpec is 
> equal to state *or* there is 0 somewhere in between 
>
> i don't really get it, i thought it should match if there is at least 
> one state in stateSet but it's not the case, so what is the point of 
> this method? 
>
> btw StateSet.stateSetMatches(int[], int[]) works as expected but int 
> variant gets me crazy, can anyone explain what is it for? 
>
> pskink 
>

-- 
-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to