Java 如何修复方向更改时的TextWatcher?
我想使用Java 如何修复方向更改时的TextWatcher?,java,android,textwatcher,Java,Android,Textwatcher,我想使用TextWatcher为多个editText编写一个验证器 我已经有了验证用户编写的数据是否正确的方法。 还添加了验证以检查editText是否为空。但在这里,它并没有像我想的那样起作用。 当手机改变方向时,这个TextWatcher也会被激活,我不希望这种情况发生。我希望它仅在用户从editText中删除数据时工作 代码如下: @Override final public void beforeTextChanged(CharSequence s, int start, int cou
TextWatcher
为多个editText
编写一个验证器
我已经有了验证用户编写的数据是否正确的方法。
还添加了验证以检查editText
是否为空。但在这里,它并没有像我想的那样起作用。
当手机改变方向时,这个TextWatcher
也会被激活,我不希望这种情况发生。我希望它仅在用户从editText
中删除数据时工作
代码如下:
@Override
final public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
final public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override
final public void afterTextChanged(Editable s) {
String text = textView.getText().toString();
if(textView.getText().toString().isEmpty()){
textView.setError("Can't be empty");
ParametersFrag.isCorrect = false;
} else {
validate(textView, text);
}
}
片段中的validate
部分如下所示:
editText2_5.addTextChangedListener(new ParameterValidator(editText2_5) {
@Override
public void validate(TextView textView, String text) {
double parDouble = Double.parseDouble(Tab2Fragment.editText2_5.getText().toString());
if (parDouble < sth_min) {
textView.setError(getString(R.string.err_min_value) + " " + sth_min);
ParametersFrag.isCorrect = false;
} else if (parDouble > sth_max) {
textView.setError(getString(R.string.err_max_value) + " " + sth_max);
ParametersFrag.isCorrect = false;
} else {
ParametersFrag.isCorrect = true;
}
}
});
editText2_5.addTextChangedListener(新参数验证器(editText2_5)){
@凌驾
公共无效验证(文本视图文本视图,字符串文本){
double parDouble=double.parseDouble(Tab2Fragment.editText2_5.getText().toString());
如果{
textView.setError(getString(R.string.err\u min\u value)+“”+sth\u min);
参数sfrag.isCorrect=false;
}否则,如果{
textView.setError(getString(R.string.err\u max\u value)+“”+sth\u max);
参数sfrag.isCorrect=false;
}否则{
参数sfrag.isCorrect=true;
}
}
});
问题是我怎样才能使它不在方向改变时触发?
谢谢试试这个:
@Override
public void onConfigurationChanged(@NotNull Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
switch (newConfig.orientation) {
case Configuration.ORIENTATION_LANDSCAPE:
editText2_5.addTextChangedListener(null);
break;
case Configuration.ORIENTATION_PORTRAIT:
// editText2_5.addTextChangedListener(null);
// ============= OR =============
// editText2_5.addTextChangedListener(new ParameterValidator(editText2_5) {
// @Override
// public void validate(TextView textView, String text) {
// double parDouble = Double.parseDouble(Tab2Fragment.editText2_5.getText().toString());
// if (parDouble < sth_min) {
// textView.setError(getString(R.string.err_min_value) + " " + sth_min);
// ParametersFrag.isCorrect = false;
// } else if (parDouble > sth_max) {
// textView.setError(getString(R.string.err_max_value) + " " + sth_max);
// ParametersFrag.isCorrect = false;
// } else {
// ParametersFrag.isCorrect = true;
// }
// }
// });
break;
default:
break;
}
}
@覆盖
公共void onconfiguration已更改(@NotNull Configuration newConfig){
super.onConfigurationChanged(newConfig);
//检查屏幕的方向
开关(newConfig.orientation){
案例配置。方向与景观:
editText2_5.addTextChangedListener(空);
打破
案例配置。方向\u纵向:
//editText2_5.addTextChangedListener(空);
//================或=============
//editText2_5.addTextChangedListener(新参数验证器(editText2_5){
//@覆盖
//公共无效验证(文本视图文本视图,字符串文本){
//double parDouble=double.parseDouble(Tab2Fragment.editText2_5.getText().toString());
//如果{
//textView.setError(getString(R.string.err\u min\u value)+“”+sth\u min);
//参数sfrag.isCorrect=false;
//}否则如果(parDouble>sth_max){
//textView.setError(getString(R.string.err\u max\u value)+“”+sth\u max);
//参数sfrag.isCorrect=false;
//}其他{
//参数sfrag.isCorrect=true;
// }
// }
// });
打破
违约:
打破
}
}
注:上述代码为Kotlin
然后在使用此布尔变量进行包装验证之后
editText2_5.addTextChangedListener(new ParameterValidator(editText2_5) {
@Override
public void validate(TextView textView, String text) {
if(!doesDeviceTitled){
//Your validation code here.
}
}});
快乐编码:)好的,现在我想,您需要在
AndroidManifest.xml中的
标记中声明android:configChanges=“orientation”
,并删除以下内容:
public void onConfigurationChanged(@NotNull Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
switch (newConfig.orientation) {
case Configuration.ORIENTATION_LANDSCAPE:
editText2_5.addTextChangedListener(null);
break;
case Configuration.ORIENTATION_PORTRAIT:
// editText2_5.addTextChangedListener(null);
// ============= OR =============
// editText2_5.addTextChangedListener(new ParameterValidator(editText2_5) {
// @Override
// public void validate(TextView textView, String text) {
// double parDouble = Double.parseDouble(Tab2Fragment.editText2_5.getText().toString());
// if (parDouble < sth_min) {
// textView.setError(getString(R.string.err_min_value) + " " + sth_min);
// ParametersFrag.isCorrect = false;
// } else if (parDouble > sth_max) {
// textView.setError(getString(R.string.err_max_value) + " " + sth_max);
// ParametersFrag.isCorrect = false;
// } else {
// ParametersFrag.isCorrect = true;
// }
// }
// });
break;
default:
break;
}
}
public void onconfiguration已更改(@NotNull Configuration newConfig){
super.onConfigurationChanged(newConfig);
//检查屏幕的方向
开关(newConfig.orientation){
案例配置。方向与景观:
editText2_5.addTextChangedListener(空);
打破
案例配置。方向\u纵向:
//editText2_5.addTextChangedListener(空);
//================或=============
//editText2_5.addTextChangedListener(新参数验证器(editText2_5){
//@覆盖
//公共无效验证(文本视图文本视图,字符串文本){
//double parDouble=double.parseDouble(Tab2Fragment.editText2_5.getText().toString());
//如果{
//textView.setError(getString(R.string.err\u min\u value)+“”+sth\u min);
//参数sfrag.isCorrect=false;
//}否则如果(parDouble>sth_max){
//textView.setError(getString(R.string.err\u max\u value)+“”+sth\u max);
//参数sfrag.isCorrect=false;
//}其他{
//参数sfrag.isCorrect=true;
// }
// }
// });
打破
违约:
打破
}
}
谢谢您的回答,但如果我理解正确,使用此代码验证将只在一个方向上有效,我希望在两个方向上都有效。谢谢您的回答,但与@Dheeraj answer一样,我认为它只在一个方向上有效。目标是阻止在方向更改时触发检查textView是否为空的代码的第一部分。
public void onConfigurationChanged(@NotNull Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
switch (newConfig.orientation) {
case Configuration.ORIENTATION_LANDSCAPE:
editText2_5.addTextChangedListener(null);
break;
case Configuration.ORIENTATION_PORTRAIT:
// editText2_5.addTextChangedListener(null);
// ============= OR =============
// editText2_5.addTextChangedListener(new ParameterValidator(editText2_5) {
// @Override
// public void validate(TextView textView, String text) {
// double parDouble = Double.parseDouble(Tab2Fragment.editText2_5.getText().toString());
// if (parDouble < sth_min) {
// textView.setError(getString(R.string.err_min_value) + " " + sth_min);
// ParametersFrag.isCorrect = false;
// } else if (parDouble > sth_max) {
// textView.setError(getString(R.string.err_max_value) + " " + sth_max);
// ParametersFrag.isCorrect = false;
// } else {
// ParametersFrag.isCorrect = true;
// }
// }
// });
break;
default:
break;
}
}