自定义控件在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; }
上边是自定义控件