Java 根据另一个EditText更改EditText
我是Android开发新手。我有两个EditText,我正在尝试根据ET2改变ET1的值,同时根据ET1改变ET2的值,在键入过程中自动进行 它们的值之和必须为200。所以如果我想在ET1中输入56,当我输入5时,ET2应该显示195。当我添加6时,ET2应该显示144(200-56)。一切都应该在关注ET1的时候发生Java 根据另一个EditText更改EditText,java,android,android-edittext,Java,Android,Android Edittext,我是Android开发新手。我有两个EditText,我正在尝试根据ET2改变ET1的值,同时根据ET1改变ET2的值,在键入过程中自动进行 它们的值之和必须为200。所以如果我想在ET1中输入56,当我输入5时,ET2应该显示195。当我添加6时,ET2应该显示144(200-56)。一切都应该在关注ET1的时候发生 scoreWe.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override
scoreWe.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (v.hasFocus()) {
scoreYou.addTextChangedListener(null);
scoreWe.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
int scoreW;
try {
scoreW = Integer.parseInt(scoreWe.getText().toString());
} catch (NumberFormatException e) {
scoreW = 0;
}
int scoreY = 200 - scoreW;
String scY = Integer.toString(scoreY);
scoreYou.setText(scY);
}
});
}
}
});
-
这是两个OnFocusChangeListeners,在我尝试在任何编辑文本中输入内容后,应用程序立即崩溃。
日志:
09-18 16:50:36.917 17764-17764/com.example.dvido.bela I/Process:发送信号。PID:17764 SIG:9我的最佳猜测是由于文本更改侦听器同时在两个edittext视图上被调用 尝试在ET2的焦点上禁用ET1的
文本更改侦听器
,反之亦然。做这样的事
禁用scoreWe textChanged侦听器
禁用您的文本更改侦听器
当你的主线程/应用程序被阻止时,你的日志会说什么?你所说的被阻止是什么意思?应用程序没有响应?还是崩溃了?它正在工作,但只是第一次。若我将光标移动到ET2,然后再移回ET1,文本将不会更改,因为侦听器已禁用,所以我必须将其重新设置为“开”。@Coelacanth我已添加代码。请检查更新的答案。将其他侦听器设置为null后,必须再次重置侦听器。那就行了。试着让我知道。我已经更改了代码,但仍然没有更改。我把它加在上面了。谢谢你的帮助。
scoreYou.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (v.hasFocus()) {
scoreWe.addTextChangedListener(null);
scoreYou.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
int scoreY;
try {
scoreY = Integer.parseInt(scoreYou.getText().toString());
} catch (NumberFormatException e) {
scoreY = 0;
}
int scoreW = 200 - scoreY;
String scW = Integer.toString(scoreW);
scoreWe.setText(scW);
}
});
}
}
});
09-18 16:50:22.362 17764-17764/com.example.dvido.bela E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.dvido.bela, PID: 17764
java.lang.NullPointerException: Attempt to invoke interface method 'void android.text.TextWatcher.beforeTextChanged(java.lang.CharSequence, int, int, int)' on a null object reference
at android.widget.TextView.sendBeforeTextChanged(TextView.java:7941)
at android.widget.TextView.setText(TextView.java:4242)
at android.widget.TextView.setText(TextView.java:4199)
at android.widget.EditText.setText(EditText.java:84)
at android.widget.TextView.setText(TextView.java:4174)
at com.example.dvido.bela.InputGame$1$1.afterTextChanged(InputGame.java:89)
at android.widget.TextView.sendAfterTextChanged(TextView.java:8007)
at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:10168)
at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:1043)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:560)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:492)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:491)
at android.text.method.NumberKeyListener.onKeyDown(NumberKeyListener.java:121)
at android.widget.TextView.doKeyDown(TextView.java:6098)
at android.widget.TextView.onKeyDown(TextView.java:5911)
at android.view.KeyEvent.dispatch(KeyEvent.java:2640)
at android.view.View.dispatchKeyEvent(View.java:9234)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at com.android.internal.policy.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:2395)
at com.android.internal.policy.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1727)
at android.app.Activity.dispatchKeyEvent(Activity.java:2725)
at android.support.v7.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:543)
at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:53)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java:312)
at com.android.internal.policy.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2310)
at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4127)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4089)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3787)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3844)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3820)
at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3981)
at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2253)
at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1874)
scoreYou.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (v.hasFocus()) {
scoreWe.addTextChangedListener(null); // ADD THIS LINE
scoreYou.addTextChangedListener(new TextWatcher(){}); // Add and Implement this line also
//do rest of your logic
}
}
});
scoreWe.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (v.hasFocus()) {
scoreYou.addTextChangedListener(null); // ADD THIS LINE
scoreYou.addTextChangedListener(new TextWatcher(){}); // Add and Implement this line also
//do rest of your logic
}
}
});