Java tutorial
//package com.java2s; //License from project: Apache License import android.support.annotation.NonNull; public class Main { private static final char[] PINYIN_T9_MAP = { '2', '2', '2', '3', '3', '3', '4', '4', '4', '5', '5', '5', '6', '6', '6', '7', '7', '7', '7', '8', '8', '8', '9', '9', '9', '9' }; public static char formatCharToT9(char c) { if (c >= 'A' && c <= 'Z') { return PINYIN_T9_MAP[c - 'A']; } else if (c >= 'a' && c <= 'z') { return PINYIN_T9_MAP[c - 'a']; } else if (isValidT9Key(c)) { return c; } return '\0'; } public static boolean isValidT9Key(char c) { return ((c >= '0') && (c <= '9')) || (c == ',') || (c == '+') || (c == '*') || (c == '#'); } public static boolean isValidT9Key(@NonNull CharSequence key) { final int LEN = key.length(); for (int i = 0; i < LEN; i++) { if (!isValidT9Key(key.charAt(i))) { return false; } } return true; } }