亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在多個 EditText 上添加值并在每個 textchange 上獲取總計

在多個 EditText 上添加值并在每個 textchange 上獲取總計

呼喚遠方 2022-05-21 19:49:34
EditText如何在不使用Button但僅使用 TextWatcher 的onTextChanged()方法的情況下對我的倍數進行總和。假設這是我的布局:編輯文本1 = 5編輯文本2 = 5編輯文本3 = 5編輯文本4 = 5總計 = 20等等并得到它的總數?當' 的值發生變化時,總數應該是變化的。EditText我已閱讀此答案。但我不能在我的程序上很好地執行它。這是我的布局
查看完整描述

2 回答

?
森林海

TA貢獻2011條經驗 獲得超2個贊

你需要使用一個TextWatcher()


而不是使用 5TextWatcher()你可以只使用一個來管理它TextWatcher()


試試這個


如果您的所有 4 個四個都不為空,則使用以下答案,edittext那么它將計算值的editext總和


public class MainActivity extends AppCompatActivity {



    EditText edtOne, edtTwo, edtThree, edtFour;

    TextView tvResult;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        edtOne = findViewById(R.id.edtOne);

        edtTwo = findViewById(R.id.edtTwo);

        edtThree = findViewById(R.id.edtThree);

        edtFour = findViewById(R.id.edtFour);


        tvResult = findViewById(R.id.tvResult);


        edtOne.addTextChangedListener(textWatcher);

        edtTwo.addTextChangedListener(textWatcher);

        edtThree.addTextChangedListener(textWatcher);

        edtFour.addTextChangedListener(textWatcher);


    }


    TextWatcher textWatcher = new TextWatcher() {

        @Override

        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {


        }


        @Override

        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {


            if (!TextUtils.isEmpty(edtOne.getText().toString().trim())

                    || !TextUtils.isEmpty(edtTwo.getText().toString().trim())

                    || !TextUtils.isEmpty(edtThree.getText().toString().trim())

                    || !TextUtils.isEmpty(edtFour.getText().toString().trim())

                    ) {



                int answer = Integer.parseInt(edtOne.getText().toString().trim()) +

                        Integer.parseInt(edtTwo.getText().toString().trim()) +

                        Integer.parseInt(edtThree.getText().toString().trim()) +

                        Integer.parseInt(edtFour.getText().toString().trim());


                Log.e("RESULT", String.valueOf(answer));

                tvResult.setText(String.valueOf(answer));

            }else {

            tvResult.setText("");

        }

        }


        @Override

        public void afterTextChanged(Editable editable) {


        }

    };


}

更新

如果您想計算所有的值editext即使您的編輯文本為空,也請嘗試以下操作TextWatcher()


TextWatcher textWatcher = new TextWatcher() {

    @Override

    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {


    }


    @Override

    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {


        if (!TextUtils.isEmpty(edtOne.getText().toString().trim())

                || !TextUtils.isEmpty(edtTwo.getText().toString().trim())

                || !TextUtils.isEmpty(edtThree.getText().toString().trim())

                || !TextUtils.isEmpty(edtFour.getText().toString().trim())

                ) {



            int firtValue = TextUtils.isEmpty(edtOne.getText().toString().trim()) ? 0 : Integer.parseInt(edtOne.getText().toString().trim());

            int secondValue = TextUtils.isEmpty(edtTwo.getText().toString().trim()) ? 0 : Integer.parseInt(edtTwo.getText().toString().trim());

            int thirdValue = TextUtils.isEmpty(edtThree.getText().toString().trim()) ? 0 : Integer.parseInt(edtThree.getText().toString().trim());

            int forthValue = TextUtils.isEmpty(edtFour.getText().toString().trim()) ? 0 : Integer.parseInt(edtFour.getText().toString().trim());


            int answer = firtValue + secondValue + thirdValue + forthValue;


            Log.e("RESULT", String.valueOf(answer));

            tvResult.setText(String.valueOf(answer));

        }else {

            tvResult.setText("");

        }

    }


    @Override

    public void afterTextChanged(Editable editable) {


    }

};


查看完整回答
反對 回復 2022-05-21
?
www說

TA貢獻1775條經驗 獲得超8個贊

我將告訴你如何去做,但我不會將解決方案作為總代碼發布。


首先,定義整數,即編輯文本計數的數量。如果有 4 個編輯文本,用 like 定義 4 個整數int first = 0, second = 0, third = 0, fourth = 0;


然后,將單獨的文本觀察器添加到您的編輯文本中,并在您的afterTextChanged方法上,通過使用從字符串中獲取整數值,Integer.parseInt(Editable s.getText.toString())并確保輸入是可轉換的,否則您將獲得一個NumberFormatException. 然后,將此值分配給相關的編輯文本,例如在您第一次使用編輯文本時first = Integer.parse(s.getText().toString())。


創建一個使用這些變量在文本視圖上顯示的函數。例如:


private void showResult() {

    textView.setText(String.valueOf(first + second + third + fourth));

}

然后,在每個 afterTextChanged 方法調用此函數以顯示總金額。


查看完整回答
反對 回復 2022-05-21
  • 2 回答
  • 0 關注
  • 145 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號