第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定

實(shí)現(xiàn)一個(gè)獲得用戶手機(jī)國家信息并同步EditText和TextView

標(biāo)簽:
Java Android

https://img1.sycdn.imooc.com//5fa31996000172df05040791.jpg

先写个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,手机才能捕捉得到:

https://img1.sycdn.imooc.com//5fa319e50001f36a03470219.jpg

接着是主代码:

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;


    }
}


點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號

舉報(bào)

0/150
提交
取消