有没有简单的方法可以通过触摸它们从TextView中选择整个单词?此功能在字典应用程序ColorDict中。
我希望能够选择这些词。顶部的ScrollView具有误导性。在对话框中选择"Select a word"之后出现,长按TextView中的某些东西后出现(你可以在底部看到“定理”一词 - 长按“定理”)。
感谢您的建议。
答案 0 :(得分:1)
尝试将此属性设置为TextView
:
android:textIsSelectable="true"
修改强>
private static final int MENU_ITEM_ID = 0x42;
private TextView targetTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
targetTextView = (TextView) findViewById(R.id.target_textview);
targetTextView.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// you can remove the default menu items if you wish
menu.removeItem(android.R.id.selectAll);
menu.removeItem(android.R.id.cut);
menu.removeItem(android.R.id.copy);
return true;
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
/*
* called when the action mode is created. Here you can
* generate action buttons for this action mode.
* */
menu.add(0, MENU_ITEM_ID, 0, "Menu Item").setIcon(android.R.drawable.ic_dialog_alert);
return true;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
// called when the action mode is about to be destroyed
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case MENU_ITEM_ID:
int start = targetTextView.getSelectionStart();
int end = targetTextView.getSelectionEnd();
CharSequence selectedText = targetTextView.getText().subSequence(start, end);
Toast.makeText(MainActivity.this, selectedText, Toast.LENGTH_SHORT).show();
mode.finish();
return true;
default:
break;
}
return false;
}
});
}