我想将Edittext设置为,无论我写什么并输入EditText,它都应该在同一时刻写入TextView。
我认为可以通过添加任何istener来实现。 我无法实现它。所以请任何人帮助我。 ? 感谢。
includedText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(!(includedText.getText().toString().equals("")))
{
double included = Double.parseDouble(includedText.getText().toString());
included = roundTwoDecimals(included);
// double amountValue = (excluded*15/100);
String amt = String.valueOf(roundTwoDecimals(included-(included/1.15)));
String excluded = String.valueOf(included/1.15);
System.out.println("The Amount is: "+amt);
amountText.setText(amt);
try{
excludedText.setText(excluded); //////// Error
}
catch(Exception e)
{
e.printStackTrace();
}
}
else
{
amountText.setText("");
//excludedText.setText("");
}
}
@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
}
});
excludedText.addTextChangedListener(new TextWatcher()
{
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(!(excludedText.getText().toString().equals("")))
{
double excluded = Double.parseDouble(excludedText.getText().toString());
excluded = roundTwoDecimals(excluded);
// double amountValue = (excluded*15/100);
String amt = String.valueOf(roundTwoDecimals(excluded*0.15));
String included = String.valueOf(roundTwoDecimals(excluded+(excluded*0.15)));
System.out.println("The Amount is: "+amt);
amountText.setText(amt);
includedText.setText(included);
}
else
{
amountText.setText("");
includedText.setText("");
}
}
@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
}
});
logcat的:
10-08 10:34:27.239: ERROR/AndroidRuntime(662): FATAL EXCEPTION: main
10-08 10:34:27.239: ERROR/AndroidRuntime(662): java.lang.StackOverflowError
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.text.DynamicLayout.reflow(DynamicLayout.java:261)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.text.DynamicLayout.<init>(DynamicLayout.java:150)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.TextView.makeNewLayout(TextView.java:4851)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.TextView.checkForRelayout(TextView.java:5348)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.TextView.setText(TextView.java:2688)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.TextView.setText(TextView.java:2556)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.EditText.setText(EditText.java:75)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.TextView.setText(TextView.java:2531)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at com.project.TaxCalculator.GSTActivity$1.onTextChanged(GSTActivity.java:66)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.TextView.setText(TextView.java:2691)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.TextView.setText(TextView.java:2556)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.EditText.setText(EditText.java:75)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.TextView.setText(TextView.java:2531)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at com.project.TaxCalculator.GSTActivity$2.onTextChanged(GSTActivity.java:110)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.TextView.setText(TextView.java:2691)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.TextView.setText(TextView.java:2556)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.EditText.setText(EditText.java:75)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.TextView.setText(TextView.java:2531)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at com.project.TaxCalculator.GSTActivity$1.onTextChanged(GSTActivity.java:66)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.TextView.setText(TextView.java:2691)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.TextView.setText(TextView.java:2556)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.EditText.setText(EditText.java:75)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.TextView.setText(TextView.java:2531)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at com.project.TaxCalculator.GSTActivity$2.onTextChanged(GSTActivity.java:110)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.TextView.setText(TextView.java:2691)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.TextView.setText(TextView.java:2556)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.EditText.setText(EditText.java:75)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.TextView.setText(TextView.java:2531)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at com.project.TaxCalculator.GSTActivity$1.onTextChanged(GSTActivity.java:66)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.TextView.setText(TextView.java:2691)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.TextView.setText(TextView.java:2556)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.EditText.setText(EditText.java:75)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.TextView.setText(TextView.java:2531)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at com.project.TaxCalculator.GSTActivity$2.onTextChanged(GSTActivity.java:110)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.TextView.setText(TextView.java:2691)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.TextView.setText(TextView.java:2556)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.EditText.setText(EditText.java:75)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.TextView.setText(TextView.java:2531)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at com.project.TaxCalculator.GSTActivity$1.onTextChanged(GSTActivity.java:66)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.TextView.setText(TextView.java:2691)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.TextView.setText(TextView.java:2556)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.EditText.setText(EditText.java:75)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.TextView.setText(TextView.java:2531)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at com.project.TaxCalculator.GSTActivity$2.onTextChanged(GSTActivity.java:110)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.TextView.setText(TextView.java:2691)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.TextView.setText(TextView.java:2556)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.EditText.setText(EditText.java:75)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.TextView.setText(TextView.java:2531)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at com.project.TaxCalculator.GSTActivity$1.onTextChanged(GSTActivity.java:66)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.TextView.setText(TextView.java:2691)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.TextView.setText(TextView.java:2556)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.EditText.setText(EditText.java:75)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.TextView.setText(TextView.java:2531)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at com.project.TaxCalculator.GSTActivity$2.onTextChanged(GSTActivity.java:110)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.TextView.setText(TextView.java:2691)
10-08 10:34:27.239: ERROR/AndroidRuntime(662): at android.widget.TextVie
答案 0 :(得分:5)
在EditText上使用addTextChangedListener方法,让你的类实现或定义一个实现TextWatcher类的内部类:
textMessage = (EditText)findViewById(R.id.textMessage);
textMessage.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){}
});
答案 1 :(得分:2)
TextView mTextView= (TextView )findViewById(R.id.textview);
EditText mEditText = (EditText)findViewById(R.id.edittext);
mEditText .addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){
mTextView.setText(s.toString);
}
});
答案 2 :(得分:1)
你必须实现android.text.method.Keylistener 你得到varios回调方法的地方。根据您的需要选择一种方法。
享受..