Search in sources :

Example 26 with InputFilter

use of android.text.InputFilter in project connect-sdk-client-android by Ingenico-ePayments.

the class RenderCurrency method renderField.

@Override
public View renderField(PaymentProductField field, InputDataPersister inputDataPersister, ViewGroup rowView, PaymentContext paymentContext) {
    if (field == null) {
        throw new InvalidParameterException("Error rendering currency, field may not be null");
    }
    if (inputDataPersister == null) {
        throw new InvalidParameterException("Error rendering currency, inputDataPersister may not be null");
    }
    if (rowView == null) {
        throw new InvalidParameterException("Error rendering currency, rowView may not be null");
    }
    if (paymentContext == null) {
        throw new InvalidParameterException("Error rendering currency, paymentContext may not be null");
    }
    PaymentItem paymentItem = inputDataPersister.getPaymentItem();
    // Create new EditText and set its style, restrictions, mask and keyboardtype
    EditText integerPart = new EditText(rowView.getContext());
    // maxlength is 9 - 2 so no integer overflow
    integerPart.setFilters(new InputFilter[] { new InputFilter.LengthFilter(7) });
    Translator translator = Translator.getInstance(rowView.getContext());
    String label = translator.getPaymentProductFieldLabel(paymentItem.getId(), field.getId());
    integerPart.setHint(label);
    // Set correct inputType type
    switch(field.getDisplayHints().getPreferredInputType()) {
        case INTEGER_KEYBOARD:
            integerPart.setInputType(android.text.InputType.TYPE_CLASS_NUMBER);
            break;
        case STRING_KEYBOARD:
            integerPart.setInputType(android.text.InputType.TYPE_CLASS_TEXT);
            break;
        case PHONE_NUMBER_KEYBOARD:
            integerPart.setInputType(android.text.InputType.TYPE_CLASS_PHONE);
            break;
        case EMAIL_ADDRESS_KEYBOARD:
            integerPart.setInputType(android.text.InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
            break;
        default:
            integerPart.setInputType(android.text.InputType.TYPE_CLASS_TEXT);
            break;
    }
    // Check if this edittext should be obfuscated
    if (field.getDisplayHints().isObfuscate()) {
        integerPart.setTransformationMethod(PasswordTransformationMethod.getInstance());
    }
    // Set values from account on file
    if (inputDataPersister.getAccountOnFile() != null) {
        for (KeyValuePair attribute : inputDataPersister.getAccountOnFile().getAttributes()) {
            if (attribute.getKey().equals(field.getId())) {
                StringFormatter stringFormatter = new StringFormatter();
                String maskedValue = stringFormatter.applyMask(field.getDisplayHints().getMask().replace("9", "*"), attribute.getValue());
                integerPart.setText(maskedValue);
                if (!attribute.isEditingAllowed()) {
                    integerPart.setEnabled(false);
                }
            }
        }
    }
    LinearLayout linearLayout = new LinearLayout(rowView.getContext());
    linearLayout.setOrientation(LinearLayout.HORIZONTAL);
    linearLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    TextView currencySymbol = new TextView(rowView.getContext());
    currencySymbol.setText(paymentContext.getAmountOfMoney().getCurrencyCode().toString());
    TextView separator = new TextView(rowView.getContext());
    String separatorLabel = translator.getPaymentProductFieldLabel(paymentItem.getId(), "separator");
    separator.setText(separatorLabel);
    EditText decimalPart = new EditText(rowView.getContext());
    decimalPart.setHint("00");
    decimalPart.setInputType(android.text.InputType.TYPE_CLASS_NUMBER);
    decimalPart.setFilters(new InputFilter[] { new InputFilter.LengthFilter(2) });
    integerPart.addTextChangedListener(new FieldInputTextWatcherCurrency(inputDataPersister, field.getId(), decimalPart, true));
    decimalPart.addTextChangedListener(new FieldInputTextWatcherCurrency(inputDataPersister, field.getId(), integerPart, false));
    // Restore data that has previously been entered in this field
    if (inputDataPersister.getValue(field.getId()) != null) {
        String value = inputDataPersister.getValue(field.getId());
        if (value.length() > 2) {
            integerPart.setText(value.substring(0, value.length() - 2));
        }
        if (!value.endsWith("00")) {
            decimalPart.setText(value.substring(value.length() - 2));
        }
    }
    LinearLayout.LayoutParams params0 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0f);
    linearLayout.addView(currencySymbol, params0);
    LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f);
    linearLayout.addView(integerPart, params1);
    LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0f);
    linearLayout.addView(separator, params2);
    LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0f);
    linearLayout.addView(decimalPart, params3);
    // Add it to parentView
    rowView.addView(linearLayout);
    return linearLayout;
}
Also used : EditText(android.widget.EditText) InputFilter(android.text.InputFilter) LayoutParams(android.view.ViewGroup.LayoutParams) KeyValuePair(com.globalcollect.gateway.sdk.client.android.sdk.model.paymentproduct.KeyValuePair) PaymentItem(com.globalcollect.gateway.sdk.client.android.sdk.model.paymentproduct.PaymentItem) StringFormatter(com.globalcollect.gateway.sdk.client.android.sdk.formatter.StringFormatter) InvalidParameterException(java.security.InvalidParameterException) Translator(com.globalcollect.gateway.sdk.client.android.exampleapp.translation.Translator) TextView(android.widget.TextView) LinearLayout(android.widget.LinearLayout)

