在没有按下android中的任何键的情况下编辑文本值?

时间:2012-06-06 08:26:33

标签: android android-edittext

我的要求如下:我需要从多个文本中获取数据 框(EditText),我需要计算并在另一个中显示它 EditText。怎么可能?正如我所提到的,没有按钮 自动按下它需要出现在总框中。

referred this,但在循环之后,我没有得到值I 在全局声明变量但也没有得到变量的值 文本框。

EditText et1, et2, etTot;
int x, y, tot;
et1 = (EditText) findViewbyId(R.id.edittext1);
et2 = (EditText) findViewbyId(R.id.edittext2);
etTot = (EditText) findViewbyId(R.id.edittextTot);

et1.addTextChangedListener(new TextWatcher() {

    public void onTextChanged(CharSequence s, int start, int before,
                              int count) {
        x = Integer.parseInt(et1.getText().toString().trim());
        System.out
                .println("AMOUNT IS " + et1.getText().toString());

    }

    public void beforeTextChanged(CharSequence s, int start, int count,
                                  int after) {

    }

    public void afterTextChanged(Editable s) {

    }
});

//same as for et2......

tot = x + y;

etTot.setText(tot.toString());

2 个答案:

答案 0 :(得分:1)

您可以使用实现TextWatcher的类并覆盖以下方法:

    @Override
        public void afterTextChanged(final Editable editable) {
            // check if user has filled some value in all editTexts
        }


        @Override
        public void beforeTextChanged(final CharSequence string,
                final int start, final int count, final int after) {
        }


        @Override
        public void onTextChanged(final CharSequence string, final int start,
                final int before, final int count) {
        }

将上述代码用于上一个editText,并检查所有editTexts是否包含某些值,即它们的值不是“”或null。如果是,则从这些editTexts中检索所有文本并计算您想要的内容。

答案 1 :(得分:1)

//global variable
EditText et1,et2,etTot;
int x,y,tot;

// in 0nCreat()
et1 = (EditText)findViewbyId(R.id.edittext1);
et2 = (EditText)findViewbyId(R.id.edittext2);
etTot = (EditText)findViewbyId(R.id.edittextTot);


et1.addTextChangedListener(new TextWatcher() {

    public void onTextChanged(CharSequence s, int start, int before,
    int count) {
        <Activity_NAME>.this.updateValue();
    }

    public void beforeTextChanged(CharSequence s, int start, int count,
    int after) {
    }

    public void afterTextChanged(Editable s) {
    }
    });

et2.addTextChangedListener(new TextWatcher() {

    public void onTextChanged(CharSequence s, int start, int before,
    int count) {
        <Activity_NAME>.this.updateValue();

    }

    public void beforeTextChanged(CharSequence s, int start, int count,
    int after) {

    }

    public void afterTextChanged(Editable s) {

    }
    });


//in class       
private void updateValue(){
    String str1 = et1.getText().toString();
    String str2 = et2.getText().toString();

    if(null!=str1 && str1.lenght()>0 && null!=str2 && str2.lenght()>0)                  
    {
        try{
            x= Integer.parseInt(str1.trim());
            y= Integer.parseInt(str1.trim());

            tot = x + y; 
            if(null!=etTot)
                etTot.setText(tot+"");
            else 
                Toast.makeText(this,"etTot is null ",Toast.LENGHT_SHORT).show();
            }catch (NumberFormatExcption nfe){}
        }

    }