Skip to content

Instantly share code, notes, and snippets.

@beilly
Created October 27, 2016 08:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beilly/f2d9c153ec3186c2860c6cb08c9ecbba to your computer and use it in GitHub Desktop.
Save beilly/f2d9c153ec3186c2860c6cb08c9ecbba to your computer and use it in GitHub Desktop.
IconFont控件
package com.benli.view;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.TextView;
import com.vcredit.cp.R;
import com.vcredit.global.AppConfig;
import com.vcredit.utils.CommonUtils;
/**
* Created by Family on 2016/3/7.
*/
public class IconFontView extends TextView {
private Context context;
private boolean isFirst = true;
private String text;
private int textColor;
private String clicked_text;
private int clicked_textColor;
public IconFontView(Context context) {
this(context, null);
}
public IconFontView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public IconFontView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;
init(attrs);
}
/** 初始化 */
private void init(AttributeSet attrs){
// 设置字体类型
setTypeface(Typeface.createFromAsset(context.getAssets(), AppConfig.ICONFONT_PATH));
// 获取配置信息(text && textColor)
getAttrs(attrs);
}
/** 获得配置信息 */
private void getAttrs(AttributeSet attrs) {
TypedArray taCustom = context.obtainStyledAttributes(attrs, R.styleable.IconFontViewAttr);
clicked_text = taCustom.getString(R.styleable.IconFontViewAttr_clicked_text);
clicked_textColor = taCustom.getColor(R.styleable.IconFontViewAttr_clicked_textColor, 0);
taCustom.recycle();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN){
text = getText().toString();
textColor = getCurrentTextColor();
}
return super.onTouchEvent(event);
}
@Override
protected void dispatchSetPressed(boolean pressed) {
super.dispatchSetPressed(pressed);
if (isDuplicateParentStateEnabled()) {
CommonUtils.LOG_D(IconFontView.class, "dispatchSetPressed" + pressed);
if (pressed){
if(clicked_text != null)
this.setText(clicked_text);
if(clicked_textColor != 0)
this.setTextColor(clicked_textColor);
}else {
this.setText(text);
this.setTextColor(textColor);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment