我希望当edittext中的类型编号显示波斯语或阿拉伯语编号而不是英文编号时,&#34;它与方法settext()配合良好但在我打字时无效。&#34; < /强> 我使用此代码显示波斯语数字,TextView和Button工作正常,但在输入时无法在EditText中工作!
public class FormatHelper {
private static String[] persianNumbers = new String[]{"۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹"};
public static String toPersianNumber(String text) {
if (text.isEmpty())
return "";
String out = "";
int length = text.length();
for (int i = 0; i < length; i++) {
char c = text.charAt(i);
if ('0' <= c && c <= '9') {
int number = Integer.parseInt(String.valueOf(c));
out += persianNumbers[number];
} else if (c == '٫') {
out += '،';
} else {
out += c;
}
}
return out;
}
}
public class LoginActivity extends AppCompatActivity {
myEditText myEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_page);
myEditText = findViewById(R.id.user_pnumber);
myEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
String s = FormatHelper.toPersianNumber(charSequence.toString());
Toast.makeText(LoginActivity.this, s, Toast.LENGTH_SHORT).show();
myEditText.setText(s);
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
我认为必须使用addTextChangedListener
更新 我收到强制关闭app的错误:
FATAL EXCEPTION: main
java.lang.StackOverflowError
at android.text.SpannableStringBuilder.getTextRunAdvances(SpannableStringBuilder.java:1212)
at android.graphics.Paint.getTextRunAdvances(Paint.java:2100)
at android.graphics.Paint.getTextRunAdvances(Paint.java:2064)
at android.text.TextLine.handleText(TextLine.java:969)
at android.text.TextLine.handleRun(TextLine.java:1178)
at android.text.TextLine.measureRun(TextLine.java:617)
at android.text.TextLine.measure(TextLine.java:480)
at android.text.TextLine.metrics(TextLine.java:454)
at android.text.Layout.getLineExtent(Layout.java:993)
at android.text.Layout.getLineStartPos(Layout.java:519)
at android.text.Layout.getHorizontal(Layout.java:856)
at android.text.Layout.getHorizontal(Layout.java:827)
at android.text.Layout.getPrimaryHorizontal(Layout.java:811)
at android.widget.TextView.getFocusedRect(TextView.java:5834)
at android.view.FocusFinder.findNextFocus(FocusFinder.java:120)
at android.view.FocusFinder.findNextFocus(FocusFinder.java:94)
at android.view.FocusFinder.findNextFocus(FocusFinder.java:65)
at android.view.ViewGroup.focusSearch(ViewGroup.java:681)
at android.view.ViewGroup.focusSearch(ViewGroup.java:683)
at android.view.ViewGroup.focusSearch(ViewGroup.java:683)
at android.view.ViewGroup.focusSearch(ViewGroup.java:683)
at android.view.ViewGroup.focusSearch(ViewGroup.java:683)
at android.view.ViewGroup.focusSearch(ViewGroup.java:683)
at android.view.ViewGroup.focusSearch(ViewGroup.java:683)
at android.view.View.focusSearch(View.java:6547)
at android.widget.TextView.onCreateInputConnection(TextView.java:6489)
at android.view.inputmethod.InputMethodManager.startInputInner(InputMethodManager.java:1213)
at android.view.inputmethod.InputMethodManager.restartInput(InputMethodManager.java:1164)
at android.widget.TextView.setText(TextView.java:4231)
at android.widget.TextView.setText(TextView.java:4148)
at android.widget.EditText.setText(EditText.java:104)
at android.widget.TextView.setText(TextView.java:4123)
at LoginActivity$2.onTextChanged(LoginActivity.java:76)
at android.widget.TextView.sendOnTextChanged(TextView.java:8284)
at android.widget.TextView.setText(TextView.java:4314)
at android.widget.TextView.setText(TextView.java:4148)
at android.widget.EditText.setText(EditText.java:104)
at android.widget.TextView.setText(TextView.java:4123)
答案 0 :(得分:1)
使用此代码
好的,你需要像et_change
这样的全局变量import React from 'react';
import MaskedInput from 'react-text-mask';
import PropTypes from 'prop-types';
import { withStyles } from 'material-ui/styles';
import TextField from 'material-ui/TextField';
const styles = theme => ({
container: {
display: 'flex',
flexWrap: 'wrap',
},
input: {
margin: theme.spacing.unit,
},
});
const TextMaskCustom = (props) => {
const { inputRef, ...other } = props;
return (
<MaskedInput
{...other}
ref={inputRef}
mask={['(', /[1-9]/, /\d/, /\d/, ')', ' ', /\d/, /\d/, /\d/, '-', /\d/, /\d/, /\d/, /\d/]}
placeholderChar={'\u2000'}
showMask
/>
);
}
TextMaskCustom.propTypes = {
inputRef: PropTypes.func.isRequired,
};
class FormattedInputs extends React.Component {
state = {
textmask: '(1 ) - ',
};
handleChange = name => event => {
this.setState({
[name]: event.target.value,
});
};
render() {
const { classes } = this.props;
return (
<div className={classes.container}>
<TextField
id="maskExample"
label="Mask Example"
className={classes.textField}
margin="normal"
InputProps={{
inputComponent: TextMaskCustom,
value:this.state.textmask,
onChange: this.handleChange('textmask'),
}}
/>
</div>
);
}
}
FormattedInputs.propTypes = {
classes: PropTypes.object.isRequired,
};
export default withStyles(styles)(FormattedInputs);
并在TextWatcher中使用它,如下所示
private boolean et_change = false;
希望它可以帮到你
答案 1 :(得分:1)
出现异常是因为您在onTextChanged
事件中设置了文本。这会再次触发事件本身,而这个新事件会无限制地触发它。
我建议您使用某种布尔条件来仅触发事件一次。
答案 2 :(得分:0)
您只需要使用支持“波斯数字”的波斯字体查看即可。 例如,使用shabnam_fd字体作为styles.xml文件中的字体系列:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="drawerArrowStyle">@style/DrawerIcon</item>
<item name="android:fontFamily">@font/shabnam_fd</item>
</style>
然后在字体文件夹中添加shabnam_fd.ttf字体。