栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

实现一个获得用户手机国家信息并同步EditText和TextView

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

实现一个获得用户手机国家信息并同步EditText和TextView


先写个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;


    }
}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/237781.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号