用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - android代码库

自己定制的RadioButton组件

2013-04-13 作者: 神马举报

[android]代码库

package com.example.myradiobtn;
 
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
 
/**
 * 控件
 *
 * @author Luo.Yunlongx
 *
 */
public class MyChoose extends LinearLayout {
 
    private Context mContext;
 
    private View mView;
    private RadioGroup mRadioGroup;
    private RadioButton mLeftRadioButton;
    private RadioButton mRightRadioButton;
 
    private LayoutParams mLayoutParams;
 
    /**
     * 构造方法
     *
     * @param context
     * @param attrs
     * @param defStyle
     */
    public MyChoose(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs);
        this.mContext = context;
        init();
    }
 
    /**
     * 构造方法
     *
     * @param context
     * @param attrs
     */
    public MyChoose(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        init();
    }
 
    /**
     * 构造方法
     *
     * @param context
     */
    public MyChoose(Context context) {
        super(context);
        this.mContext = context;
        init();
    }
 
    /**
     * 初始化
     */
    private void init() {
        mView = LayoutInflater.from(mContext).inflate(R.layout.view_mychoose,
                null);
        mLeftRadioButton = (RadioButton) mView
                .findViewById(R.id.mychoose_radioButton1);
        mRightRadioButton = (RadioButton) mView
                .findViewById(R.id.mychoose_radioButton2);
        mRadioGroup = (RadioGroup) mView.findViewById(R.id.mychoose_radioGroup);
        mLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
 
        mView.setLayoutParams(mLayoutParams);
 
        this.setLayoutParams(mLayoutParams);
        this.addView(mView);
 
    }
 
    /**
     * 设置显示的文字
     *
     * @param leftText
     *            左侧文字
     * @param rightText
     *            右侧文字
     */
    public void setTexts(String leftText, String rightText) {
        mLeftRadioButton.setText(leftText);
        mRightRadioButton.setText(rightText);
    }
 
    /**
     * 设置点击事件
     *
     * @param mOnCheckedChange
     */
    public void setOnClick(final IOnCheckedChange mOnCheckedChange, final int id) {
        mRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
 
                switch (checkedId) {
                case R.id.mychoose_radioButton1:
                    /* 改变字的颜色 */
                    mLeftRadioButton.setTextColor(mContext.getResources()
                            .getColor(android.R.color.white));
                    mRightRadioButton.setTextColor(mContext.getResources()
                            .getColor(android.R.color.black));
                    /**
                     * 选中左侧时的事件
                     */
                    if (mOnCheckedChange != null) {
                        mOnCheckedChange.leftOnClick(id);
                    }
                    break;
                case R.id.mychoose_radioButton2:
                    /* 改变字的颜色 */
                    mRightRadioButton.setTextColor(mContext.getResources()
                            .getColor(android.R.color.white));
                    mLeftRadioButton.setTextColor(mContext.getResources()
                            .getColor(android.R.color.black));
                    /**
                     * 选中右侧时的事件
                     */
                    if (mOnCheckedChange != null) {
                        mOnCheckedChange.rightOnClick(id);
                    }
                    break;
                }
            }
        });
    }
 
    /**
     * 是否第一个选中
     *
     * @param leftSelected
     */
    public void setChoose(boolean leftSelected) {
        mLeftRadioButton.setSelected(leftSelected);
        mRightRadioButton.setSelected(!leftSelected);
    }
 
    /**
     *
     * 自己写个接口,处理RadioGroup.setOnCheckedChangeListener相应
     *
     * @author Luo.Yunlongx
     *
     */
    public interface IOnCheckedChange {
        /**
         * 左侧被选中
         *
         * @param id
         *            区分事件源
         */
        public void leftOnClick(int id);
 
        /**
         * 右侧被选中
         *
         * @param id
         *            区分事件源
         */
        public void rightOnClick(int id);
    }
 
}

[代码运行效果截图]


自己定制的RadioButton组件

[源代码打包下载]




网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...