先写个Layout:
记得,添加不同国家的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;
}
}


