Reemplazar el dígito inglés con el dígito persa en Cadena, excepto las URL
Escribí una TextView personalizada para la cadena persa, este TextView debe reemplazar los dígitos de Inglés a dígitos persa
public class PersianTextView extends TextView { public PersianTextView(Context context) { super(context); } public PersianTextView(Context context, AttributeSet attrs) { super(context, attrs); } public PersianTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override public void setText(CharSequence text, BufferType type) { String t = text.toString(); t = t.replaceAll("0", "۰"); t = t.replaceAll("1", "۱"); t = t.replaceAll("2", "۲"); t = t.replaceAll("3", "۳"); t = t.replaceAll("4", "۴"); t = t.replaceAll("5", "۵"); t = t.replaceAll("6", "۶"); t = t.replaceAll("7", "۷"); t = t.replaceAll("8", "۸"); t = t.replaceAll("9", "۹"); super.setText((CharSequence)t, type); } }
esta vista funciona correctamente, pero si establezco un texto de HTML, los enlaces que contienen dígitos en inglés no se mostrarán como enlace. ¿cuál es la manera más fácil de resolver el problema?
- TextView setScaleX () / setScaleY () y setTextIsSelectable (true) selección
- Android / Java: ¿Determinar si el color del texto se mezclará con el fondo?
- Cómo se desvaneció un carácter único en un TextView
- Desplazamiento de TextView en una ScrollView a una subcadena de texto específica
- Copiar texto de TextView en Android
- Android: No se puede utilizar android.R.drawable como imagen de fondo para textview
- Actividad requiere FLAG_ACTIVITY_NEW_TASK Indicador
- ¿Por qué la marquesina no funciona en Widget?
- Texto de Android alrededor de error de imagen
- ¿Hay una manera de hacer ellipsize = "marquee" siempre desplazarse?
- Android: conversión entre cadenas, SpannedStrings y Spannablestrings
- Disposición de Android: Alinear un TextView y un Spinner
- Hacer una vista de texto de hipervínculo en android
Se necesita de alguna forma hacer concordancia de patrones en los enlaces.
private static final Pattern DIGIT_OR_LINK_PATTERN = Pattern.compile("(\\d|https?:[\\w_/+%?=&.]+)"); // Pattern: (dig|link ) private static final Map<String, String> PERSIAN_DIGITS = new HashMap<>(); static { PERSIAN_DIGITS.put("0", "۰"); PERSIAN_DIGITS.put("1", "۱"); PERSIAN_DIGITS.put("2", "۲"); PERSIAN_DIGITS.put("3", "۳"); PERSIAN_DIGITS.put("4", "۴"); PERSIAN_DIGITS.put("5", "۵"); PERSIAN_DIGITS.put("6", "۶"); PERSIAN_DIGITS.put("7", "۷"); PERSIAN_DIGITS.put("8", "۸"); PERSIAN_DIGITS.put("9", "۹"); } public static String persianDigits(String s) { StringBuffer sb = new StringBuffer(); Matcher m = DIGIT_OR_LINK_PATTERN.matcher(s); while (m.find()) { String t = m.group(1); if (t.length() == 1) { // Digit. t = PERSIAN_DIGITS.get(t); } m.appendReplacement(sb, t); } m.appendTail(sb); return sb.toString(); }
PD
Dependiendo de qué texto, podría ser mejor reemplazar sólo los dígitos fuera de las etiquetas HTML, que está entre >
y <
.
private static final Pattern DIGIT_OR_LINK_PATTERN = Pattern.compile("(\\d|<[^>]*>)", Pattern.DOTALL|Pattern.MULTILINE);
probar esto, no tienen comprobar si esto funciona:
String t = text.toString(); // separate input by spaces ( URLs don't have spaces ) String [] parts = t.split("\\s+"); t=""; // Attempt to convert each item into an URL. for( String item : parts ) try { URL url = new URL(item); t+=item + " "; } catch (MalformedURLException e) { String temp = item; item.replaceAll("0", "۰"); etc. etc. t+=item + " "; } super.setText((CharSequence)t, type);
Convertir números de Inglés a método de números persas:
public String persianNumbers(String englishNumber) { StringBuilder builder = new StringBuilder(); String str = englishNumber; char[] persianChars = {'\u0660', '\u0661', '\u0662', '\u0663', '\u0664', '\u0665', '\u0666', '\u0667', '\u0668', '\u0669'}; for (int i = 0; i < str.length(); i++) { if (Character.isDigit(str.charAt(i))) { builder.append(persianChars[(int) (str.charAt(i)) - 48]); } else { builder.append(str.charAt(i)); } } System.out.println("Number in English : " + str); System.out.println("Number In Persian : " + builder.toString()); return builder.toString(); }
- Compilación de Android Only con API 20: Android 4.4 (KitKat Wear)
- android, recurso de fondo que se vuelve negro en ICS mientras se trabaja en KitKat