-1

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

提到了这个,但是在循环之后我没有得到我全局声明变量的值,但也没有得到文本框的值。

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());
4

2 回答 2

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并计算您想要的内容。

于 2012-06-06T08:33:26.563 回答
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){}
        }

    }
于 2012-06-06T08:46:10.247 回答