编辑文本值更改

时间:2015-11-17 04:49:02

标签: android

我有3个EditText字段并提交按钮。任何EditText中的值的更改将修改其他两个EditText.Have应用的setOnEditorActionListener和setOnFocusChangeListener的值但是如果用户更改了一个editText并单击了提交按钮,则不会更改其他editText。哪个听众使用。请帮助。我可以在所有三个EditText中使用addTextChangedListener。

remainingWorkEdit.setOnFocusChangeListener(new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            // TODO Auto-generated method stub
            String remainingWorkInput = remainingWorkEdit.getText()
                    .toString().trim();

            Float actualWorkCalculated = Float.parseFloat(actualWorkEdit
                    .getText().toString().trim());

            actualWorkEdit.setText(actualWorkCalculated.toString());
            Float percentageCalculated = actualWorkCalculated
                    / Float.parseFloat(workEdit.getText().toString().trim())
                    * 100;
            percentageEdit.setText(percentageCalculated.toString());
            Float workCalculated = Float.parseFloat(actualWorkEdit
                    .getText().toString().trim())
                    + Float.parseFloat(remainingWorkInput);
            workEdit.setText(workCalculated.toString());

        }
    });

    remainingWorkEdit
            .setOnEditorActionListener(new OnEditorActionListener() {

                @Override
                public boolean onEditorAction(TextView v, int actionId,
                        KeyEvent event) {
                    // TODO Auto-generated method stub
                    String remainingWorkInput = remainingWorkEdit.getText()
                            .toString().trim();
                    if (actionId == EditorInfo.IME_ACTION_NEXT
                            || actionId == EditorInfo.IME_ACTION_DONE) {

                        Float actualWorkCalculated = Float
                                .parseFloat(workEdit.getText().toString()
                                        .trim())
                                - Float.parseFloat(remainingWorkInput);
                        actualWorkEdit.setText(actualWorkCalculated
                                .toString());
                        Float percentageCalculated = actualWorkCalculated
                                / Float.parseFloat(workEdit.getText()
                                        .toString().trim()) * 100;
                        percentageEdit.setText(percentageCalculated
                                .toString());
                        Float workCalculated = Float
                                .parseFloat(actualWorkEdit.getText()
                                        .toString().trim())
                                + Float.parseFloat(remainingWorkInput);
                        workEdit.setText(workCalculated.toString());
                        return true;
                    }
                    return false;
                }
            });

其他两个editText的代码几乎相同。

1 个答案:

答案 0 :(得分:1)

你必须做这样的事情

remainingWorkEdit.addTextChangedListener(new TextWatcher()
{ 
    @Override
    public void onTextChanged(CharSequence s, int start, int before,int count) 
    {
        percentageEdit.setText(percentageCalculated.toString());

        workEdit.setText(workCalculated.toString());
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) 
    {
        // TODO Auto-generated method stub
    }

    @Override
    public void afterTextChanged(Editable s) 
    {
        // TODO Auto-generated method stub

    }
});