Android-具有字符串值的自定义状态
我按照上的教程为状态列表实现自定义状态。它适用于布尔属性,但不适用于字符串属性 这是我的源代码:Android-具有字符串值的自定义状态,android,Android,我按照上的教程为状态列表实现自定义状态。它适用于布尔属性,但不适用于字符串属性 这是我的源代码: 国家清单: 属性: 实际上,您缺少一些陈述,请在下面找到解决方案 public class CategoryIconView extends ImageView { // this is used when we want to merge our state with the ones from the system private static fin
- 国家清单:
- 属性:
实际上,您缺少一些陈述,请在下面找到解决方案
public class CategoryIconView extends ImageView {
// this is used when we want to merge our state with the ones from the system
private static final int[] CATEGORY_ID_STATE_SET = {R.attr.category_id};
String categoryID;
public CategoryIconView(Context context) {
super(context);
}
public CategoryIconView(Context context, AttributeSet attrs) {
super(context, attrs);
// R.styleable.CustomStates is the id of the custom state and CustomStates it's the name of your styleable
// from attributes.xml
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CategoryIconView, 0, 0);
// R.styleable.CustomStates_has_new_data is an ID that is created automatically when you create your
// custom state list in attributes.xml
categoryID = typedArray.getString(R.styleable.CategoryIconView_category_id);
}
public CategoryIconView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// R.styleable.CustomStates is the id of the custom state and CustomStates it's the name of your styleable
// from attributes.xml
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CategoryIconView, 0, 0);
// R.styleable.CustomStates_has_new_data is an ID that is created automatically when you create your
// custom state list in attributes.xml
categoryID = typedArray.getString(R.styleable.CategoryIconView_category_id);
}
@Override
public int[] onCreateDrawableState(int extraSpace) {
Log.d(getClass().getSimpleName(), "category ID " + categoryID);
final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
if (categoryID != null) {
mergeDrawableStates(drawableState, CATEGORY_ID_STATE_SET);
}
return drawableState;
}
// this method will be used when you will need to set your image button state in the code
public void setCategoryID(String categoryId) {
this.categoryID = "Three";
refreshDrawableState();
}
}
实际上,您缺少一些陈述,请在下面找到解决方案
public class CategoryIconView extends ImageView {
// this is used when we want to merge our state with the ones from the system
private static final int[] CATEGORY_ID_STATE_SET = {R.attr.category_id};
String categoryID;
public CategoryIconView(Context context) {
super(context);
}
public CategoryIconView(Context context, AttributeSet attrs) {
super(context, attrs);
// R.styleable.CustomStates is the id of the custom state and CustomStates it's the name of your styleable
// from attributes.xml
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CategoryIconView, 0, 0);
// R.styleable.CustomStates_has_new_data is an ID that is created automatically when you create your
// custom state list in attributes.xml
categoryID = typedArray.getString(R.styleable.CategoryIconView_category_id);
}
public CategoryIconView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// R.styleable.CustomStates is the id of the custom state and CustomStates it's the name of your styleable
// from attributes.xml
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CategoryIconView, 0, 0);
// R.styleable.CustomStates_has_new_data is an ID that is created automatically when you create your
// custom state list in attributes.xml
categoryID = typedArray.getString(R.styleable.CategoryIconView_category_id);
}
@Override
public int[] onCreateDrawableState(int extraSpace) {
Log.d(getClass().getSimpleName(), "category ID " + categoryID);
final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
if (categoryID != null) {
mergeDrawableStates(drawableState, CATEGORY_ID_STATE_SET);
}
return drawableState;
}
// this method will be used when you will need to set your image button state in the code
public void setCategoryID(String categoryId) {
this.categoryID = "Three";
refreshDrawableState();
}
}
你能把代码格式化吗?对不起。我不知道为什么我不能用SO编辑器格式化代码。我在使用firefox。你好,我已经给出了一些答案,你能检查一下吗?检查后让我知道。你是否尝试了该解决方案?它是否工作?你能正确格式化代码吗?对不起。我不知道为什么我不能用SO编辑器格式化代码。我在使用firefox。你好,我已经给出了一些答案,你能检查一下吗?检查后让我知道。你是否尝试过该解决方案?它是否有效?
public class CategoryIconView extends ImageView {
// this is used when we want to merge our state with the ones from the system
private static final int[] CATEGORY_ID_STATE_SET = {R.attr.category_id};
String categoryID;
public CategoryIconView(Context context) {
super(context);
}
public CategoryIconView(Context context, AttributeSet attrs) {
super(context, attrs);
// R.styleable.CustomStates is the id of the custom state and CustomStates it's the name of your styleable
// from attributes.xml
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CategoryIconView, 0, 0);
// R.styleable.CustomStates_has_new_data is an ID that is created automatically when you create your
// custom state list in attributes.xml
categoryID = typedArray.getString(R.styleable.CategoryIconView_category_id);
}
public CategoryIconView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// R.styleable.CustomStates is the id of the custom state and CustomStates it's the name of your styleable
// from attributes.xml
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CategoryIconView, 0, 0);
// R.styleable.CustomStates_has_new_data is an ID that is created automatically when you create your
// custom state list in attributes.xml
categoryID = typedArray.getString(R.styleable.CategoryIconView_category_id);
}
@Override
public int[] onCreateDrawableState(int extraSpace) {
Log.d(getClass().getSimpleName(), "category ID " + categoryID);
final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
if (categoryID != null) {
mergeDrawableStates(drawableState, CATEGORY_ID_STATE_SET);
}
return drawableState;
}
// this method will be used when you will need to set your image button state in the code
public void setCategoryID(String categoryId) {
this.categoryID = "Three";
refreshDrawableState();
}
}