gpt4 book ai didi

android - 编辑文本中的波斯数字在我打字时不起作用

转载 作者:搜寻专家 更新时间:2023-11-01 09:29:03 24 4
gpt4 key购买 nike

我希望在 edittext 中键入数字时显示波斯语或阿拉伯语数字而不是英语数字,“它与方法 settext() 配合使用效果很好,但在我输入时不起作用。”我使用此代码显示波斯数字,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

更新我收到强制关闭应用程序的错误:

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)

最佳答案

使用此代码

好的,你需要像 et_change 这样的全局变量

 private boolean et_change = false;

在 TextWatcher 中像下面这样使用它

myEditText.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) {
if (et_change) {
et_change = false;
return;
}
et_change = true;


String s = FormatHelper.toPersianNumber(charSequence.toString());
myEditText.setText(s);
}



@Override
public void afterTextChanged(Editable s) {

}
});

希望对你有帮助

关于android - 编辑文本中的波斯数字在我打字时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48750052/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com