如何实现自定义选择状态的CustomView? [英] How to implement a CustomView with custom selector states?
问题描述
我想创建一个 CustomView
显示图像。在点击视图应改变其状态。应该有三种状态(关,设置,NotSet,要)视图可以重新present。我想用的选择在XML中做到这一点。它并不necesseraliy需要是一个定制的选择器。我可以重复选择的三种状态(这并不重要,如果国家的名称不同的话)。
I want to create a CustomView
that displays an image. On click the view should change its state. There should be three states (off, set, notset) the view can represent. I want to do this with a selector in XML. It does not necesseraliy need to be a custom selector. I could reuse three states of the selector (it does not matter if the names of the state are different then).
有没有实现这一目标的好方法?
Is there a good way to achieve this?
推荐答案
万一您的问题并没有解决自己呢。我做状态的改变与新实施的Android 按钮
。
Just in case your issue has not resolved itself yet. I do the changing of states with a new implementation of the Android Button
.
的状态中的.xml通过的选择定义和设置。在这里,在 attrs.xml 文件中定义的三种状态:
The states are defined in .xml and set via a selector. Here the three states defined in the attrs.xml file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="states">
<attr name="state_on" format="boolean" />
<attr name="state_off" format="boolean" />
<attr name="state_notset" format="boolean" />
</declare-styleable>
</resources>
和选择器( statebutton_selector.xml )在可绘制文件夹中: (我认为让一个特定的状态会自动禁用其他国家 - 的可绘制像STATE_ON只是png格式图片重新presenting各州)
And the selector (statebutton_selector.xml) inside the drawables folder: (I assume that enabling a specific state automatically disables the other states - the drawables like "state_on" are just .png images representing the individual states)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.example.statebuttontest">
<item
app:state_on="true"
app:state_off="false"
app:state_notset="false"
android:drawable="@drawable/state_on" />
<item
app:state_on="false"
app:state_off="true"
app:state_notset="false"
android:drawable="@drawable/state_off" />
<item
app:state_on="false"
app:state_off="false"
app:state_notset="true"
android:drawable="@drawable/state_notset" />
</selector>
此外,要知道引用您正确的包名在选择XML文件,如你的清单文件中指出:
Also, be aware to reference your correct package name in the selector xml file, as stated in your Manifest file:
xmlns:app="http://schemas.android.com/apk/res/com.example.statebuttontest"
最后, StateButton
类扩展按钮
。用一个简单的 OnClickListener
的状态改变。我也实现了一个 OnStateChangedListener
的比如可以通过包含该按钮,将被调用时的状态变化的活动来实现。
And finally, the StateButton
class that extends Button
. With a simple OnClickListener
the state is changed. I also implemented an OnStateChangedListener
that for example can be implemented by an Activity that contains the Button and will be called whenever the state changes.
状态本身就是 onCreateDrawableState(...)
方法内完成被称为的变化自动每次按钮被点击。 extraspace + 1的意思,那会有drawableStates阵列内的一个附加状态。
The changing of the state itself is done inside the onCreateDrawableState(...)
method that is called automatically every time the Button is clicked. "extraspace + 1" means, that there will be one additional state inside the drawableStates array.
public class StateButton extends Button implements OnClickListener {
private static final int[] mStates = { R.attr.state_notset, R.attr.state_on, R.attr.state_off };
private int mStateIndex = 0; // first state is "notset"
private OnStateChangedListener mListener;
public StateButton(Context context, AttributeSet attrs) {
super(context, attrs);
setOnClickListener(this);
}
@Override
public void onClick(View v) {
changeState();
}
public void changeState() {
mStateIndex = (mStateIndex+1) % mStates.length;
// notify listener
if(mListener != null) mListener.onStateChanged(mStates[mStateIndex]);
}
@Override
protected int[] onCreateDrawableState(int extraSpace) {
final int[] drawableState = super.onCreateDrawableState(extraSpace+1);
int [] state = { mStates[mStateIndex] };
mergeDrawableStates(drawableState, state);
return drawableState;
}
public void setOnStateChangedListener(OnStateChangedListener l) {
this.mListener = l;
}
}
最后但并非最不重要的,设置选择为背景你的按钮
:
<com.example.statebuttontest.StateButton
android:id="@+id/stateButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/statebutton_selector"
android:text="" />
在活动的一个例子
(与监听器):
public class MainActivity extends Activity implements OnStateChangedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StateButton s = (StateButton) findViewById(R.id.stateButton1);
s.setOnStateChangedListener(this);
}
@Override
public void onStateChanged(int state) {
Log.i("Main", "State changed to: " + getResources().getResourceEntryName(state));
}
}
这篇关于如何实现自定义选择状态的CustomView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!