选取文字的聪明文字联想(Textselection与InputConnection)

选取文字的聪明文字联想(Textselection与InputConnection)

新建一个继承Activity类的TextSelectionActivity,并设置布局文件为:textselection.xml。

在布局文件添加一个EditText和一个Button组件:

 

    <EditText

        android:id="@+id/textselection_edit"

        android:layout_width="match_parent"

        android:layout_height="wrap_content" />

 

    <Button

        android:id="@+id/textselection_btn"

        android:layout_width="match_parent"

        android:layout_height="wrap_content" />

而后在代码中显示实现:

package lyx.feng.second;

 

import java.util.ArrayList;

 

import lyx.feng.simpletextdemo.R;

import android.app.Activity;

import android.app.AlertDialog;

import android.content.DialogInterface;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.inputmethod.EditorInfo;

import android.view.inputmethod.InputConnection;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

 

public class TextSelectionActivity extends Activity implements OnClickListener {

    private EditText edit = null;

    private Button btn = null;

    // 文字联想

    private InputConnection conn = null;

    // 联想的数据

    private String item[] = { "apple", "age", "android", "adapter",

           "arrayadapter", "amount", "bug", "busy", "before", "button",

           "baseadapter", "bundle" };

    // 选择的ItemId

    private int ItemId = 0;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       super.setContentView(R.layout.textselection);

       this.edit = (EditText) super.findViewById(R.id.textselection_edit);

       this.btn = (Button) super.findViewById(R.id.textselection_btn);

       this.btn.setOnClickListener(this);

       this.btn.setText("显示联想的字符串");

       // 取得InputConnection对象

       this.conn = this.edit.onCreateInputConnection(new EditorInfo());

    }

 

    @Override

    public void onClick(View v) {

       // 判断是否有选择文字

       if (conn.getSelectedText(0) != null) {

           // 取得选择的文字

           String selection = this.conn.getSelectedText(0).toString();

           // Toast显示选择的文字

           Toast.makeText(this, "选择的文字:" + selection, Toast.LENGTH_SHORT)

                  .show();

           // 设置下划线

           conn.setComposingRegion(this.edit.getSelectionStart(),

                  this.edit.getSelectionEnd());

           // 新建一个ArrayList保存匹配的字符串

           ArrayList<String> list = new ArrayList<String>();

           for (int i = 0; i < item.length; i++) {

              if (item[i].indexOf(selection) != -1) {

                  list.add(item[i]);

              }

           }

           // 判断List里面是否有数据,有就提示对话框让用户选择,没有Toast提示

           if (list.size() > 0) {

              this.ItemId = 0;

              final String temp[] = new String[list.size()];

              list.toArray(temp);

              AlertDialog.Builder builder = new AlertDialog.Builder(this)

                     .setTitle("请选择")

                     .setSingleChoiceItems(temp, 0,

                            new DialogInterface.OnClickListener() {

 

                                @Override

                                public void onClick(DialogInterface dialog,

                                       int which) {

                                   // 设置ItemId

                                   ItemId = which;

                                }

                            })

                     .setPositiveButton("确定",

                            new DialogInterface.OnClickListener() {

 

                                @Override

                                public void onClick(DialogInterface dialog,

                                       int which) {

                                   // 替换为匹配的字符串

                                    conn.setComposingText(temp[ItemId], 1);

                                   // 结束匹配

                                   conn.finishComposingText();

                                }

                            })

                     .setNegativeButton("取消",

                            new DialogInterface.OnClickListener() {

 

                                @Override

                                public void onClick(DialogInterface dialog,

                                       int which) {

                                   // 结束匹配

                                   conn.finishComposingText();

                                   // 取消选择

                                   conn.setSelection(0, 0);

                                }

                            });

              builder.create().show();

           } else {

              Toast.makeText(this, "没有匹配的文字", Toast.LENGTH_SHORT).show();

           }

 

       } else {

           Toast.makeText(this, "你还没有选择文字", Toast.LENGTH_SHORT).show();

       }

    }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值