从API 21开始,您可以使用XML属性androID:letterSpacing =“2”或者来自代码myEditText.setLetterSpacing(2);
在API 21之前,使用带有以下代码的TextWatcher
private static final String LETTER_SPACING = " ";private EditText myEditText;private String myPrevIoUsText;...// Get the vIEwsmyEditText = (EditText) v.findVIEwByID(R.ID.edt_code);myEditText.addTextChangedListener(this);...@OverrIDepublic voID beforeTextChanged(CharSequence s,int start,int count,int after) { // nothing here}@OverrIDepublic voID onTextChanged(CharSequence s,int before,int count) { // nothing here}@OverrIDepublic voID afterTextChanged(Editable s) { String text = s.toString(); // Only update the EditText when the user modify it -> Otherwise it will be triggered when adding spaces if (!text.equals(myPrevIoUsText)) { // Remove spaces text = text.replace(" ",""); // Add space between each character StringBuilder newText = new StringBuilder(); for (int i = 0; i < text.length(); i++) { if (i == text.length() - 1) { // Do not add a space after the last character -> Allow user to delete last character newText.append(Character.toupperCase(text.charat(text.length() - 1))); } else { newText.append(Character.toupperCase(text.charat(i)) + LETTER_SPACING); } } myPrevIoUsText = newText.toString(); // Update the text with spaces and place the cursor at the end myEditText.setText(newText); myEditText.setSelection(newText.length()); }}总结
以上是内存溢出为你收集整理的android – 在textview中设置字母之间的空格全部内容,希望文章能够帮你解决android – 在textview中设置字母之间的空格所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)