在TextView中字母之间的空间集合 [英] set spaces between letters in textview

查看:124
本文介绍了在TextView中字母之间的空间集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是有办法字母之间的自定义空间(像素)设置为EDITTEXT?我只是如何设置线之间的空间中,但在同一行字母之间的bot

is there a way to set a custom space (in pixels) between letters to an editText? I found only how to set spaces between the lines, but bot between letters on the same row

推荐答案

我不得不做这我今天所以这里对这个问题的一些更新:

I had to do this myself today so here are some updates about this problem :

从21 API,你可以使用XML属性的android:字母间距=2或code myEditText.setLetterSpacing(2);

From API 21 you can use XML attribute android:letterSpacing="2" or from code myEditText.setLetterSpacing(2);

在21 API,使用TextWatcher具有以下code

Before API 21, use a TextWatcher with the following code

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 start, 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());
    }
}

这篇关于在TextView中字母之间的空间集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