edittext android中的字符映射

时间:2014-09-08 06:10:13

标签: android

我想制作我的编辑文字,就像我写字符" g"它的相关地图自定义字符应该像印地语一样写成它的"जी"

我认为应该有角色映射,但没有任何知识可以帮助我 该怎么做

其他应用https://play.google.com/store/apps/details?id=nichetech.hindi.editor也是这样做的,有离线和在线选项

在线正在谷歌翻译的帮助,但如果我选择离线,那么写作就像这样发生

在这里你可以看到键盘是英文的,但写作是用印地语编写的

enter image description here

由于

我是否有用英语写作的方式,它的相关映射字符只能在我的应用程序的EditText中编写。

有没有人这样做,请帮助我,怎么做

4 个答案:

答案 0 :(得分:7)

为了完成你所追求的目标,我将创建一个映射到其他字符的字符的HashMap。如果未映射某些特定字符,则将其打印出来。这是我提出的一个例子:

final HashMap<Character, Character> charMap = new HashMap<Character, Character>();
charMap.put('q', '1');
charMap.put('w', '2');
charMap.put('e', '3');
charMap.put('r', '4');
charMap.put('t', '5');
charMap.put('y', '6');

final EditText editText = (EditText) findViewById(R.id.editText);

editText.addTextChangedListener(new TextWatcher() {
    boolean replaced;

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        Log.e("TAG", start + " " + before + " " + count);
        // Check in lower case
        String oldStr = s.toString().toLowerCase();
        StringBuilder newStr = new StringBuilder(s);

        // Loop through changed chars
        for (int i = 0; i < count; i++) {
            // Replace if a substitution is avaiable
            Character replacement = charMap.get(oldStr.charAt(start + i));
            if (replacement != null) {
                replaced = true;
                newStr.setCharAt(start + i, replacement);
            }
        }

        if (replaced) {
            replaced = false;
            editText.setText(newStr);
            // Move cursor after the new chars
            editText.setSelection(start + count);
        }

    }

    @Override
    public void afterTextChanged(Editable s) {

    }
});

优点:

  • 在寻找替代品时忽略大小写。 (q = Q = 1)
  • 立即替换单个和多个字符
  • 不循环整个字符串
  • 可以在另一个字符串的中间替换

缺点:

  • 您必须为要替换的每个字符都有一个HashMap条目
  • ...

作为附注,我想列举一下你的应用程序的“在线版本”有一些限制:

  • 仅在输入空格,新行或标点符号时才进行转换。
  • 您无法为已转换的字词添加字母。

应用程序“离线”版本也有一个小错误:

  • 它不会转换使用Swipe
  • 复制或书写的单词

答案 1 :(得分:0)

我猜你最好不要创建自己的* .ttf文件,包含所有字符映射。您可以在EditText(或您正在使用的任何文本小部件)上设置它。欢迎在资产/目录(或您可以阅读的任何目录)中使用TTF字体。您可以使用以下命令创建Typeface对象:

Typeface.createFromAsset(AssetManager mgr, String path)

并将其设置为:

textView.setTypeface(mTypeface);

答案 2 :(得分:0)

关键字符映射文件(.kcm文件)负责将Android密钥代码与修饰符的组合映射到Unicode字符。具有密钥的所有内部(内置)输入设备都需要特定于设备的密钥布局文件,如果只告诉系统该设备只是特殊用途(不是全键盘)。

特定于设备的键布局文件对于外部键盘是可选的,通常根本不需要。系统提供适用于许多外部键盘的通用键字符映射。

如果没有特定于设备的密钥布局文件,则系统将选择默认值。

关键字符映射文件由USB供应商,产品(和可选版本)ID或输入设备名称定位。

关键字符映射文件由USB供应商,产品(和可选版本)ID或输入设备名称定位。

假设用户一起按A和SHIFT。系统首先找到与KEYCODE_A关联的属性和行为集。

键A {     标签:'A'     基地:'a'     转移,封锁:'A'     ctrl,alt,meta:none }

ey ESCAPE {     base:fallback BACK     alt,meta:fallback HOME     ctrl:后备MENU }

