先写个Layout:
<?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:layout_height="match_parent" android:orientation="vertical" android:gravity="center" android:background="#ffffff" tools:context=".MainActivity"> <EditText android:id="@+id/edt" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入" android:inputType="number|phone" android:digits="0123456789" android:textColor="#000000" android:textSize="15dp" android:textColorHint="#707070"/> <TextView android:id="@+id/txt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#000000" android:text="数字" android:textSize="15dp"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="horizontal"> <TextView android:id="@+id/money_symbol" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/euro" android:textSize="15dp" android:textColor="#000000"/> <TextView android:id="@+id/amount" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="00" android:textColor="#000000" android:textSize="15dp"/> </LinearLayout> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="随便"/> </LinearLayout>
记得,添加不同国家的value,手机才能捕捉得到:
接着是主代码:
package com.example.onemoretryapplication; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.text.Editable; import android.text.TextWatcher; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import java.util.Locale; public class MainActivity extends AppCompatActivity { private EditText edt; private TextView txt, money_symbol = null, amount; private Button btn; private float result; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); edt = findViewById(R.id.edt); txt = findViewById(R.id.txt); money_symbol = findViewById(R.id.money_symbol); amount = findViewById(R.id.amount); btn = findViewById(R.id.btn); initEvent(); } private void initEvent() { edt.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { //变化时执行的方法 //TextView接收editText的内容,实现同步显示 int num = 0 ; try{ num = Integer.parseInt(edt.getText().toString()); }catch (NumberFormatException e){ } if(num == 0){ Toast.makeText(MainActivity.this, "请至少输入一位用户", Toast.LENGTH_SHORT).show(); }else{ txt.setText(edt.getText().toString()); amount.setText(String.valueOf(getResult())); } } @Override public void afterTextChanged(Editable s) { } }); } private float getResult() { Locale locale; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) { locale = getResources().getConfiguration().getLocales().get(0); } else { locale = getResources().getConfiguration().locale; } String language = locale.getCountry(); String str = "CN"; if (language.equals(str)) { int txt = Integer.parseInt(edt.getText().toString()); result = (float) (txt * 0.0135*6.9); return result; } return result; } }
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