Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 在textview中设置字母之间的空格大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
有没有办法在editText的字母之间设置自定义空格(以像素为单位)?我只找到了如何在行之间设置空格,但是在同一行上的字母之间设置了空格

解决方法

我今天必须自己这样做,所以这里有一些关于这个问题的更新:

从API 21开始,您可以使用XML属性android:letterSpacing =“2”或者来自@L_620_4@myEditText.setLetterSpacing(2);

在API 21之前,使用带有以下代码的TextWatcher

private static final String LETTER_SPACING = " ";

private EditText myEditText;

private String myPrevIoUsText;

...
// Get the views
myEditText = (EditText) v.findViewById(R.id.edt_codE);

myEditText.addTextChangedListener(this);
...

@Override
public void beforeTextChanged(CharSequence s,int start,int count,int after) {
    // Nothing here
}

@Override
public void ontextChanged(CharSequence s,int before,int count) {
    // Nothing here
}

@Override
public 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中设置字母之间的空格所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。