【问题标题】:Android-Two toggle buttons in list view, its working like radio buttonAndroid-列表视图中的两个切换按钮,其工作方式类似于单选按钮
【发布时间】:2014-09-10 05:05:55
【问题描述】:

我的要求:- 带有选择选项的问题,选项是“是,否,否结果”。选项名称是根据问题自动加载的。所以这个选项在 android 的切换按钮和单选按钮上可用。 但是radion按钮占用空间很大,toggle按钮对我来说更好。

想要在列表视图中使用两个切换按钮(选项)列出问题。 - 完全的。 例如:- 1.第一个切换按钮-“是” 2. 第二个切换按钮 - “否”

条件:- 1.如果点击第一个切换按钮-'ON',第二个切换按钮应该是'OFF',反之亦然。 2.但不能通过程序来完成。 3. 请分享您的想法来实现该功能。

总结:- 想要使用 2 个切换按钮,例如单选按钮。

问候, 纳雷什T

【问题讨论】:

    标签: android listview checkbox togglebutton radio-group


    【解决方案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 标记为选中,其余标记为未选中。这将是一个非常简单的逻辑。试一试。

    一切顺利。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-09
      • 1970-01-01
      • 2018-02-13
      • 2012-10-16
      • 2012-05-19
      • 1970-01-01
      • 2019-11-10
      • 1970-01-01
      相关资源
      最近更新 更多