机器人如何获得所选单词EDITTEXT? [英] Android How to get selected word in Edittext?

查看:121
本文介绍了机器人如何获得所选单词EDITTEXT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发类似记事本的应用程序中,我要动态地更改选定文本格式(颜色,改变字体的风格,粗体,斜体,下划线等) 我如何格式化一个特定的词?

I am developing an app like Notepad in which I want to change the selected text formatting dynamically (colors, changing font styles, bold, italic, underline etc.) How can I format a specific word?

推荐答案

您可以使用所选择的字 getSelectionStart() getSelectionEnd() 方法:

You can get the selected word using getSelectionStart() and getSelectionEnd() method :

EditText etx=(EditText)findViewById(R.id.editext);

int startSelection=etx.getSelectionStart();
int endSelection=etx.getSelectionEnd();

String selectedText = etx.getText().substring(startSelection, endSelection);


然后,您可以使用此选择在满弦把它给 SpannableStringBuilder 上的一个按钮点击/其它事件后申请的具体格式:


Then you can apply your specific formatting by using this selected substring in the full string after taking it to a SpannableStringBuilder on a button click/some other event:

  int startSelection=etx.getSelectionStart();
  int endSelection=etx.getSelectionEnd();

                  final SpannableStringBuilder sb = new SpannableStringBuilder(etx.getText().toString());

                        final StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD); // Span to make text bold
                        final StyleSpan iss = new StyleSpan(android.graphics.Typeface.ITALIC); // Span to make text italic                                     
                        sb.setSpan(iss, startSelection, endSelection, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                        sb.setSpan(bss, startSelection, endSelection, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                        etx.setText(sb);    


<一个href="http://stackoverflow.com/questions/4897349/android-coloring-part-of-a-string-using-textview-settext">Reference.

这篇关于机器人如何获得所选单词EDITTEXT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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