选择,检查,并启动Android中的状态之间的区别是什么?区别、状态、Android

2023-09-12 21:36:01 作者:天蝎#玖柒伍陆;座

我想知道有什么不同的国家。我没有找到任何网页上澄清这一点。

解决方案

经过和活性之间的差别其实是相当有趣的。即使是谷歌文档道歉(下文强调):

  

...例如,在列表视图中具有单个或多个选   启用后,在当前选择集中的意见被激活。 (嗯,   是啊,我们深感抱歉这里的术语。)的活化   国家传播到它被设置视图的孩子。

因此​​,这里的区别是:

激活的蜂窝被引入,所以你不能在这之前使用它 激活现在是每个View的属性。它的方法setActivated()和isActivated() 激活传播到视图的孩子们在其中设置 在经过​​围绕一个视图实现可检查的接口。方法setChecked(),器isChecked(),切换()

setChecked()或setActivated(),这取决于如下(摘自Android源$ C ​​$ C)Android版本的ListView(后蜂窝)电话

 如果(mChoiceMode = CHOICE_MODE_NONE和放大器;!&安培;!mCheckStates = NULL){
    如果(孩子的instanceof辨认){
        ((可检查的)子).setChecked(mCheckStates.get(位置));
    }否则,如果(的getContext()。getApplicationInfo()。targetSdkVersion
            > = android.os.Build.VERSION_ codeS.HONEYCOMB){
        child.setActivated(mCheckStates.get(位置));
    }
}
 

请注意的mCheckStates变量。它跟踪其在列表中的位置进行检查/激活。这些是通过可访问的,例如,getCheckedItemPositions()。还需要注意的是调用ListView.setItemChecked()调用上面。换句话说,它可以同样地称为setItemActivated()

在此之前的蜂窝,我们必须实现的解决方法,以反映我们的列表项state_checked。这是因为ListView控件调用setChecked()仅在布局中最顶层的视图(和布局没有实现可检查的)......,它不没有帮助传播。这些变通办法是以下形式:扩展根布局来实现可检查的。在它的构造,递归地找到所有实现可检查的儿童。当setChecked()等等被调用,通过调用到这些意见。如果这些意见落实国家列表可绘制(如复选框)用不同的绘制为state_checked然后选中状态反映在用户界面。

要做到一个很好的背景列表项的蜂窝后,所有你需要做的是有一个状态列表绘制一个可绘制的状态state_activated这样的(和使用setItemChecked()当然):

 <项目的android:STATE_ pressed =真
    机器人:可绘制=@可绘制/ list_item_bg_ pressed/>
<项目安卓state_activated =真
    机器人:可绘制=@可绘制/ list_item_bg_activated/>
<项目机器人:可绘制=@可绘制/ list_item_bg_normal/>
 
Android环境检测与绕过对抗

要做到一个很好的背景,以蜂窝,你会做一些像上面的state_checked,您还需要延长的最顶层,以实现可检查的接口前一个列表项。在你那么需要告诉你的Andr​​oid正在实施的状态是否是真的还是假的通过实施onCreateDrawableState()并调用refreshDrawableState()每当状态更改。

 <项目的android:STATE_ pressed =真
    机器人:可绘制=@可绘制/ list_item_bg_ pressed/>
<项目安卓state_checked =真
    机器人:可绘制=@可绘制/ list_item_bg_checked/>
<项目机器人:可绘制=@可绘制/ list_item_bg_normal/>
 

...和code来实现可检查结合state_checked在RelativeLayout的可能是:

 公共类RelativeLayoutCheckable扩展RelativeLayout的实现可检查{

    公共RelativeLayoutCheckable(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
    }

    公共RelativeLayoutCheckable(上下文的背景下){
        超(上下文);
    }

    私人布尔mChecked = FALSE;

    @覆盖
    保护无效onFinishInflate(){
        super.onFinishInflate();
    }
    @覆盖
    公共布尔器isChecked(){
        返回mChecked;
    }

    @覆盖
    公共无效setChecked(布尔检查){
        mChecked =检查;
        refreshDrawableState();
    }

    私有静态最终诠释[] mCheckedStateSet = {
        android.R.attr.state_checked,
    };

    @覆盖
    保护INT [] onCreateDrawableState(INT extraSpace){
        最终诠释[] drawableState = super.onCreateDrawableState(extraSpace + 1);
        如果(器isChecked()){
            mergeDrawableStates(drawableState,mCheckedStateSet);
        }
        返回drawableState;
    }

    @覆盖
    公共无效切换(){
        setChecked(!mChecked);
    }
}
 

