018、EditText的文字联想

EditText已带有选取字词的功能,所选取的文字可以通过InputConnection.setComposingRegion()方法,将User选取的文字取出加上底线。
EditText可以通过getSelectedStart()和getSelectedEnd()两个方法获取选择用户选择文字的起始字符与终点字符
 
InputConnection.getSelectedText(0),取得EditText中被选取的字符,没有则返回null
 
InputConnection.setComposingText(String,int)将指定参数替换选择的文字,第一个参数为用来替换的字符串,而第二参数为替换后光标的位置,传入0,替换后光标在替换字符串头部,1替换后为字符串结尾,2为字符串再下一个字符位置
 
InputConnection.finishComposingText()取消有底线的文字,即将文字的底线去除
 
EditText.setSelection(int,int)可以设置选择的文字范围
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et = (EditText) findViewById(R.id.et);
        // 取得inputConnection对象
        inputConnection = et.onCreateInputConnection(new EditorInfo());
        findViewById(R.id.bt).setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // 判断用户有没有选择文字
                if (inputConnection.getSelectedText(0) != null) {
                    // 取出用户选择的文字
                    String selectedText = inputConnection.getSelectedText(0)
                            .toString();
                    // 给选择的文字点底线
                    inputConnection.setComposingRegion(et.getSelectionStart(),
                            et.getSelectionEnd());
                    // 文字联想,将联想到的文字存储在集合中使用
                    ArrayList<String> textStrs = new ArrayList<String>();
                    for (String item : items) {
                        if (item.contains(selectedText)) {
                            textStrs.add(item);
                        }
                    }
                    if (textStrs.size() > 0) {
                        // 已经联想到有内容啦,将index清零,index为等会弹出的dialog中的用户选择的替换内容的index
                        index = 0;
                        // 将集合转换为数组
                        final String[] strs = new String[textStrs.size()];
                        textStrs.toArray(strs);
                        // 弹出dialog供用户选择替换的联想文字
                        new AlertDialog.Builder(MainActivity.this)
                                .setTitle("文字联想")
                                .setSingleChoiceItems(strs, 0,
                                        new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(
                                                    DialogInterface dialog,
                                                    int which) {
                                                index = which;
                                            }
                                        })
                                .setPositiveButton("确定",
                                        new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(
                                                    DialogInterface dialog,
                                                    int which) {
                                                //替换选择的文字
                                                inputConnection.setComposingText(strs[index], 1);
                                                //取消选择文字的下划线,不调用该方法,这替换文字后,文字下面依然有下划线
                                                inputConnection.finishComposingText();
                                            }
                                        })
                                .setNegativeButton("取消",
                                        new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(
                                                    DialogInterface dialog,
                                                    int which) {
                                                //取消选择的文字,即取出之前选择文字下划线
                                                inputConnection.finishComposingText();
                                                et.setSelection(0, 0);
                                            }
                                        }).show();
                    } else {
                        // 弹出对话框,提示用户没有联想到提示内容
                        new AlertDialog.Builder(MainActivity.this)
                                .setTitle("文字联想").setMessage("未联想到任何文字哦")
                                .setNeutralButton("确定", null).show();
                    }
                } else {
                    Toast.makeText(MainActivity.this, "您还没有选择文字哦", 0).show();
                }
            }
        });
    }
EditText选取文字时,显示的光标图片,可以自定义,在xml文件中定义,代码如下
    <EditText
        android:id="@+id/et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSelectHandle="@drawable/cursor1"
        android:textSelectHandleLeft="@drawable/cursor2"
        android:textSelectHandleRight="@drawable/cursor3" />

 

原文地址:https://www.cnblogs.com/zyh-blog/p/3343646.html