长度换算器

package com.atomic.moretool;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.text.method.DigitsKeyListener;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

public class LengthConversion extends AppCompatActivity{
    private final int user_input_delay=1500;//用户输入延迟
    private long last_text_eidt=0;
    private double value;
    private final Handler handler=new Handler();
    private Runnable runnable;
    private EditText meditText;
    private List<EditText> etl;
    private final double[] doubles=new double[]{1000,10000,100000,1000000,1000000000,2,300,3000,30000
            ,300000,3000000,0.54,546.81,0.62,4.97,1093.61,3280.84,39370.08};
    @SuppressLint("DefaultLocale")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_length_conversion);
        runnable=new Runnable() {
            @SuppressLint("DefaultLocale")
            @Override
            public void run() {
                if (System.currentTimeMillis()>(last_text_eidt+user_input_delay-500)){
                    if (value!=0){
                        etl.get(0).setText(String.format("%.4f",value));
                        for (int z=0;z<doubles.length;z++){
                            etl.get(z+1).setText(String.format("%.4f",value*doubles[z]));
                        }
                    }else{
                        ToastUtils.showShortToast(getBaseContext(),"先输入其中一个");
                    }
                    handler.removeCallbacks(this);
                }
            }
        };
        init();
    }

    private void init() {
        etl = new ArrayList<>();
        for (int i=0;i<19;i++){
            int id=getResources().getIdentifier("l"+i,"id",getPackageName());
            EditText editText = findViewById(id);
            editText.setSelectAllOnFocus(true);
            editText.setSingleLine(true);
            editText.setTextSize(12);
            String digits="0123456789.";
            editText.setKeyListener(DigitsKeyListener.getInstance(digits));
            etl.add(editText);
            int i0 = i;
            editText.setOnFocusChangeListener((view, b) -> {
                if (b){
                    meditText=(EditText) view;//判读是哪一个eidtText
                    meditText.setTag("t"+ i0);
                    meditText.setFilters(new InputFilter[]{filter});
                    meditText.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence charSequence, int i12, int i1, int i2) {

                        }

                        @Override
                        public void onTextChanged(CharSequence charSequence, int i12, int i1, int i2) {
                            value=getValue(meditText);
                            handler.removeCallbacks(runnable);//确保只有一个runnable运行
                        }
                        @Override
                        public void afterTextChanged(Editable editable) {
                            if (String.valueOf(value).length()>0){
                                last_text_eidt=System.currentTimeMillis();
                                handler.postDelayed(runnable,user_input_delay);
                            }
                        }
                    });
                }
            });
        }
    }
    private double getValue(EditText meditText){
        String x=meditText.getText().toString().trim();
        double value=0;
        if (!x.equals("")){
            double d=Double.parseDouble(x);//得到edittext中的值
            if (meditText.getTag().equals("t0")){
                value=d;
            }else if (meditText.getTag().equals("t1")){
                value=d/doubles[0];
            }else if (meditText.getTag().equals("t2")){
                value=d/doubles[1];
            }else if (meditText.getTag().equals("t3")){
                value=d/doubles[2];
            }else if (meditText.getTag().equals("t4")){
                value=d/doubles[3];
            }else if (meditText.getTag().equals("t5")){
                value=d/doubles[4];
            }else if (meditText.getTag().equals("t6")){
                value=d/doubles[5];
            }else if (meditText.getTag().equals("t7")){
                value=d/doubles[6];
            }else if (meditText.getTag().equals("t8")){
                value=d/doubles[7];
            }else if (meditText.getTag().equals("t9")){
                value=d/doubles[8];
            }else if (meditText.getTag().equals("t10")){
                value=d/doubles[9];
            }else if (meditText.getTag().equals("t11")){
                value=d/doubles[10];
            }else if (meditText.getTag().equals("t12")){
                value=d/doubles[11];
            }else if (meditText.getTag().equals("t13")){
                value=d/doubles[12];
            }else if (meditText.getTag().equals("t14")){
                value=d/doubles[13];
            }else if (meditText.getTag().equals("t15")){
                value=d/doubles[14];
            }else if (meditText.getTag().equals("t16")){
                value=d/doubles[15];
            }else if (meditText.getTag().equals("t17")){
                value=d/doubles[16];
            }else if (meditText.getTag().equals("t18")){
                value=d/doubles[17];
            }
        }
        return value;
    }
    private final InputFilter filter= (source, start, end, dest, dstart, dend) -> {
        if (dest.length()==0 && source.equals(".")){
            return "0.";
        }
        return null;
    };
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_margin="15sp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <TextView
                    android:layout_weight="1"
                    android:text="公里(km)"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    tools:ignore="SmallSp" />
                <TextView
                    android:layout_weight="1"
                    android:text="米(m)"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    tools:ignore="SmallSp" />
                <TextView
                    android:layout_weight="1"
                    android:text="分米(dm)"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    tools:ignore="SmallSp" />
                <TextView
                    android:layout_weight="1"
                    android:text="厘米(cm)"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
            </LinearLayout>
            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <EditText
                    android:id="@+id/l0"
                    android:layout_weight="1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
                <EditText
                    android:id="@+id/l1"
                    android:layout_weight="1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
                <EditText
                    android:id="@+id/l2"
                    android:layout_weight="1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
                <EditText
                    android:id="@+id/l3"
                    android:layout_weight="1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <TextView
                    android:layout_weight="1"
                    android:text="毫米(mm)"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <TextView
                    android:layout_weight="1"
                    android:text="微米(um)"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <TextView
                    android:layout_weight="1"
                    android:text=""
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <TextView
                    android:layout_weight="1"
                    android:text=""
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
            </LinearLayout>
            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <EditText
                    android:id="@+id/l4"
                    android:layout_weight="1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
                <EditText
                    android:id="@+id/l5"
                    android:layout_weight="1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
                <EditText
                    android:id="@+id/l6"
                    android:layout_weight="1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
                <EditText
                    android:id="@+id/l7"
                    android:layout_weight="1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <TextView
                    android:layout_weight="1"
                    android:text=""
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <TextView
                    android:layout_weight="1"
                    android:text=""
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <TextView
                    android:layout_weight="1"
                    android:text=""
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <TextView
                    android:layout_weight="1"
                    android:text=""
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
            </LinearLayout>
            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <EditText
                    android:id="@+id/l8"
                    android:layout_weight="1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
                <EditText
                    android:id="@+id/l9"
                    android:layout_weight="1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
                <EditText
                    android:id="@+id/l10"
                    android:layout_weight="1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
                <EditText
                    android:id="@+id/l11"
                    android:layout_weight="1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <TextView
                    android:layout_weight="1"
                    android:text="海里(nmi)"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <TextView
                    android:layout_weight="1"
                    android:text="英寻"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <TextView
                    android:layout_weight="1"
                    android:text="英里(mi)"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <TextView
                    android:layout_weight="1"
                    android:text="弗隆(fur)"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
            </LinearLayout>
            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <EditText
                    android:id="@+id/l12"
                    android:layout_weight="1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
                <EditText
                    android:id="@+id/l13"
                    android:layout_weight="1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
                <EditText
                    android:id="@+id/l14"
                    android:layout_weight="1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
                <EditText
                    android:id="@+id/l15"
                    android:layout_weight="1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <TextView
                    android:layout_weight="1"
                    android:text="码(yd)"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <TextView
                    android:layout_weight="1"
                    android:text="英尺(ft)"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <TextView
                    android:layout_weight="1"
                    android:text="英寸(in)"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>

            </LinearLayout>
            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <EditText
                    android:id="@+id/l16"
                    android:layout_weight="1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
                <EditText
                    android:id="@+id/l17"
                    android:layout_weight="1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
                <EditText
                    android:id="@+id/l18"
                    android:layout_weight="1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
            </LinearLayout>
        </LinearLayout>
    </ScrollView>
</LinearLayout>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Atomic_space

你的鼓励就是我创作最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值