Example 27 with InputFilter

use of android.text.InputFilter in project android2 by aqi00.

the class ViewUtil method getMaxLength.

// 获取编辑框的最大长度,通过反射机制调用隐藏方法
public static int getMaxLength(EditText et) {
    int length = 0;
    try {
        InputFilter[] inputFilters = et.getFilters();
        for (InputFilter filter : inputFilters) {
            Class<?> c = filter.getClass();
            if (c.getName().equals("android.text.InputFilter$LengthFilter")) {
                Field[] f = c.getDeclaredFields();
                for (Field field : f) {
                    if (field.getName().equals("mMax")) {
                        field.setAccessible(true);
                        length = (Integer) field.get(filter);
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return length;
}
Also used : Field(java.lang.reflect.Field) InputFilter(android.text.InputFilter)

Example 28 with InputFilter

use of android.text.InputFilter in project android2 by aqi00.

the class ViewUtil method getMaxLength.

// 获取编辑框的最大长度,通过反射机制调用隐藏方法
public static int getMaxLength(EditText et) {
    int length = 0;
    try {
        InputFilter[] inputFilters = et.getFilters();
        for (InputFilter filter : inputFilters) {
            Class<?> c = filter.getClass();
            if (c.getName().equals("android.text.InputFilter$LengthFilter")) {
                Field[] f = c.getDeclaredFields();
                for (Field field : f) {
                    if (field.getName().equals("mMax")) {
                        field.setAccessible(true);
                        length = (Integer) field.get(filter);
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return length;
}
Also used : Field(java.lang.reflect.Field) InputFilter(android.text.InputFilter)

Example 29 with InputFilter

use of android.text.InputFilter in project platform_packages_apps_Settings by BlissRoms.

the class Utf8ByteLengthFilterTest method testFilter.

@SmallTest
public void testFilter() {
    // Define the variables
    CharSequence source;
    SpannableStringBuilder dest;
    // Constructor to create a LengthFilter
    InputFilter lengthFilter = new Utf8ByteLengthFilter(10);
    InputFilter[] filters = { lengthFilter };
    // filter() implicitly invoked. If the total length > filter length, the filter will
    // cut off the source CharSequence from beginning to fit the filter length.
    source = "abc";
    dest = new SpannableStringBuilder("abcdefgh");
    dest.setFilters(filters);
    dest.insert(1, source);
    String expectedString1 = "aabbcdefgh";
    assertEquals(expectedString1, dest.toString());
    dest.replace(5, 8, source);
    String expectedString2 = "aabbcabcgh";
    assertEquals(expectedString2, dest.toString());
    dest.insert(2, source);
    assertEquals(expectedString2, dest.toString());
    dest.delete(1, 3);
    String expectedString3 = "abcabcgh";
    assertEquals(expectedString3, dest.toString());
    dest.append("12345");
    String expectedString4 = "abcabcgh12";
    assertEquals(expectedString4, dest.toString());
    // 2 Chinese chars == 6 bytes in UTF-8
    source = "\u60a8\u597d";
    dest.replace(8, 10, source);
    assertEquals(expectedString3, dest.toString());
    dest.replace(0, 1, source);
    String expectedString5 = "\u60a8bcabcgh";
    assertEquals(expectedString5, dest.toString());
    dest.replace(0, 4, source);
    String expectedString6 = "\u60a8\u597dbcgh";
    assertEquals(expectedString6, dest.toString());
    // 2 Latin-1 chars == 4 bytes in UTF-8
    source = "\u00a3\u00a5";
    dest.delete(2, 6);
    dest.insert(0, source);
    String expectedString7 = "\u00a3\u00a5\u60a8\u597d";
    assertEquals(expectedString7, dest.toString());
    dest.replace(2, 3, source);
    String expectedString8 = "\u00a3\u00a5\u00a3\u597d";
    assertEquals(expectedString8, dest.toString());
    dest.replace(3, 4, source);
    String expectedString9 = "\u00a3\u00a5\u00a3\u00a3\u00a5";
    assertEquals(expectedString9, dest.toString());
    // filter() explicitly invoked
    dest = new SpannableStringBuilder("abcdefgh");
    CharSequence beforeFilterSource = "TestLengthFilter";
    String expectedAfterFilter = "TestLength";
    CharSequence actualAfterFilter = lengthFilter.filter(beforeFilterSource, 0, beforeFilterSource.length(), dest, 0, dest.length());
    assertEquals(expectedAfterFilter, actualAfterFilter);
}
Also used : Utf8ByteLengthFilter(com.android.settings.bluetooth.Utf8ByteLengthFilter) InputFilter(android.text.InputFilter) SpannableStringBuilder(android.text.SpannableStringBuilder) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 30 with InputFilter

use of android.text.InputFilter in project platform_packages_apps_Settings by BlissRoms.

the class WifiP2pSettings method onCreateDialog.

@Override
public Dialog onCreateDialog(int id) {
    if (id == DIALOG_DISCONNECT) {
        String deviceName = TextUtils.isEmpty(mSelectedWifiPeer.device.deviceName) ? mSelectedWifiPeer.device.deviceAddress : mSelectedWifiPeer.device.deviceName;
        String msg;
        if (mConnectedDevices > 1) {
            msg = getActivity().getString(R.string.wifi_p2p_disconnect_multiple_message, deviceName, mConnectedDevices - 1);
        } else {
            msg = getActivity().getString(R.string.wifi_p2p_disconnect_message, deviceName);
        }
        AlertDialog dialog = new AlertDialog.Builder(getActivity()).setTitle(R.string.wifi_p2p_disconnect_title).setMessage(msg).setPositiveButton(getActivity().getString(R.string.dlg_ok), mDisconnectListener).setNegativeButton(getActivity().getString(R.string.dlg_cancel), null).create();
        return dialog;
    } else if (id == DIALOG_CANCEL_CONNECT) {
        int stringId = R.string.wifi_p2p_cancel_connect_message;
        String deviceName = TextUtils.isEmpty(mSelectedWifiPeer.device.deviceName) ? mSelectedWifiPeer.device.deviceAddress : mSelectedWifiPeer.device.deviceName;
        AlertDialog dialog = new AlertDialog.Builder(getActivity()).setTitle(R.string.wifi_p2p_cancel_connect_title).setMessage(getActivity().getString(stringId, deviceName)).setPositiveButton(getActivity().getString(R.string.dlg_ok), mCancelConnectListener).setNegativeButton(getActivity().getString(R.string.dlg_cancel), null).create();
        return dialog;
    } else if (id == DIALOG_RENAME) {
        mDeviceNameText = new EditText(getActivity());
        mDeviceNameText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(30) });
        if (mSavedDeviceName != null) {
            mDeviceNameText.setText(mSavedDeviceName);
            mDeviceNameText.setSelection(mSavedDeviceName.length());
        } else if (mThisDevice != null && !TextUtils.isEmpty(mThisDevice.deviceName)) {
            mDeviceNameText.setText(mThisDevice.deviceName);
            mDeviceNameText.setSelection(0, mThisDevice.deviceName.length());
        }
        mSavedDeviceName = null;
        AlertDialog dialog = new AlertDialog.Builder(getActivity()).setTitle(R.string.wifi_p2p_menu_rename).setView(mDeviceNameText).setPositiveButton(getActivity().getString(R.string.dlg_ok), mRenameListener).setNegativeButton(getActivity().getString(R.string.dlg_cancel), null).create();
        return dialog;
    } else if (id == DIALOG_DELETE_GROUP) {
        int stringId = R.string.wifi_p2p_delete_group_message;
        AlertDialog dialog = new AlertDialog.Builder(getActivity()).setMessage(getActivity().getString(stringId)).setPositiveButton(getActivity().getString(R.string.dlg_ok), mDeleteGroupListener).setNegativeButton(getActivity().getString(R.string.dlg_cancel), mDeleteGroupListener).create();
        return dialog;
    }
    return null;
}
Also used : AlertDialog(android.app.AlertDialog) EditText(android.widget.EditText) InputFilter(android.text.InputFilter)

Aggregations

InputFilter (android.text.InputFilter)86 EditText (android.widget.EditText)31 TextView (android.widget.TextView)27 View (android.view.View)26 DialogInterface (android.content.DialogInterface)18 AlertDialog (android.app.AlertDialog)17 Paint (android.graphics.Paint)12 Editable (android.text.Editable)12 Spanned (android.text.Spanned)10 TextPaint (android.text.TextPaint)10 LinearLayout (android.widget.LinearLayout)10 TextWatcher (android.text.TextWatcher)9 Bundle (android.os.Bundle)8 SpannableStringBuilder (android.text.SpannableStringBuilder)8 ImageView (android.widget.ImageView)8 SmallTest (android.test.suitebuilder.annotation.SmallTest)7 Button (android.widget.Button)7 Context (android.content.Context)6 AlertDialog (android.support.v7.app.AlertDialog)6 Utf8ByteLengthFilter (com.android.settings.bluetooth.Utf8ByteLengthFilter)6