Android:是否有一种等效于XML android:digits的方法?

Android: Is there a method equivalent to XML android:digits?

在布局xml中,使用以下内容定义可以显示货币的EditText。

1
2
3
4
5
6
7
    <EditText
    android:id="@+id/et1"
    android:layout_width="210dp"
    android:layout_height="wrap_content"
    android:imeOptions="actionNext"
    android:inputType="phone"
    android:digits="0123456789.,$">

但是,这不是本地化的。 我希望能够使用NumberFormat.getCurrencyInstance().getCurrency().getSymbol();返回的符号代替android:digits中的$

我不知道如何在程序中设置android:digits

解决了,感谢阿加瓦尔。 我只需要更彻底地阅读文档。


尝试这个:

1
2
3
4
<EditText
    android:inputType="number"
    android:digits="0123456789."
/>

从代码:

1
weightInput.setKeyListener(DigitsKeyListener.getInstance("0123456789."));

但是,它允许用户包括多个""。

您也可以这样做以接受数字...

1
2
EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_NUMBER);


对于那些感兴趣的人,这是我如何解决原始问题。 这是可以处理多个语言环境的货币编辑文本的完整实现。 仍然可能有一些问题(似乎无法正确显示日本货币符号,并且我无法获得想要的键盘(12_KEY)),但除此之外,有些人可能会发现此问题。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
public class CurrencytestActivity extends Activity
{
private static final Integer    MAX_VALUE_DIGITS    = 9;
EditText        et1;
NumberFormat    mCurrencyFormatter;
CurrencyTextWatcher tw;

@Override public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Get info about local currency
    mCurrencyFormatter = NumberFormat.getCurrencyInstance();
    int fractionDigits = mCurrencyFormatter.getCurrency().getDefaultFractionDigits();

    et1 = (EditText)findViewById(R.id.et1); // Get a handle to the TextEdit control

    // Add local currency symbol to digits allowed for EditText display and use
    // DigitsKeyListener to tell the control.  Unfortunately, this also resets the inputType
    // that is specified in the XML layout file.  Don't know how to fix that yet.
    // Also, this doesn't seem to work for Japanese (probably due to UNICODE or something).
    // The symbol gets added to displayCharacters, but the EditText doesn't use it.
    String displayCharacters ="0123456789.," + mCurrencyFormatter.getCurrency().getSymbol();
    et1.setKeyListener(DigitsKeyListener.getInstance( displayCharacters ));        

    // Add a text watcher to the EditText to manage currency digit entry. The TextWatcher
    // won't allow the symbol or decimal or comma to be entered by the user, but they are
    // still displayed when the result is formatted in afterTextChanged().
    tw = new CurrencyTextWatcher( MAX_VALUE_DIGITS, fractionDigits );        
    et1.addTextChangedListener( tw );        
    et1.setCursorVisible( false );

    ((Button)findViewById(R.id.button1)).setOnClickListener(onButtonClick);
}

public class CurrencyTextWatcher implements TextWatcher
{
    boolean mEditing;   // Used to prevent recursion
    Double  mAmount;
    int     mDigitCount, mMaxDigits, mFractionDivisor;

    public CurrencyTextWatcher( int maxDigits, int fractionDigits )
    {
        mEditing = false;
        mFractionDivisor = (fractionDigits == 0) ? 1 : ((fractionDigits == 1) ? 10 : 100);
        mAmount = 0.0;
        mDigitCount = 0;
        mMaxDigits = maxDigits;
    }

    public synchronized void afterTextChanged(Editable s)
    {
        // Don't update EditText display if we are editing
        if ( !mEditing )
        {
            // Under cover of mEditing, update the EditText display with
            // the newly formatted value
            mEditing = true;
            s.replace( 0, s.length(), mCurrencyFormatter.format( mAmount ));
            mEditing = false;
        }
    }
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
    public double GetAmount() { return( mAmount ); }
    public void onTextChanged(CharSequence s, int start, int before, int count)
    {
        if ( !mEditing )
        {
            // Added a digit to the value
            if (( count == 1 ) && ( mDigitCount < mMaxDigits ))
            {
                // Obtain the added character
                CharSequence x = s.subSequence( start, start + count );

                // Ignore any characters other than number digits for addition to value
                if (( x.charAt( 0 ) >= '0') && ( x.charAt( 0 ) <= '9'))
                {
                    // Multiply by ten to shift existing digits to the left and
                    // add in the new digit as the decimal place appropriate to this currency
                    mAmount = (mAmount * 10) + (Double.parseDouble( x.toString() ) / mFractionDivisor);
                    mDigitCount += 1;
                }
            }

            // Delete last digit from the value
            else if (( count == 0 ) && ( mDigitCount > 0))
            {
                // Subtract the amount of the last digit and divide by ten to
                // effectively delete the last character entered
                mAmount -= (mAmount % (0.001 * mFractionDivisor) );
                mAmount /= 10;
                mDigitCount -= 1;
            }
        }
    }
}

private View.OnClickListener onButtonClick = new View.OnClickListener()
{
    @Override public void onClick(View v)
    {
        if (v.getId() == R.id.button1 )
        {
            // Get the value from the textwatcher and display it.
            double mAmountTest = tw.GetAmount();
            ((TextView)findViewById(R.id.tv1)).setText(mCurrencyFormatter.format( mAmountTest ));
        }
    }
};
}

以及随附的XML布局:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center|top"
android:orientation="vertical">

<EditText
    android:id="@+id/et1"
    android:layout_width="210dp"
    android:layout_height="wrap_content"
    android:imeOptions="actionNext"
    android:inputType="phone">
    <requestFocus />
</EditText>

<TextView
    android:id="@+id/tv1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Extract from TextWatcher" />
</LinearLayout>


是的,你可以在这里检查

http://developer.android.com/reference/android/widget/EditText.html
对于几乎每个属性,都存在等效的方法。

1
setKeyListener(KeyListener)