Android的一些工具方法

该文章包含一系列Android开发中的实用工具类,如LogUtils用于调试日志,WordUtil获取资源字符串,ToastUtil展示吐司提示,以及StringUtil处理文本的空值和格式化操作。这些工具类在提高代码复用性和简化日常开发中起到重要作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class LogUtils {

private static Boolean isDebug = null;
private static String TAG = Constants.APP_TAG;

public static void e(String s) {
    if (isDebug) {
        e(TAG, s);
    }
}

public static void e(String tag, String s) {
    if (isDebug) {
        Log.e(tag, s);
    }
}

/**
 * Sync lib debug with app's debug value. Should be called in module Application
 *
 * @param context
 */
public static void syncIsDebug(Context context) {
    if (isDebug == null) {
        isDebug = context.getApplicationInfo() != null &&
                (context.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
    }
}

public static boolean isDebug() {
    return isDebug == null ? false : isDebug.booleanValue();
}

}

public class WordUtil {

private static Resources sResources;

static {
    sResources = CommonAppContext.sInstance.getResources();
}

public static String getString(int res) {
    return sResources.getString(res);
}

}

/**

  • Created by cxf on 2017/8/3.
    */

public class ToastUtil {

private static Toast sToast;
private static long sLastTime;
private static String sLastString;

static {
    sToast = makeToast();
}

private static Toast makeToast() {
    Toast toast = new Toast(CommonAppContext.sInstance);
    toast.setDuration(Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.CENTER, 0, 0);
    View view = LayoutInflater.from(CommonAppContext.sInstance).inflate(R.layout.view_toast, null);
    toast.setView(view);
    return toast;
}


public static void show(int res) {
    show(WordUtil.getString(res));
}

public static void show(String s) {
    if (TextUtils.isEmpty(s)) {
        return;
    }
    long curTime = System.currentTimeMillis();
    if (curTime - sLastTime > 2000) {
        sLastTime = curTime;
        sLastString = s;
        sToast.setText(s);
        sToast.show();
    } else {
        if (!s.equals(sLastString)) {
            sLastTime = curTime;
            sLastString = s;
            sToast = makeToast();
            sToast.setText(s);
            sToast.show();
        }
    }

}

}

package com.zliang.chunzhen.utils;

import android.widget.EditText;
import android.widget.TextView;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;

/**

  • Created by cxf on 2018/9/29.
    */

public class StringUtil {

// 判断字符串是否为空
public static boolean isEmpty(String str) {
    if (str == null)
        return true;
    if (str.length() == 0)
        return true;
    if (str.trim().length() == 0)
        return true;
    if (str.equals("null"))
        return true;
    return false;
}


public static String dropNull(String str) {
    if (!isEmpty(str)) {
        return str;
    }
    return "";
}

public static String nullToZero(String str) {
    if (!isEmpty(str)) {
        return str;
    }
    return "0";
}

public static String getTextViewValue(TextView textView) {
    return textView.getText().toString().trim();
}

public static String getEditViewValue(EditText editText) {
    return editText.getText().toString().trim();
}

public static int formatPingJiaScore(String score) {
    return Integer.parseInt(String.valueOf(Math.round(Double.parseDouble(score))));
}

public static String formatTimeDropSecond(String time) {

    if (isEmpty(time)){
        return "";
    }

    if (time.length() >= 3) {
        return time.substring(0, time.length() - 3);
    }
    return "";
}

public static String formatMoney(String money) {

    NumberFormat nf = new DecimalFormat("0.00");
    Double d = Double.parseDouble(nullToZero(money));

    String temp = nf.format(d);
    return "¥"+getPrettyNumber(temp);

// NumberFormat nf = new DecimalFormat(“¥0.00”);
// Double d = Double.parseDouble(nullToZero(money));
// return nf.format(d);
}

public static String format2Point(String money) {
    NumberFormat nf = new DecimalFormat("0.00");
    Double d = Double.parseDouble(nullToZero(money));
    return nf.format(d);
}

public static String getPrettyNumber(String number) {
    return BigDecimal.valueOf(Double.parseDouble(number))
            .stripTrailingZeros().toPlainString();
}

}

<?xml version="1.0" encoding="utf-8"?>

<View
android:layout_width="match_parent"
android:layout_height="@dimen/line_height"
android:background="@color/common_line_color" />

public class Adapter_Bank extends RefreshAdapter {

private View.OnClickListener mOnClickListener;

public Adapter_Bank(Context context) {
    super(context);
    mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Object tag = v.getTag();
            if (tag != null) {
                int position = (int) tag;
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(mList.get(position), position);
                }
            }
        }
    };
}


@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    return new Vh(mInflater.inflate(R.layout.item_bank, parent, false));
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder vh, int position) {
    ((Vh) vh).setData(mList.get(position), position);
}

class Vh extends RecyclerView.ViewHolder {

    ImageView imgHead;
    TextView textName;

    public Vh(View itemView) {
        super(itemView);
        imgHead = (ImageView) itemView.findViewById(R.id.imgHead);
        textName = (TextView) itemView.findViewById(R.id.textName);
        itemView.setOnClickListener(mOnClickListener);
    }

    void setData(CardBean bean, int position) {
        itemView.setTag(position);


        if (StringUtil.isEmpty(bean.getImgs())){
            imgHead.setVisibility(View.GONE);
        }else {
            imgHead.setVisibility(View.VISIBLE);
            GlideApp.with(mContext)
                    .load(bean.getImgs()).into(imgHead);
        }

        textName.setText(bean.getName());

    }
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值