Android EditText中的字符映射 [英] character mapping in edittext android

查看:43
本文介绍了Android EditText中的字符映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要使我的edittext像我写字符"g"时一样,它的相关映射自定义字符应像在印地语中这样写,即जी"

I want to make my edittext like when I write character "g" it's related mapping custom character should written like here in Hindi it's "जी"

我认为应该有字符映射,但是不了解任何人都可以帮助我怎么做

I think there should be character mapping but having no knowledge can anybody help me how to do that

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

Other app https://play.google.com/store/apps/details?id=nichetech.hindi.editor is also doing same like this way, there is option available offline and online

在线是在Google翻译的帮助下完成的,但是如果我选择离线",则写作会以这种方式发生

Online is doing with help of google translator but if I choose Offline then writing happen like this way

在这里您可以看到键盘是英语,但是书写是用印地语完成的

Here you can see that Keyboard is English, but writing is done in Hindi language

谢谢

我可以用英语书写,并且它的相关映射字符将仅以我的应用程序的EditText书写.

Is there way that I write in English and it's related mapping character will written in EditText of my application only.

有人这样做吗,然后请帮助我,怎么做

Does anybody done like this way then please help me, how to do that

推荐答案

要完成您想要的工作,我将创建一个映射到其他字符的字符的HashMap.如果未映射某些特定字符,则将其打印出来.这是我举的一个例子:

To accomplish what you're after, I would create a HashMap of chars that map to other chars. If some specific char is not mapped just print it out. Here's an example I've put up:

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条目
  • ...

作为一个旁注,我想列举一下您给定应用程序的在线版本"具有的一些限制:

As a side-note I'd like to name a few limitations that your given app's "online version" has:

  • 仅当输入空格,换行或标点符号时才进行转换.
  • 您不能在已转换的单词中添加字母.

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

The apps "offline" version also has a minor bug:

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

这篇关于Android EditText中的字符映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