0

I am having issues with addTextChangedListener. I have 2 edittext (edittext1 and edittext2) that when the text (in this case numbers) are added or changed I need it to run a calculation and output it to a textview called resultstext.

I have found many examples and have modified them to do what I need them to do, but when I call the editText1.addTextChangedListener.inputTextwatcher; it tells me it cant find the symbol addTextChangedListener. Below is the code, how to straighten this out?

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

2 回答 2

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));
}
}
于 2013-08-30T03:16:55.207 回答
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);
于 2013-08-30T02:53:33.193 回答