Android EditText中的字母间距 [英] Letter spacing in Android EditText

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

问题描述

我试图显示一个用空格分隔的字母的文本框.

I am trying to Display a text box with letters separated by spaces.

    EditText wordText = (EditText) findViewById(R.id.word_text);
    wordText.setPaintFlags(wordText.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
    InputFilter[] filterArray = new InputFilter[2];
    filterArray[0] = new InputFilter.AllCaps();
    filterArray[1] = new InputFilter.LengthFilter(10);
    wordText.setGravity(Gravity.CENTER);

这是布局文件.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/match_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:layout_marginTop="100dp">

    <EditText android:id="@+id/word_text"
        android:layout_width="200dip"
        android:layout_height="wrap_content"
        android:hint="Your Word"
        android:maxLength="15"
        android:inputType="textCapWords"/>

    <Button
        android:id="@+id/match_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="start"
        android:text="Match"
        android:layout_marginTop="50dp"/>
</LinearLayout>

如何在API 12中的字母之间添加空格.我不想使用setLetterSpacing().

How can I add space between letters in API 12. I dont want to use setLetterSpacing().

此外,当我开始键入文字时,setGravity(Gravity.CENTER)导致我的字母从框的中央出现.如何使它从左到右显示?如果设置Gravity.LEFT,则EditText本身将移至LinearLayout的左侧.有人可以帮忙吗?

Also, when I start typing, setGravity(Gravity.CENTER) is causing my letters to appear from the center of the box. How do i make it appear from Left to right ? If I set Gravity.LEFT, the EditText itself moves to the left in the LinearLayout. Can anyone help ?

推荐答案

  1. 设置TextWatcher(此示例仅在EditText字段中输入数字,如有必要,可以更改它)

  1. Make TextWatcher (this example for enter only digits into EditText field, you can change it if necessary)

public class CustomTextWatcher implements TextWatcher{

    private static final char space = ' ';

public CodeTextWatcher (){}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}

@Override
public void afterTextChanged(Editable s) {

    // Remove all spacing char
    int pos = 0;
    while (true) {
        if (pos >= s.length()) break;
        if (space == s.charAt(pos) && (((pos + 1) % 2) != 0 || pos + 1 == s.length())) {
            s.delete(pos, pos + 1);
        } else {
            pos++;
        }
    }

    // Insert char where needed.
    pos = 1;
    while (true) {
        if (pos >= s.length()) break;
        final char c = s.charAt(pos);
        // Only if its a digit where there should be a space we insert a space
        if ("0123456789".indexOf(c) >= 0) {
            s.insert(pos, "" + space);
        }
        pos += 2;
    }
}

}

将此textWatcher附加到您的EditText:

Attach this textWatcher to your EditText:

 EditText.addTextChangedListener(new CustomTextWatcher());

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

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