android中如何获取两个EditText的同时值?
我想将 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
}
});
日志猫:
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
I want to set the Edittext as like, whatever i write and enter in to the EditText, it should be written in to the TextView at the same Moment.
I think it can be possible by adding any istener.
I am not able to implement it. So Please anyone help me in this . ?
thanks.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在您的 EditText 上使用 addTextChangedListener 方法,并使您的类实现或定义一个实现 TextWatcher 类的内部类:
更多 :: Android:如何获取 EditText 更改?
Use the addTextChangedListener method on your EditText and make your class implement or define an inner class implementing the TextWatcher class:
For more :: Android: How can I get EditText change?
你必须实现 android.text.method.Keylistener
您可以在其中获得各种回调方法。根据您的需要选择一种方法。
享受..
You have to implement android.text.method.Keylistener
where you get varios callback methods. Choose a method as per your needs.
Enjoy..