找不到符号addTextChangedListener

时间:2013-08-30 02:38:19

标签: android android-edittext

我遇到addTextChangedListener问题。我有2个edittext(edittext1和edittext2),当添加或更改文本(在本例中为数字)时,我需要它来运行计算并将其输出到名为resultstext的textview。

我找到了许多示例,并修改了它们以执行我需要它们执行的操作,但是当我调用editText1.addTextChangedListener.inputTextwatcher;时,它告诉我它无法找到符号addTextChangedListener。下面是代码,如何理顺这个?

package com.example.MyProject;

import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.view.FocusFinder;
import android.view.Menu;
import android.widget.EditText;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

private android.widget.EditText editText1;
private android.widget.EditText editText2;
private android.widget.TextView resultsText;


private void onItemSelected() {
editText1 = (EditText)findViewById(R.id.textView13);
editText2 = (EditText)findViewById(R.id.textView15);
resultsText = (android.widget.TextView)findViewById(R.id.textView16);
}


  android.text.TextWatcher inputTextWatcher = new android.text.TextWatcher()

{
    public void afterTextChanged(android.text.Editable s) {
        calculateResult();
    }
    public void beforeTextChanged(CharSequence s, int start, int count, int after){}
    public void onTextChanged(CharSequence s, int start, int before, int count){}
};

editText1.addTextChangedListener.inputTextWatcher;
editText2.addTextChangedListener.inputTextWatcher;

private void calculateResult() throws NumberFormatException {

    Editable editableValue1 = editText1.getText(),
            editableValue2 = editText2.getText();

    double value1 = 0.0,
            value2 = 0.0,
            result;

    if (editableValue1 != null)
        value1 = Double.parseDouble(editableValue1.toString());

    if (editableValue2 != null)
        value2 = Double.parseDouble(editableValue2.toString());

    result = ((0.5 * value1) / 6.1) * value2;


    resultsText.setText(String.valueOf(result));
}
}

2 个答案:

答案 0 :(得分:1)

放置一些代码是错误的。我试图解决它。看看以下内容是否适合您:

package com.example.MyProject;

import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.view.FocusFinder;
import android.view.Menu;
import android.widget.EditText;

public class MainActivity extends Activity {

private android.widget.EditText editText1;
private android.widget.EditText editText2;
private android.widget.TextView resultsText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    editText1 = (EditText)findViewById(R.id.textView13);
    editText2 = (EditText)findViewById(R.id.textView15);
    resultsText = (android.widget.TextView)findViewById(R.id.textView16);

    android.text.TextWatcher inputTextWatcher = new android.text.TextWatcher() {
        public void afterTextChanged(android.text.Editable s) {
            calculateResult();
        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after){}
        public void onTextChanged(CharSequence s, int start, int before, int count){}
    };

    editText1.addTextChangedListener(inputTextWatcher);
    editText2.addTextChangedListener(inputTextWatcher);        
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}   

private void calculateResult() throws NumberFormatException {

    Editable editableValue1 = editText1.getText();
    Editable editableValue2 = editText2.getText();

    double value1 = 0.0,
            value2 = 0.0,
            result;

    if (editableValue1 != null)
        value1 = Double.parseDouble(editableValue1.toString());

    if (editableValue2 != null)
        value2 = Double.parseDouble(editableValue2.toString());

    result = ((0.5 * value1) / 6.1) * value2;


    resultsText.setText(String.valueOf(result));
}
}

答案 1 :(得分:0)

尝试使用

           private TextWatcher watcher = new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        //your code here
    }

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

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

然后将观察者添加到您的edittext

       youredittext.addTextChangedListener(watcher);