465 lines
13 KiB
Java
465 lines
13 KiB
Java
/*
|
||
*******************************************
|
||
* File: WidgetTools.java
|
||
* Author: Lijc
|
||
* Date: 2015-3-20
|
||
* Company: BlueMobi
|
||
********************************************/
|
||
package com.fenghoo.seven.utils;
|
||
|
||
import android.app.Activity;
|
||
import android.content.Context;
|
||
import android.content.pm.PackageInfo;
|
||
import android.content.pm.PackageManager;
|
||
import android.content.pm.PackageManager.NameNotFoundException;
|
||
import android.graphics.Bitmap;
|
||
import android.graphics.BitmapFactory;
|
||
import android.graphics.Paint;
|
||
import android.text.Editable;
|
||
import android.text.Selection;
|
||
import android.text.Spannable;
|
||
import android.text.SpannableString;
|
||
import android.text.TextUtils;
|
||
import android.text.TextWatcher;
|
||
import android.text.style.ImageSpan;
|
||
import android.util.DisplayMetrics;
|
||
import android.util.SparseArray;
|
||
import android.view.View;
|
||
import android.widget.Button;
|
||
import android.widget.CheckBox;
|
||
import android.widget.EditText;
|
||
import android.widget.RadioButton;
|
||
import android.widget.TextView;
|
||
import android.widget.Toast;
|
||
|
||
/**
|
||
*
|
||
* @ClassName: WidgetTools
|
||
* @Description: TODO(描述: Widget工具类)
|
||
* @author Lijc
|
||
* @Company: BlueMobi
|
||
* @date 2015年4月3日 下午6:04:29
|
||
* @version V1.0
|
||
* <p>
|
||
* >>>使用说明:
|
||
* <p>
|
||
* 1.setText()可用于TextView,Button,CheckBox,RadioButton,EditText;
|
||
* <p>
|
||
* 2.setUnderline() 在文字下面设置下划线
|
||
* <p>
|
||
* 3.setDeleteline() 在文字上设置删除线
|
||
* <p>
|
||
* 4.getText() 获取文字
|
||
* <p>
|
||
* 5.get() ViewHolder 简洁模式
|
||
* <p>
|
||
* 6.spanText() 文字前面插入图片
|
||
* <p>
|
||
* 7.DisplayImage()获取设置图片
|
||
* <p>
|
||
* 8.formatMoney() 格式化显示金额
|
||
* <p>
|
||
* 9.getAppCode() 获取版本code
|
||
*/
|
||
public class ToolsUtils {
|
||
|
||
private static long lastClickTime;
|
||
|
||
/**
|
||
* 连续点击
|
||
*
|
||
* @return
|
||
*/
|
||
public static boolean isFastDoubleClick() {
|
||
long time = System.currentTimeMillis();
|
||
long timeD = time - lastClickTime;
|
||
if (0 < timeD && timeD < 1000) {
|
||
return true;
|
||
}
|
||
lastClickTime = time;
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
* @Title: getText @Description: TODO @param tv @param text @return
|
||
* void @throws
|
||
*/
|
||
public static String getText(View v) {
|
||
if (v instanceof TextView) {// TextView
|
||
return ((TextView) v).getText().toString();
|
||
}
|
||
if (v instanceof Button) {// Button
|
||
return ((Button) v).getText().toString();
|
||
}
|
||
if (v instanceof CheckBox) {// CheckBox
|
||
return ((CheckBox) v).getText().toString();
|
||
}
|
||
if (v instanceof RadioButton) {// RadioButton
|
||
return ((RadioButton) v).getText().toString();
|
||
}
|
||
if (v instanceof EditText) {// EditText
|
||
return ((EditText) v).getText().toString();
|
||
}
|
||
return "";
|
||
}
|
||
|
||
/**
|
||
* @Title: setText @Description: TODO 设置TextView的text 为空时默认显示 -- @param
|
||
* tv @param text @return void @throws
|
||
*/
|
||
public static void setText(View v, String text) {
|
||
setText(v, text, "");
|
||
}
|
||
|
||
/**
|
||
* @Title: setText @Description: TODO 设置TextView的text @param tv @param
|
||
* text @param defaultText 默认显示 @return void @throws
|
||
*/
|
||
public static void setText(View v, String text, String defaultText) {
|
||
if (TextUtils.isEmpty(text))
|
||
text = defaultText;
|
||
if (v instanceof TextView) {// TextView
|
||
((TextView) v).setText(text);
|
||
return;
|
||
}
|
||
if (v instanceof Button) {// Button
|
||
((Button) v).setText(text);
|
||
return;
|
||
}
|
||
if (v instanceof CheckBox) {// CheckBox
|
||
((CheckBox) v).setText(text);
|
||
return;
|
||
}
|
||
if (v instanceof RadioButton) {// RadioButton
|
||
((RadioButton) v).setText(text);
|
||
return;
|
||
}
|
||
if (v instanceof EditText) {// EditText
|
||
((EditText) v).setText(text);
|
||
return;
|
||
}
|
||
|
||
}
|
||
|
||
/**
|
||
* @Title: setText @Description: TODO String资源文件的format方法 @param @param
|
||
* tv @param @param context @param @param text @param @param
|
||
* resId @return void @throws
|
||
*/
|
||
public static void setText(View v, Context context, String text, int resId) {
|
||
String format = context.getResources().getString(resId);
|
||
setText(v, String.format(format, text));
|
||
}
|
||
|
||
/**
|
||
* @Title: setText @Description: TODO String资源文件的format方法 @param tv @param
|
||
* context @param i @param resId @return void @throws
|
||
*/
|
||
public static void setText(View v, Context context, int i, int resId) {
|
||
String format = context.getResources().getString(resId);
|
||
setText(v, String.format(format, i));
|
||
}
|
||
|
||
/**
|
||
* @Title: setText @Description: TODO String资源文件的format方法 @param @param
|
||
* tv @param @param context @param @param text1 @param @param
|
||
* text2 @param @param resId @return void @throws
|
||
*/
|
||
public static void setText(View v, Context context, String text1, String text2, int resId) {
|
||
String format = context.getResources().getString(resId);
|
||
setText(v, String.format(format, text1, text2));
|
||
}
|
||
|
||
/**
|
||
* @Title: setText @Description: TODO String资源文件的format方法 @param @param
|
||
* tv @param @param context @param @param i1 @param @param
|
||
* i2 @param @param resId @return void @throws
|
||
*/
|
||
public static void setText(View v, Context context, int i1, int i2, int resId) {
|
||
String format = context.getResources().getString(resId);
|
||
setText(v, String.format(format, i1, i2));
|
||
}
|
||
|
||
/**
|
||
* @Title: setTextViewStyle @Description: TODO 优惠价格 @param tv @param
|
||
* text @return void @throws
|
||
*/
|
||
public static void setTextViewStyle(TextView tv) {
|
||
tv.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
|
||
}
|
||
|
||
/**
|
||
* @Title: setUnderline @Description: TODO 下划线 @param tv @param text @return
|
||
* void @throws
|
||
*/
|
||
public static void setUnderline(View v) {
|
||
if (v instanceof TextView) {// TextView
|
||
((TextView) v).getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
|
||
((TextView) v).getPaint().setAntiAlias(true);
|
||
return;
|
||
}
|
||
if (v instanceof Button) {// Button
|
||
((Button) v).getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
|
||
((Button) v).getPaint().setAntiAlias(true);
|
||
return;
|
||
}
|
||
if (v instanceof CheckBox) {// CheckBox
|
||
((CheckBox) v).getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
|
||
((CheckBox) v).getPaint().setAntiAlias(true);
|
||
return;
|
||
}
|
||
if (v instanceof RadioButton) {// RadioButton
|
||
((RadioButton) v).getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
|
||
((RadioButton) v).getPaint().setAntiAlias(true);
|
||
return;
|
||
}
|
||
if (v instanceof EditText) {// EditText
|
||
((EditText) v).getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
|
||
((EditText) v).getPaint().setAntiAlias(true);
|
||
return;
|
||
}
|
||
|
||
}
|
||
|
||
/**
|
||
* @Title: setDelline @Description: TODO 删除线 @param tv @param text @return
|
||
* void @throws
|
||
*/
|
||
public static void setDeleteline(View v) {
|
||
if (v instanceof TextView) {// TextView
|
||
((TextView) v).getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
|
||
((TextView) v).getPaint().setAntiAlias(true);
|
||
return;
|
||
}
|
||
if (v instanceof Button) {// Button
|
||
((Button) v).getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
|
||
((Button) v).getPaint().setAntiAlias(true);
|
||
return;
|
||
}
|
||
if (v instanceof CheckBox) {// CheckBox
|
||
((CheckBox) v).getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
|
||
((CheckBox) v).getPaint().setAntiAlias(true);
|
||
return;
|
||
}
|
||
if (v instanceof RadioButton) {// RadioButton
|
||
((RadioButton) v).getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
|
||
((RadioButton) v).getPaint().setAntiAlias(true);
|
||
return;
|
||
}
|
||
if (v instanceof EditText) {// EditText
|
||
((EditText) v).getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
|
||
((EditText) v).getPaint().setAntiAlias(true);
|
||
return;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @TODO ViewHodler简写模式
|
||
* @param view
|
||
* @param id
|
||
* @return
|
||
*/
|
||
public <T extends View> T get(View view, int id) {
|
||
SparseArray<View> viewHolder = (SparseArray<View>) view.getTag();
|
||
if (viewHolder == null) {
|
||
viewHolder = new SparseArray<>();
|
||
view.setTag(viewHolder);
|
||
}
|
||
View childView = viewHolder.get(id);
|
||
if (childView == null) {
|
||
childView = view.findViewById(id);
|
||
viewHolder.put(id, childView);
|
||
}
|
||
return (T) childView;
|
||
}
|
||
|
||
/**
|
||
*
|
||
* @param context
|
||
* @param v
|
||
* @param drawableId
|
||
* @param text
|
||
*/
|
||
public static void spanText(Context context, View v, int drawableId, String text) {
|
||
Bitmap b = BitmapFactory.decodeResource(context.getResources(), drawableId);
|
||
ImageSpan imgSpan = new ImageSpan(context, b);
|
||
SpannableString spanString = new SpannableString("ic");
|
||
spanString.setSpan(imgSpan, 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
|
||
if (v instanceof TextView) {// TextView
|
||
((TextView) v).setText(spanString);
|
||
((TextView) v).append(text);
|
||
return;
|
||
}
|
||
if (v instanceof Button) {// Button
|
||
((Button) v).setText(spanString);
|
||
((Button) v).append(text);
|
||
return;
|
||
}
|
||
if (v instanceof CheckBox) {// CheckBox
|
||
((CheckBox) v).setText(spanString);
|
||
((CheckBox) v).append(text);
|
||
return;
|
||
}
|
||
if (v instanceof RadioButton) {// RadioButton
|
||
((RadioButton) v).setText(spanString);
|
||
((RadioButton) v).append(text);
|
||
return;
|
||
}
|
||
if (v instanceof EditText) {// EditText
|
||
((EditText) v).setText(spanString);
|
||
((EditText) v).append(text);
|
||
return;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取应用版本code
|
||
*
|
||
* @param ctx
|
||
* @return
|
||
*/
|
||
public static int getAppCode(Context ctx) {
|
||
try {
|
||
PackageManager packageManager = ctx.getPackageManager();
|
||
PackageInfo packInfo = packageManager.getPackageInfo(ctx.getPackageName(), 0);
|
||
return packInfo.versionCode;
|
||
} catch (NameNotFoundException e) {
|
||
// TODO Auto-generated catch block
|
||
return 0;
|
||
}
|
||
|
||
}
|
||
|
||
/**
|
||
* 2 * 获取版本号
|
||
* 3 * @return 当前应用的版本号
|
||
* 4
|
||
*/
|
||
public static String getVersion(Context ctx) {
|
||
try {
|
||
PackageManager manager = ctx.getPackageManager();
|
||
PackageInfo info = manager.getPackageInfo(ctx.getPackageName(), 0);
|
||
return "V"+info.versionName;
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
return "";
|
||
}
|
||
}
|
||
|
||
/**
|
||
* px转dp
|
||
*
|
||
* @param poContext
|
||
* @param pxValue
|
||
* @return
|
||
*/
|
||
public static int px2dip(Context poContext, float pxValue) {
|
||
final float scale = poContext.getResources().getDisplayMetrics().density;
|
||
return (int) (pxValue / scale + 0.5f);
|
||
}
|
||
|
||
/**
|
||
* dp转px
|
||
*
|
||
* @param poContext
|
||
* @param dpValue
|
||
* @return
|
||
*/
|
||
public static int dip2px(Context poContext, float dpValue) {
|
||
final float scale = poContext.getResources().getDisplayMetrics().density;
|
||
return (int) (dpValue * scale + 0.5f);
|
||
}
|
||
|
||
/**
|
||
* 将px值转换为sp值,保证文字大小不变
|
||
*
|
||
* @param pxValue
|
||
* @param
|
||
* (DisplayMetrics类中属性scaledDensity)
|
||
* @return
|
||
*/
|
||
public static int px2sp(Context context, float pxValue) {
|
||
final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
|
||
return (int) (pxValue / fontScale + 0.5f);
|
||
}
|
||
|
||
public static int getScreenWidth(Activity poActivity) {
|
||
DisplayMetrics metric = new DisplayMetrics();
|
||
poActivity.getWindowManager().getDefaultDisplay().getMetrics(metric);
|
||
// int width = metric.widthPixels; // 屏幕宽度(像素)
|
||
// int height = metric.heightPixels; // 屏幕高度(像素)
|
||
// float density = metric.density; // 屏幕密度(0.75 / 1.0 / 1.5)
|
||
// int densityDpi = metric.densityDpi;
|
||
return metric.widthPixels;
|
||
}
|
||
|
||
public static int getScreenHeight(Activity poActivity) {
|
||
DisplayMetrics metric = new DisplayMetrics();
|
||
poActivity.getWindowManager().getDefaultDisplay().getMetrics(metric);
|
||
// int width = metric.widthPixels; // 屏幕宽度(像素)
|
||
// int height = metric.heightPixels; // 屏幕高度(像素)
|
||
// float density = metric.density; // 屏幕密度(0.75 / 1.0 / 1.5)
|
||
// int densityDpi = metric.densityDpi;
|
||
return metric.heightPixels;
|
||
}
|
||
|
||
|
||
/**
|
||
* 设置 编辑文本的长度
|
||
* @param mContext
|
||
* @param editText
|
||
* @param lens
|
||
*/
|
||
public static void setEditTextLenth(final Activity mContext,final EditText editText,final int lens) {
|
||
/**
|
||
* 设置商品描述的字数最多为140个字符
|
||
*/
|
||
editText.addTextChangedListener(new TextWatcher() {
|
||
@Override
|
||
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
||
// TODO Auto-generated method stub
|
||
Editable editable = editText.getText();
|
||
int len = editable.length();
|
||
editText.setText("(" + editable.length() + "/" + 140 + ")");
|
||
if (len > lens) {
|
||
Toast.makeText(mContext, "最多140个字符", Toast.LENGTH_SHORT).show();
|
||
int selEndIndex = Selection.getSelectionEnd(editable);
|
||
String str = editable.toString();
|
||
String newStr = str.substring(0, 140);// 截取新字符串
|
||
editText.setText(newStr);
|
||
editable = editText.getText();
|
||
int newLen = editable.length();// 新字符串的长度
|
||
// 旧光标位置超过字符串长度
|
||
if (selEndIndex > newLen) {
|
||
selEndIndex = editable.length();
|
||
}
|
||
Selection.setSelection(editable, selEndIndex);// 设置新光标所在的位置
|
||
}
|
||
}
|
||
@Override
|
||
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
||
// TODO Auto-generated method stub
|
||
}
|
||
|
||
@Override
|
||
public void afterTextChanged(Editable s) {
|
||
// TODO Auto-generated method stub
|
||
String strTmp = s.toString();
|
||
// String tmp = EmojiFilter.filterEmoji(strTmp);
|
||
// if (!strTmp.trim().equals("") && !strTmp.equals(tmp)) {
|
||
// editText.setText(tmp);
|
||
// editText.invalidate();
|
||
// Toast.makeText(mContext, "暂不支持表情输入", Toast.LENGTH_SHORT).show();
|
||
// }
|
||
if (s instanceof Spannable) {// 改变光标的位置到最后
|
||
Spannable spanText = (Spannable) s;
|
||
Selection.setSelection(spanText, s.length());
|
||
}
|
||
}
|
||
});
|
||
}
|
||
|
||
|
||
|
||
}
|