我的要求: - 选择选项,选项的问题是'是,否,否结果'。选项名称是对应于问题的动态加载。所以这个选项在android中的切换按钮和单选按钮上可用。 但是radion按钮占据高度,切换按钮对我来说更好。
想要在listview中使用两个切换按钮(选项)列出问题。 - 完成。 例如:- 1.第一个切换按钮 - “是” 2.第二个切换按钮 - “否”
条件: - 1.如果单击第一个切换按钮-ON,则第二个切换按钮应处于“OFF”状态,反之亦然。 但是不能通过编程方式完成。 3.请分享您的想法以实现这一功能。
总结: - 想要使用2个切换按钮,如radion按钮。
此致 Naresh T
答案 0 :(得分:1)
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标记为未选中的所选休止符。这将是一个非常简单的逻辑。试一试。
一切顺利。