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;
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);
}
@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);
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>