键A {     标签:'A'     2号'     基地:'a'     转移,封锁:'A'     alt:'#'     shift + alt,capslock + alt:none }

关键SPACE {     标签: ' '     号码:''     基地:''     转变:''     alt:'\ uef01'     shift + alt:'\ uef01' }

答案 3 :(得分:0)

public void MapCharacter() {
    this.singleChar.put((Object)"a", (Object)"\u0905");
    this.singleChar.put((Object)"b", (Object)"\u092c");
    this.singleChar.put((Object)"c", (Object)"\u091a");
    this.singleChar.put((Object)"d", (Object)"\u0926");
    this.singleChar.put((Object)"e", (Object)"\u090f");
    this.singleChar.put((Object)"f", (Object)"\u092b");
    this.singleChar.put((Object)"g", (Object)"\u0917");
    this.singleChar.put((Object)"h", (Object)"\u0939");
    this.singleChar.put((Object)"i", (Object)"\u0907");
    this.singleChar.put((Object)"j", (Object)"\u091c");
    this.singleChar.put((Object)"k", (Object)"\u0915");
    this.singleChar.put((Object)"l", (Object)"\u0932");
    this.singleChar.put((Object)"m", (Object)"\u092e");
    this.singleChar.put((Object)"n", (Object)"\u0928");
    this.singleChar.put((Object)"o", (Object)"\u0913");
    this.singleChar.put((Object)"p", (Object)"\u092a");
    this.singleChar.put((Object)"q", (Object)"");
    this.singleChar.put((Object)"r", (Object)"\u0930");
    this.singleChar.put((Object)"s", (Object)"\u0938");
    this.singleChar.put((Object)"t", (Object)"\u0924");
    this.singleChar.put((Object)"u", (Object)"\u0909");
    this.singleChar.put((Object)"v", (Object)"\u0935");
    this.singleChar.put((Object)"w", (Object)"\u0935");
    this.singleChar.put((Object)"x", (Object)"\u0915\u094d\u0937");
    this.singleChar.put((Object)"y", (Object)"\u092f");
    this.singleChar.put((Object)"z", (Object)"\u091d");
    this.singleChar.put((Object)"A", (Object)"\u0906");
    this.singleChar.put((Object)"B", (Object)"\u092c");
    this.singleChar.put((Object)"C", (Object)"\u091a");
    this.singleChar.put((Object)"D", (Object)"\u0921");
    this.singleChar.put((Object)"E", (Object)"\u090d");
    this.singleChar.put((Object)"F", (Object)"\u092b");
    this.singleChar.put((Object)"G", (Object)"\u0917");
    this.singleChar.put((Object)"H", (Object)"\u0903");
    this.singleChar.put((Object)"I", (Object)"\u0908");
    this.singleChar.put((Object)"J", (Object)"\u091c");
    this.singleChar.put((Object)"K", (Object)"\u0915");
    this.singleChar.put((Object)"L", (Object)"\u0933");
    this.singleChar.put((Object)"M", (Object)"\u0902");
    this.singleChar.put((Object)"N", (Object)"\u0923");
    this.singleChar.put((Object)"O", (Object)"\u0911");
    this.singleChar.put((Object)"P", (Object)"\u092a");
    this.singleChar.put((Object)"Q", (Object)"");
    this.singleChar.put((Object)"R", (Object)"\u0930");
    this.singleChar.put((Object)"S", (Object)"\u0936");
    this.singleChar.put((Object)"T", (Object)"\u091f");
    this.singleChar.put((Object)"U", (Object)"\u090a");
    this.singleChar.put((Object)"V", (Object)"\u0935");
    this.singleChar.put((Object)"W", (Object)"\u0935");
    this.singleChar.put((Object)"X", (Object)"\u0915\u094d\u0937");
    this.singleChar.put((Object)"Y", (Object)"\u092f");
    this.singleChar.put((Object)"Z", (Object)"\u091d");
    this.singleChar.put((Object)"1", (Object)"\u0967");
    this.singleChar.put((Object)"2", (Object)"\u0968");
    this.singleChar.put((Object)"3", (Object)"\u0969");
    this.singleChar.put((Object)"4", (Object)"\u096a");
    this.singleChar.put((Object)"5", (Object)"\u096b");
    this.singleChar.put((Object)"6", (Object)"\u096c");
    this.singleChar.put((Object)"7", (Object)"\u096d");
    this.singleChar.put((Object)"8", (Object)"\u096e");
    this.singleChar.put((Object)"9", (Object)"\u096f");
    this.singleChar.put((Object)"0", (Object)"\u0966");
    this.singleChar.put((Object)"#", (Object)"\u0953");
    this.singleChar.put((Object)"$", (Object)" \u0951");
    this.singleChar.put((Object)"^", (Object)"\u094d");
    this.singleChar.put((Object)":", (Object)"\u0903");
    this.delimtrChar.put((Object)" ", (Object)" ");
    this.delimtrChar.put((Object)"!", (Object)"!");
    this.delimtrChar.put((Object)"@", (Object)"\u0970");
    this.delimtrChar.put((Object)"%", (Object)"%");
    this.delimtrChar.put((Object)"&", (Object)"\u093d");
    this.delimtrChar.put((Object)"(", (Object)"(");
    this.delimtrChar.put((Object)")", (Object)")");
    this.delimtrChar.put((Object)"~", (Object)"~");
    this.delimtrChar.put((Object)"`", (Object)"`");
    this.delimtrChar.put((Object)"_", (Object)"_");
    this.delimtrChar.put((Object)"=", (Object)"=");
    this.delimtrChar.put((Object)"{", (Object)"{");
    this.delimtrChar.put((Object)"}", (Object)"}");
    this.delimtrChar.put((Object)"|", (Object)"\u0964");
    this.delimtrChar.put((Object)"\"", (Object)"\"");
    this.delimtrChar.put((Object)"<", (Object)"<");
    this.delimtrChar.put((Object)">", (Object)">");
    this.delimtrChar.put((Object)"?", (Object)"?");
    this.delimtrChar.put((Object)"+", (Object)"+");
    this.delimtrChar.put((Object)"-", (Object)"-");
    this.delimtrChar.put((Object)"[", (Object)"[");
    this.delimtrChar.put((Object)"]", (Object)"]");
    this.delimtrChar.put((Object)"\\", (Object)"\\");
    this.delimtrChar.put((Object)";", (Object)";");
    this.delimtrChar.put((Object)"'", (Object)"'");
    this.delimtrChar.put((Object)",", (Object)",");
    this.delimtrChar.put((Object)".", (Object)".");
    this.delimtrChar.put((Object)"/", (Object)"/");
    this.doubleChar.put((Object)"aa", (Object)"\u0906");
    this.doubleChar.put((Object)"ai", (Object)"\u0910");
    this.doubleChar.put((Object)"au", (Object)"\u0914");
    this.doubleChar.put((Object)"ou", (Object)"\u0914");
    this.doubleChar.put((Object)"ee", (Object)"\u0908");
    this.doubleChar.put((Object)"oo", (Object)"\u090a");
    this.doubleChar.put((Object)"aM", (Object)"\u0905\u0902");
    this.doubleChar.put((Object)"aM~", (Object)"\u0905\u0901");
    this.doubleChar.put((Object)"aH", (Object)"\u0905\u0903");
    this.doubleChar.put((Object)"a:", (Object)"\u0905\u0903");
    this.doubleChar.put((Object)"NG", (Object)"\u0919");
    this.doubleChar.put((Object)"OM", (Object)"\u0950");
    this.doubleChar.put((Object)"+~", (Object)"\u5350");
    this.doubleChar.put((Object)"Rs", (Object)"\u20b9");
    this.doubleChar.put((Object)"||", (Object)"\u0965");
    this.doubleChar.put((Object)"NY", (Object)"\u091e");
    this.doubleChar.put((Object)"Gy", (Object)"\u091c\u094d\u091e");
    this.doubleChar.put((Object)"kh", (Object)"\u0916");
    this.doubleChar.put((Object)"gh", (Object)"\u0918");
    this.doubleChar.put((Object)"Ch", (Object)"\u091b");
    this.doubleChar.put((Object)"chh", (Object)"\u091b");
    this.doubleChar.put((Object)"ch", (Object)"\u091a");
    this.doubleChar.put((Object)"th", (Object)"\u0925");
    this.doubleChar.put((Object)"Th", (Object)"\u0920");
    this.doubleChar.put((Object)"dh", (Object)"\u0927");
    this.doubleChar.put((Object)"Dh", (Object)"\u0922");
    this.doubleChar.put((Object)"jh", (Object)"\u091d");
    this.doubleChar.put((Object)"ph", (Object)"\u092b");
    this.doubleChar.put((Object)"bh", (Object)"\u092d");
    this.doubleChar.put((Object)"sh", (Object)"\u0936");
    this.doubleChar.put((Object)"Sh", (Object)"\u0937");
    this.doubleChar.put((Object)"kSh", (Object)"\u0915\u094d\u0937");
    this.doubleChar.put((Object)"Ri", (Object)"\u090b");
    this.doubleChar.put((Object)"RI", (Object)"\u0960");
    this.doubleChar.put((Object)"Li~", (Object)"\u090c");
    this.doubleChar.put((Object)"LI~", (Object)"\u0961");
    this.doubleChar.put((Object)"@@", (Object)"\u0971");
    this.doubleChar.put((Object)"$$", (Object)"\u0952");
    this.matraChar.put((Object)"a", (Object)"\u093e");
    this.matraChar.put((Object)"A", (Object)"\u093e");
    this.matraChar.put((Object)"i", (Object)"\u093f");
    this.matraChar.put((Object)"I", (Object)"\u0940");
    this.matraChar.put((Object)"u", (Object)"\u0941");
    this.matraChar.put((Object)"U", (Object)"\u0942");
    this.matraChar.put((Object)"e", (Object)"\u0947");
    this.matraChar.put((Object)"E", (Object)"\u0945");
    this.matraChar.put((Object)"o", (Object)"\u094b");
    this.matraChar.put((Object)"O", (Object)"\u0949");
    this.matraChar.put((Object)"ai", (Object)"\u0948");
    this.matraChar.put((Object)"au", (Object)"\u094c");
    this.matraChar.put((Object)"ou", (Object)"\u094c");
    this.matraChar.put((Object)"aa", (Object)"\u093e");
    this.matraChar.put((Object)"oo", (Object)"\u0942");
    this.matraChar.put((Object)"ee", (Object)"\u0940");
    this.matraChar.put((Object)"*", (Object)"\u093c");
    this.matraChar.put((Object)"M~", (Object)"\u0901");
    this.matraChar.put((Object)"r", (Object)"\u094d\u0930");
    this.matraChar.put((Object)"R", (Object)"\u0930\u094d");
    this.matraChar.put((Object)"Ri", (Object)"\u0943");
    this.matraChar.put((Object)"RI", (Object)"\u0944");
    this.matraChar.put((Object)"Li~", (Object)"\u0962");
    this.matraChar.put((Object)"LI~", (Object)"\u0963");
}