String相关
手机号加密显示
public static String passPhone(String phone) {
if(null!=phone&&phone.length() ==11){
return phone.substring(0, 3) + "****" + phone.substring(7);
}else{
return phone;
}
}
屏幕相关
/**
* 获取屏幕的宽度
* */
public static int getScreenWidth(Context context) {
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
return wm.getDefaultDisplay().getWidth();
}
/**
* 屏幕的高度
* */
public static int getScreenHeight(Context context) {
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
return wm.getDefaultDisplay().getHeight();
}
/**
* 将px值转换为dip或dp值,保证尺寸大小不变
*
* @param pxValue
* ( scale DisplayMetrics类中属性density)
* @return
*/
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
/**
* 将dip或dp值转换为px值,保证尺寸大小不变
*
* @param dipValue
*
* (DisplayMetrics类中属性density)
* @return
*/
public static int dip2px(Context context, float dipValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dipValue * scale + 0.5f);
}
/**
* 将px值转换为sp值,保证文字大小不变
*
* @param pxValue
* (DisplayMetrics类中属性scaledDensity)
* @return
*/
public static int px2sp(Context context, float pxValue) {
final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (pxValue / fontScale + 0.5f);
}
/**
* 将sp值转换为px值,保证文字大小不变
*
* @param spValue
* (DisplayMetrics类中属性scaledDensity)
* @return
*/
public static int sp2px(Context context, float spValue) {
final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (spValue * fontScale + 0.5f);
}
/**
* 获取屏幕中控件顶部位置的高度--即控件顶部的Y点
*
* @return
*/
public static int getScreenViewTopHeight(View view) {
return view.getTop();
}
/**
* 获取屏幕中控件底部位置的高度--即控件底部的Y点
*
* @return
*/
public static int getScreenViewBottomHeight(View view) {
return view.getBottom();
}
/**
* 获取屏幕中控件左侧的位置--即控件左侧的X点
*
* @return
*/
public static int getScreenViewLeftHeight(View view) {
return view.getLeft();
}
/**
* 获取屏幕中控件右侧的位置--即控件右侧的X点
*
* @return
*/
public static int getScreenViewRightHeight(View view) {
return view.getRight();
}
网络相关
/**
* 判断网络是否可用的方法
* @param context
* @return
*/
public static boolean isOpenNetwork(Context context) {
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
return false;
}
/**
* 判断WIFI网络是否可用的方法
* @param context
* @return
*/
public static boolean isOpenWifi(Context context) {
ConnectivityManager connManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
return mWifi.isConnected();
}
控件相关
/**
* 设置控件Text
* @param v
* @param text
*/
public static void setText(View v, String text) {
if(v==null)
return ;
if (TextUtils.isEmpty(text))
text = "";
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;
}
}
/**
* 获取控件上的Text
* @param v
* @return
*/
public static String getText(View v) {
if(null==v) return "";
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 "";
}
/**
* 下划线
* @param v
*/
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;
}
}
/**
* 删除线
* @param v
*/
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;
}
}
Edittext保留小数位
/**
* @param editText
* @param retain 需要保留的小数位
*/
public static void setInputDecimals(EditText editText, final int retain) {
editText.addTextChangedListener(new TextWatcher() {
public String a = "";
public boolean isCopy = false;
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (1 == count) {
if (0 == start) {
a = s.toString();
if (".".equals(a)) {
s = "0.";
a = "0";
isCopy = true;
editText.setText(s);
editText.setSelection(s.length());
}
} else if (1 == start) {
char c = s.charAt(1);
if ("0".equals(a) && '.' != (c)) {
s = a + "." + c;
isCopy = true;
editText.setText(s);
editText.setSelection(s.length());
}
}
if (s.toString().contains(".")) {
if (s.length() - s.toString().indexOf(".") > retain + 1) {
s = s.toString().subSequence(0, s.toString().indexOf(".") + retain + 1);
isCopy = true;
editText.setText(s);
editText.setSelection(s.length());
}
}
} else if (count > 1) {
if (isCopy) {
isCopy = false;
} else {
editText.setText("");
}
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
显示输入字数
/**
*
* @param edt
* @param inputCount
* @param maxNum
*/
public static void inputCountFilter(EditText edt,TextView inputCount,int maxNum) {
if (null == inputCount||edt==null)
return;
inputCount.setText(MessageFormat.format("{0}/{1}",0,maxNum));
edt.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxNum)});
edt.addTextChangedListener(new TextWatcher() {
private CharSequence wordNum;//记录输入的字数
private int selectionStart;
private int selectionEnd;
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
wordNum = s;//实时记录输入的字数
}
@Override
public void afterTextChanged(Editable s) {
//TextView显示剩余字数
inputCount.setText(MessageFormat.format("{0}/{1}",s.length(),maxNum));
selectionStart = edt.getSelectionStart();
selectionEnd = edt.getSelectionEnd();
if (wordNum.length() > maxNum) {
//删除多余输入的字(不会显示出来)
s.delete(selectionStart - 1, selectionEnd);
int tempSelection = selectionEnd;
edt.setText(s);
edt.setSelection(tempSelection);//设置光标在最后
}
}
});
}
TextView 不同大小字体
/**
* 设置一个textview显示大小不同的字体
*
* @param textView
* @param text
* @param textSize dip
* @param start
* @param end
*/
public static void setAbsoluteSizeSpan(TextView textView, String text, int textSize, int start, int end) {
Spannable sp = new SpannableString(text);
sp.setSpan(new AbsoluteSizeSpan(textSize, true), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setText(sp);
}
Textview .fromHtml() 过时处理
@SuppressWarnings("deprecation")
public static CharSequence fromHtml(String content) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
return Html.fromHtml(content,Html.FROM_HTML_MODE_LEGACY);
} else {
return Html.fromHtml(content);
}
}
Edittext 字母输入小写显示大写转换
注:仅显示,调用getText()获取的还是输入时的状态
/**
* 小写转大写的方法
*
* @return
*/
public static ReplacementTransformationMethod UpperCaseTransform() {
return new ReplacementTransformationMethod() {
@Override
protected char[] getOriginal() {
char[] aa = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
return aa;
}
@Override
protected char[] getReplacement() {
char[] cc = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
return cc;
}
};
}
跳转抖音个人详情或视频详情
1、user id获取>进入个人界面点击分享,复制分享短链(如:https://v.douyin.com/J1q7Nfd/),将短链放到浏览器中访问得到长链(如:https://www.iesdouyin.com/share/user/37379769094355?...),其中“37379769094355”就是id
2、detail id获取>点击分享,复制分享短链(如:https://v.douyin.com/J1q7Nxsd/),将短链放到浏览器中访问得到长链(如:https://www.iesdouyin.com/share/video/683024568564478735/?...),其中“683024568564478735”就是id
public static void openDouyin(Context mContext,boolean isUser,String id) {
String user = MessageFormat.format("snssdk1128://user/profile/{0}",id);
String detail = MessageFormat.format("snssdk1128://aweme/detail/{0}",id);
try {
Uri uri = Uri.parse(isUser?user:detail);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
mContext.startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}