得到的TextView选定的文本 [英] Get Selected Text from TextView
问题描述
我试图让一个TextView用户选择的文本,
我wan't使用安卓textIsSelectable =真正的
来让我的用户拷贝/粘贴操作
不过,我没有一个线索后如何显示操作栏菜单中获取文本,我们的目标是实现一个谷歌图书样的行为:你选择一个单词,它为您提供了一个定义。
我想你要找的是 TextView.setCustomSelectionActionModeCallback
。这将允许你创建自己的 ActionMode.Callback
当选择了文本。然后你可以使用 TextView.getSelectionStart
和 TextView.getSelectionEnd
检索选中的文本,当你的菜单项
选择。这里有一个简单的例子:
mTextView.setCustomSelectionActionModeCallback(新的回调(){
@覆盖
在prepareActionMode公共布尔(ActionMode模式,菜单菜单){
//去掉全选选项
menu.removeItem(android.R.id.selectAll);
//删除了腰斩选项
menu.removeItem(android.R.id.cut);
//去掉复制全部选项
menu.removeItem(android.R.id.copy);
返回true;
}
@覆盖
公共布尔onCreateActionMode(ActionMode模式,菜单菜单){
//调用时,第一个创建的动作模式。菜单提供
//将用于产生用于操作模式操作按钮
//下面是一个例子菜单项
menu.add(0,分辨率,0,定义)的setIcon(R.drawable.ic_action_book)。
返回true;
}
@覆盖
公共无效onDestroyActionMode(ActionMode模式){
//当一个操作模式将要退出打了个电话,
//销毁
}
@覆盖
公共布尔onActionItemClicked(ActionMode模式,菜单项项){
开关(item.getItemId()){
病例定义:
INT分钟= 0;
。INT最大= mTextView.getText()长度();
如果(mTextView.isFocused()){
最终诠释SelStart的= mTextView.getSelectionStart();
最终诠释selEnd = mTextView.getSelectionEnd();
分= Math.max(0,Math.min(SelStart的,selEnd));
最大= Math.max(0,Math.max(SelStart的,selEnd));
}
//与所选文本执行的定义查询
最后的CharSequence selectedText = mTextView.getText()子进程(最小值,最大值)。
//完成并关闭ActionMode
mode.finish();
返回true;
默认:
打破;
}
返回false;
}
});
结果
I'm trying to get the text the user selected in a TextView,
i wan't to use the android:textIsSelectable="true"
to allow my user copy/paste actions
However I don't have a clue as how to get the text once the action bar menu is displayed, the goal is to implement a Google book like behavior : you select a word and it gives you a definition.
I think what you're looking for is TextView.setCustomSelectionActionModeCallback
. This will allow you to create your own ActionMode.Callback
for when the text is selected. Then you can use TextView.getSelectionStart
and TextView.getSelectionEnd
to retrieve the selected text when your MenuItem
is selected. Here's a quick example:
mTextView.setCustomSelectionActionModeCallback(new Callback() {
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// Remove the "select all" option
menu.removeItem(android.R.id.selectAll);
// Remove the "cut" option
menu.removeItem(android.R.id.cut);
// Remove the "copy all" option
menu.removeItem(android.R.id.copy);
return true;
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Called when action mode is first created. The menu supplied
// will be used to generate action buttons for the action mode
// Here is an example MenuItem
menu.add(0, DEFINITION, 0, "Definition").setIcon(R.drawable.ic_action_book);
return true;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
// Called when an action mode is about to be exited and
// destroyed
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case DEFINITION:
int min = 0;
int max = mTextView.getText().length();
if (mTextView.isFocused()) {
final int selStart = mTextView.getSelectionStart();
final int selEnd = mTextView.getSelectionEnd();
min = Math.max(0, Math.min(selStart, selEnd));
max = Math.max(0, Math.max(selStart, selEnd));
}
// Perform your definition lookup with the selected text
final CharSequence selectedText = mTextView.getText().subSequence(min, max);
// Finish and close the ActionMode
mode.finish();
return true;
default:
break;
}
return false;
}
});
Results
这篇关于得到的TextView选定的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!