试试 Guillaume Beraudo 的代码:
/*
ToggleImageButton.java
Copyright (C) 2010 Belledonne Communications, Grenoble, France
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package com.home.automation;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Checkable;
import android.widget.ImageButton;
/**
* Image button storing a checked state to display alternating drawables.
* The "checked" drawable is displayed when button is down / checked.
* The "unchecked" drawable is displayed when button is up / unchecked.
*
* @author Guillaume Beraudo
*
*/
public class ToggleImageButton extends ImageButton implements Checkable, OnClickListener{
private boolean checked;
private Drawable stateChecked;
private Drawable stateUnChecked;
private boolean drawablesForBackground;
private OnCheckedChangeListener onCheckedChangeListener;
public ToggleImageButton(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ToggleImageButton);
stateChecked = getResources().getDrawable(array.getResourceId(R.styleable.ToggleImageButton_checked, -1));
stateUnChecked = getResources().getDrawable(array.getResourceId(R.styleable.ToggleImageButton_unchecked, -1));
drawablesForBackground = array.getBoolean(R.styleable.ToggleImageButton_bgdrawables, false);
setBackgroundColor(Color.TRANSPARENT);
setOnClickListener(this);
handleCheckChanged();
}
/**
* @return the stateChecked
*/
public Drawable getStateChecked() {
return stateChecked;
}
/**
* @param stateChecked the stateChecked to set
*/
public void setStateChecked(Drawable stateChecked) {
this.stateChecked = stateChecked;
}
/**
* @return the stateUnChecked
*/
public Drawable getStateUnChecked() {
return stateUnChecked;
}
/**
* @param stateUnChecked the stateUnChecked to set
*/
public void setStateUnChecked(Drawable stateUnChecked) {
this.stateUnChecked = stateUnChecked;
}
public void setChecked(boolean checked) {
this.checked = checked;
handleCheckChanged();
}
public boolean isChecked() {
return checked;
}
private void handleCheckChanged() {
Drawable d = checked? stateChecked : stateUnChecked;
if (drawablesForBackground) {
setBackgroundDrawable(d);
} else {
setImageDrawable(d);
}
requestLayout();
invalidate();
if (onCheckedChangeListener != null) onCheckedChangeListener.onCheckedChanged(this, checked);
}
public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
onCheckedChangeListener = listener;
}
public static interface OnCheckedChangeListener {
void onCheckedChanged(ToggleImageButton button, boolean checked);
}
public void onClick(View v) {
toggle();
}
@Override
public void toggle() {
setChecked(!isChecked());
}
@Override
public boolean performClick() {
toggle();
return super.performClick();
}
}
现在要启用类似广播组的功能,请编写这样的类:
public class CustomMenuItem extends LinearLayout {
private ToggleImageButton menuImage;
private TextView menuText;
private Context context;
int resId;
int resId_Pressed;
int light = 0;
private String name;
public CustomMenuItem(Context context, int resId, int resId_Pressed,int light){
this(context, null);
this.context = context;
this.resId = resId;
this.resId_Pressed = resId_Pressed;
this.light = light;
}
public CustomMenuItem(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomMenuItem(Context context2, int resId, int resId_Pressed,String areaName) {
this(context2, null);
this.context = context2;
this.resId = resId;
this.resId_Pressed = resId_Pressed;
this.name = areaName;
}
public LinearLayout getCustomView(){
LayoutInflater menuItemInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout menuLayout = (LinearLayout) menuItemInflater.inflate(R.layout.menuitem, null);
menuImage = (ToggleImageButton) menuLayout.findViewById(R.id.menuimage);
menuImage.setStateChecked(getResources().getDrawable(resId_Pressed));
menuImage.setStateUnChecked(getResources().getDrawable(resId));
menuImage.setChecked(false);
menuText = (TextView) menuLayout.findViewById(R.id.menutext);
if(light == 0 && name != null)
menuText.setText(name);
else
menuText.setText(light);
return menuLayout;
}
}
在您的 XML 中添加 CustomMenuItem 作为 Layout 并将 ToggleImageButton 添加到它。您可能需要对 CustomMenuItem 类进行某些改进,以处理众多选择中的单个选择。为此,您可以遵循以下简单方法:
- 单击 ToggleImageButton 时,遍历按钮列表并将给定 ID 标记为选中,其余标记为未选中。这将是一个非常简单的逻辑。试一试。
一切顺利。