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 の代わりに 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

興味のある方のために、これが私が元の質問をどのように解決したかです。これは、複数のロケールを処理できる通貨編集テキストの完全な実装です。それでも問題が発生する可能性があります(日本の通貨記号が正しく表示されないようで、必要なキーボード(12_KEY)を取得できません)が、そうでない場合は、これが役立つ場合があります。

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レイアウト:

<?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 に答える
0

はい、ここで確認できます

http://developer.android.com/reference/android/widget/EditText.html ほぼすべての属性に対して、同等のメソッドが存在します。

setKeyListener(KeyListener)
于 2012-04-06T06:05:19.177 に答える