character mapping in edittext android

后端 未结 4 1830
猫巷女王i
猫巷女王i 2021-02-05 22:48

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

相关标签:
4条回答
  • 2021-02-05 22:50

    I guess you are better off looking at creating your own *.ttf file with all character mappings. You can just set it on your EditText (or whatever text widget you are using). TTF fonts are welcome in assets/ directory (or any directory you can read from). You can create Typeface object with:

    Typeface.createFromAsset(AssetManager mgr, String path)

    and setting it with:

    textView.setTypeface(mTypeface);
    
    0 讨论(0)
  • 2021-02-05 23:00
    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");
    }
    
    0 讨论(0)
  • 2021-02-05 23:01

    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) {
    
        }
    });
    

    Pros:

    • Ignores case when looking for a replacement. (q = Q = 1)
    • Replaces immediately single and multiple chars
    • Doesn't loop the whole string
    • Can replace in the middle of another string

    Cons:

    • You have to have a HashMap entry for every character you want replaced
    • ...

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

    • The converting is done only when a space, new line or punctuation mark is entered.
    • You cannot add letters to already converted words.

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

    • It doesn't convert words that are copied in or written with Swipe
    0 讨论(0)
  • 2021-02-05 23:09

    Key character map files (.kcm files) are responsible for mapping combinations of Android key codes with modifiers to Unicode characters.Device-specific key layout files are required for all internal (built-in) input devices that have keys, if only to tell the system that the device is special purpose only (not a full keyboard).

    Device-specific key layout files are optional for external keyboards, and often aren't needed at all. The system provides a generic key character map that is suitable for many external keyboards.

    If no device-specific key layout file is available, then the system will choose a default instead.

    Key character map files are located by USB vendor, product (and optionally version) id or by input device name.

    Key character map files are located by USB vendor, product (and optionally version) id or by input device name.

    Suppose the user pressed A and SHIFT together. The system first locates the set of properties and behaviors associated with KEYCODE_A.

    key A { label: 'A' base: 'a' shift, capslock: 'A' ctrl, alt, meta: none }

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

    key A { label: 'A' number: '2' base: 'a' shift, capslock: 'A' alt: '#' shift+alt, capslock+alt: none }

    key SPACE { label: ' ' number: ' ' base: ' ' shift: ' ' alt: '\uef01' shift+alt: '\uef01' }

    0 讨论(0)
提交回复
热议问题