| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2087 人关注过本帖
标题:自定义控件在activity/fragment中怎么获取id添加点击事件
只看楼主 加入收藏
winner1995
Rank: 2
等 级:论坛游民
帖 子:64
专家分:41
注 册:2015-10-11
结帖率:88.89%
收藏
 问题点数:0 回复次数:0 
自定义控件在activity/fragment中怎么获取id添加点击事件
程序代码:
package com.sto.courier.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v7.widget.SwitchCompat;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sto.courier.R;

/**
 * Created by Administrator on 2017-06-27.
 */

public class VSettingItem extends LinearLayout implements View.OnClickListener {

    private Context mContext ;

    public static interface OnSettingItemClickListener {
        void onSettingItemClick(View view);
    }

    public VSettingItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        setOrientation(HORIZONTAL);
        setBackgroundColor(mContext.getResources().getColor(R.color.white));
        inflate(context, R.layout.v_setting_item, this);

        ivIcon = (ImageView) findViewById(android.R.id.icon);

        tvSettingItem = (TextView) findViewById(android.R.id.text1);

        tvValueInfo = (TextView) findViewById(android.R.id.text2);

        ivMoreArrow = (ImageView) findViewById(R.id.arrow);

        btnSwitch = (SwitchCompat) findViewById(R.id.btn_switch);

        if (attrs != null) {
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.VSettingItem);
            int iconResId = ta.getResourceId(R.styleable.VSettingItem_item_icon, 0);
            CharSequence itemName = ta.getText(R.styleable.VSettingItem_item_name);
            boolean showArrow = ta.getBoolean(R.styleable.VSettingItem_show_arrow, false);
            boolean showSwitch = ta.getBoolean(R.styleable.VSettingItem_show_switch, false);

            if (!showSwitch) {
                setItem(iconResId, itemName, showArrow);
            } else {
                // setSwitchItem(itemName, false);
            }

            ta.recycle();
        }

        findViewById(R.id.wt_setting_item_layout).setOnClickListener(this);
    }

    public void setOnSettingItemClickListener(OnSettingItemClickListener listener) {
        this.listener = listener;
    }

    @Override
    public void onClick(View v) {
        if (listener != null) {
            listener.onSettingItemClick(this);
        } else {
            handleClick();
        }
    }

    protected void handleClick() {

    }

    @Override
    public void setEnabled(boolean enabled) {
        findViewById(R.id.wt_setting_item_layout).setEnabled(enabled);
        super.setEnabled(enabled);

        tvSettingItem.setTextColor(mContext.getResources().getColor(enabled ? R.color.t16b : R.color.t14d));
    }


    public void setValueInfo(String valueInfo) {
        tvValueInfo.setText(valueInfo);
    }

    public String getValueInfo() {
        return tvValueInfo.getText().toString();
    }

    public void setCheckied(boolean checked) {
        btnSwitch.setChecked(checked);
    }

    public boolean isChecked() {
        return btnSwitch.isChecked();
    }

    public void setItem(int iconResId, CharSequence item, boolean showArrow) {
        if (iconResId != 0) {
            ivIcon.setVisibility(VISIBLE);
            ivIcon.setImageResource(iconResId);
        } else {
            ivIcon.setVisibility(GONE);
        }
        tvSettingItem.setText(item);
        ivMoreArrow.setVisibility(showArrow ? VISIBLE : GONE);
        ivMoreArrow.setImageResource(R.mipmap.icon_tright);
        btnSwitch.setVisibility(GONE);
    }

    protected void setItem(int textId, boolean showArrow) {
        ivIcon.setVisibility(GONE);
        tvSettingItem.setText(textId);
        ivMoreArrow.setVisibility(showArrow ? VISIBLE : GONE);
        ivMoreArrow.setImageResource(R.mipmap.icon_tright);
        btnSwitch.setVisibility(GONE);
    }

    protected void setSwitchItem(int textId, boolean defaultCheck) {
        ivIcon.setVisibility(GONE);
        tvSettingItem.setText(textId);
        ivMoreArrow.setVisibility(GONE);
        btnSwitch.setVisibility(VISIBLE);
        btnSwitch.setChecked(defaultCheck);
    }

    public void setSettingItemColor(int res) {
        tvSettingItem.setTextColor(res);
    }

    public void setSettingItemValue(String value) {
        tvSettingItem.setText(value);
    }

    public String getSettingItemValue() {
        return tvSettingItem.getText().toString();
    }

    private ImageView ivIcon;

    private TextView tvSettingItem;

    private TextView tvValueInfo;

    private ImageView ivMoreArrow;

    private SwitchCompat btnSwitch;

    private OnSettingItemClickListener listener;
}

上边是自定义控件
搜索更多相关主题的帖子: import android Context public void 
2018-01-11 21:25
快速回复:自定义控件在activity/fragment中怎么获取id添加点击事件
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.035270 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved