您的位置:首页 > 移动开发 > Android开发

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

2016-03-12 21:04 656 查看

选取文字的聪明文字联想(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());
    }
 
    @Ove
4000
rride
    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();
       }
    }
}
 

 

 

 

 

 

 

 

 

 

 

 

 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息