感谢以下内容:

http://sriramramani.word$p$pss.com/2012/11/17/custom-states/

#1: Android的:如何添加自定义按钮状态

#1:Custom可检查显示这是为了响应选择

http://www.charlesharley.com/2012/programming/custom-drawable-states-in-android/

http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

http://blog.marvinlabs.com/2010/10/29/custom-listview-ability-check-items/

I'd like to know what differs those states. I didn't find any webpage clarifying this.

解决方案

The difference between Checked and Activated is actually quite interesting. Even the Google documentation is apologetic (emphasis below added):

... For example, in a list view with single or multiple selection enabled, the views in the current selection set are activated. (Um, yeah, we are deeply sorry about the terminology here.) The activated state is propagated down to children of the view it is set on.

So here is the difference:

Activated was introduced in Honeycomb so you can't use it before that Activated is now a property of every View. It has methods setActivated() and isActivated() Activated propagates to children of the View on which it is set Checked revolves around a View implementing the Checkable interface. Methods setChecked(), isChecked(), toggle()

ListView (after Honeycomb) calls setChecked() OR setActivated() depending on Android version as below (taken from Android source code):

if (mChoiceMode != CHOICE_MODE_NONE && mCheckStates != null) {
    if (child instanceof Checkable) {
        ((Checkable) child).setChecked(mCheckStates.get(position));
    } else if (getContext().getApplicationInfo().targetSdkVersion
            >= android.os.Build.VERSION_CODES.HONEYCOMB) {
        child.setActivated(mCheckStates.get(position));
    }
}

Note the mCheckStates variable. It keeps track of which positions in your list are checked / activated. These are accessible via, for example, getCheckedItemPositions(). Note also that a call to ListView.setItemChecked() invokes the above. In other words, it could equally be called setItemActivated().

Prior to Honeycomb we had to implement workarounds to reflect state_checked in our list items. This is because ListView calls setChecked() ONLY on the topmost View in the layout (and layouts do not implement checkable) ... and it does NOT propagate without help. These workarounds were of the following form: Extend the root layout to implement Checkable. In its constructor, recursively find all the children that implement Checkable. When setChecked() etc... are called, pass the call on to those Views. If those views implement state list drawables (eg a CheckBox) with a different drawable for state_checked then the checked state is reflected in the UI.

To do a nice background to a list item after Honeycomb all you need do is have a state list drawable with a drawable for the state state_activated like this (and use setItemChecked() of course):

<item android:state_pressed="true"
    android:drawable="@drawable/list_item_bg_pressed"/>
<item android:state_activated="true"
    android:drawable="@drawable/list_item_bg_activated"/>
<item android:drawable="@drawable/list_item_bg_normal"/>

To do a nice background to a list item prior to HoneyComb you would do something like the above for state_checked and you ALSO need to extend your topmost view to implement the Checkable interface. Within that you then need to tell Android whether the state you are implementing is true or false by implementing onCreateDrawableState() and calling refreshDrawableState() whenever the state changes.

<item android:state_pressed="true"
    android:drawable="@drawable/list_item_bg_pressed"/>
<item android:state_checked="true"
    android:drawable="@drawable/list_item_bg_checked"/>
<item android:drawable="@drawable/list_item_bg_normal"/>

... and the code to implement Checkable combined with state_checked in a RelativeLayout could be:

public class RelativeLayoutCheckable extends RelativeLayout implements Checkable {

    public RelativeLayoutCheckable(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public RelativeLayoutCheckable(Context context) {
        super(context);
    }

    private boolean mChecked = false;

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
    }
    @Override
    public boolean isChecked() {
        return mChecked;
    }

    @Override
    public void setChecked(boolean checked) {
        mChecked = checked;
        refreshDrawableState();
    }

    private static final int[] mCheckedStateSet = {
        android.R.attr.state_checked,
    };

    @Override
    protected int[] onCreateDrawableState(int extraSpace) {
        final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
        if (isChecked()) {
            mergeDrawableStates(drawableState, mCheckedStateSet);
        }
        return drawableState;
    }    

    @Override
    public void toggle() {
        setChecked(!mChecked);
    }
}

Thanks to the following:

http://sriramramani.wordpress.com/2012/11/17/custom-states/

Stackoverflow: Android: how to add a custom button state

Stackoverflow: Custom Checkable View which responds to Selector

http://www.charlesharley.com/2012/programming/custom-drawable-states-in-android/

http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

http://blog.marvinlabs.com/2010/10/29/custom-listview-ability-check-items/