Android 问题整理 六

// 控值小数点后两位小数,金额输入限制

EditText.addTextChangedListener(new MoneyTextWatcher(EditText));
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

/**
 * 控值小数点后两位小数
 */
public class MoneyTextWatcher implements TextWatcher {
    private EditText editText;
    private int digits = 2;

    public MoneyTextWatcher(EditText et) {
        editText = et;
    }
    public MoneyTextWatcher setDigits(int d) {
        digits = d;
        return this;
    }


    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        //删除“.”后面超过2位后的数据
        if (s.toString().contains(".")) {
            if (s.length() - 1 - s.toString().indexOf(".") > digits) {
                s = s.toString().subSequence(0, s.toString().indexOf(".") + digits+1);
                editText.setText(s);
                editText.setSelection(s.length()); //光标移到最后
            }
        }
        //如果"."在起始位置,则起始位置自动补0
        if (s.toString().trim().substring(0).equals(".")) {
            s = "0" + s;
            editText.setText(s);
            editText.setSelection(2);
        }

        //如果起始位置为0,且第二位跟的不是".",则无法后续输入
        if (s.toString().startsWith("0")
                && s.toString().trim().length() > 1) {
            if (!s.toString().substring(1, 2).equals(".")) {
                editText.setText(s.subSequence(0, 1));
                editText.setSelection(1);
                return;
            }
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
}

// android 录制mp3

参考:Android 录制mp3使用mp3lame 库_chezi008-CSDN博客

github:https://github.com/chezi008/AndroidMp3Recorder

项目根:gradle
jcenter()
maven { url 'https://jitpack.io' }

app module

implementation 'com.github.chezi008:AndroidMp3Recorder:v1.0.4'
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:gravity="center"
    android:layout_height="match_parent" >


    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="开始"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button_stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="停止"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</LinearLayout>
package com.zowneo.myapplication4;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.ibbhub.mp3recorderlib.IAudioRecorder;
import com.ibbhub.mp3recorderlib.Mp3Recorder;
import com.ibbhub.mp3recorderlib.RecorderView;
import com.ibbhub.mp3recorderlib.SpectrumView;

import java.io.File;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {
    Button button;
    Button button_stop;
    private IAudioRecorder mRecorder;
    private String filePath= Environment.getExternalStorageDirectory().getPath() + "/test.mp3";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        createFile();
        mRecorder = new Mp3Recorder();
        button = findViewById(R.id.button);
        button_stop = findViewById(R.id.button_stop);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mRecorder.start(filePath);
            }
        });

        button_stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mRecorder.stop();
                String strFinish = String.format("录制完成,保存在:%s",filePath);
                Log.e("==================", strFinish);
            }
        });
    }


    private void createFile() {
        File file = new File(filePath);
        if (!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

 

// 上传图片抓包无效,OnError 报错

com.lzy.okgo.exception.HttpException: network error! http response code is 404 or 5xx!

有可能是图片格式的问题。

参考:Android平台png转jpg的实现

附:Android 图片选择器PictureSelector 参数属性

附:【Android】简单实现拷贝文件和文件夹到另一个目录下


    /**
     * 转换图片格式为jpeg
     * @param pngFilePath
     * @param jpgFilePath
     */
    public static void convertToJpg(String pngFilePath, String jpgFilePath) {
        Bitmap bitmap = BitmapFactory.decodeFile(pngFilePath);
        try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(jpgFilePath))) {
            if (bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos)) {
                bos.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取转换图片格式为jpeg后的路径
     * @param path
     * @return
     */
    public static String getConvertToJpg(String path) {
        File file = new File(path);
        File parentFile = file.getParentFile();
        String name = file.getName();
        name = name.replace(".png", ".jpeg");
        convertToJpg(path, parentFile+name);
        return parentFile+name;
    }
范例:
String path = listPicBanner.get(i).getCompressPath();
filesBanner.add(new File(getConvertToJpg(path)));

// 金额计算 BigDecimal加减乘除,以及保留精度

参考:BigDecimal加减乘除_csdn565973850的博客-CSDN博客

参考:java.math.BigDecimal保留两位小数_p393975269的博客-CSDN博客

BigDecimal bignum1 = new BigDecimal("10");  
BigDecimal bignum2 = new BigDecimal("5");  
BigDecimal bignum3 = null;  
  
//加法  
bignum3 =  bignum1.add(bignum2);       
System.out.println("和 是:" + bignum3);  
  
//减法  
bignum3 = bignum1.subtract(bignum2);  
System.out.println("差  是:" + bignum3);  
  
//乘法  
bignum3 = bignum1.multiply(bignum2);  
System.out.println("积  是:" + bignum3);  
  
//除法  
bignum3 = bignum1.divide(bignum2);  
System.out.println("商  是:" + bignum3); 

 

发表评论

zh_CNChinese