2

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

    <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();代替$in android:digits

我不知道如何android:digits从我的程序中进行设置。

感谢 Agarwal 解决了。我只需要更彻底地阅读文档。

4

3 回答 3

3

试试这个:

<EditText
    android:inputType="number"
    android:digits="0123456789."
/>

从代码:

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

但是,它允许用户包含几个“.”。

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

EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
于 2012-04-06T07:54:18.110 回答
0

是的,你可以在这里查看

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

setKeyListener(KeyListener)
于 2012-04-06T06:05:19.177 回答
0

For those interested, here is how I solved the original question. It is the complete implementation of a currency edit text that can handle multiple locales. Still may be some problems (Doesn't seem to display Japanese currency symbol correctly, and I can't get the keyboard I want (12_KEY)), but otherwise, some may find this helpful.

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 ));
        }
    }
};
}

And the accompanying XML layout:

<?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>
于 2012-04-07T22:44:12.710 回答