gpt4 book ai didi

android - 在android中将文本转换为表情符号

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:44:49 25 4
gpt4 key购买 nike

HI in my app i use this class to change my text to emoticon .

public class MainActivity extends Activity {

private static final Factory spannableFactory = Spannable.Factory
.getInstance();

private static final Map<Pattern, Integer> emoticons = new HashMap<Pattern, Integer>();

static {
addPattern(emoticons, ":)", R.drawable.ic_launcher);
addPattern(emoticons, ":-)", R.drawable.ic_launcher);
// ...
}

private static void addPattern(Map<Pattern, Integer> map, String smile,
int resource) {
map.put(Pattern.compile(Pattern.quote(smile)), resource);
}

public static boolean addSmiles(Context context, Spannable spannable) {
boolean hasChanges = false;
for (Entry<Pattern, Integer> entry : emoticons.entrySet()) {
Matcher matcher = entry.getKey().matcher(spannable);
while (matcher.find()) {
boolean set = true;
for (ImageSpan span : spannable.getSpans(matcher.start(),
matcher.end(), ImageSpan.class))
if (spannable.getSpanStart(span) >= matcher.start()
&& spannable.getSpanEnd(span) <= matcher.end())
spannable.removeSpan(span);
else {
set = false;
break;
}
if (set) {
hasChanges = true;
spannable.setSpan(new ImageSpan(context, entry.getValue()),
matcher.start(), matcher.end(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
return hasChanges;
}

public static Spannable getSmiledText(Context context, CharSequence text) {
Spannable spannable = spannableFactory.newSpannable(text);
addSmiles(context, spannable);
return spannable;
}

OnClickListener listener1 = new OnClickListener() {
@Override
public void onClick(View v) {

EditText tx = (EditText) findViewById(R.id.editText1);
tx.getText().insert(tx.getSelectionStart(), getSmiledText(getBaseContext(), ":-)"));

}};




@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);




Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(listener1);


}

Q1 : i clicked on this Button and show emoticon in edittext . now i have 400 emoticon , i should create 400 Button? or this is not best way ?

Q2 : in app when i clicked on EditText , SelectionStart() is 1 or 0 and begin typing from first line . how to change selectionstart to everywhere user touched in edittext like line 3 of edittext ?

最佳答案

使用内置库

https://github.com/ankushsachdeva/emojicon

https://github.com/rockerhieu/emojicon

或创建自定义输入法

https://github.com/AnySoftKeyboard/AnySoftKeyboard

如果您在创建自定义软键盘时感到困惑,请自由选择

关于android - 在android中将文本转换为表情符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25699324/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com