我正在使用ActionMode.Callback,但我需要知道文本何时被选中...例如
答案 0 :(得分:2)
我想你可以在这里找到答案:
Android Text Selection Listener
您在这里寻找的关键术语,以帮助您进行研究, 是
ActionMode
,前提是您的目标是蜂窝状或更新状态。API docs(向下滚动到“使用上下文操作模式”) 一旦找到你正在寻找的东西,就做好解释事情 因为,这是他们使用的最大障碍,但基本上是什么 你需要做的就是:
- 将您的
EditText
设置为可选(android:textIsSelectable="true"
或setTextIsSelectable(true);
- 实施
醇>ActionMode.Callback
界面并提供您自己的菜单项。注意:如上所述,这仅适用于API级别11+。如果 您要定位早期平台,获取文本事件 选择要复杂得多。
答案 1 :(得分:1)
in .xml:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textIsSelectable="true" />
<。>在.class中:
textview.setCustomSelectionActionModeCallback(new callback(textview));
...
public class callback implements Callback {
private TextView mTextView;
public callback(TextView text) {
this.mTextView = text;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
int start = mTextView.getSelectionStart();
int end = mTextView.getSelectionEnd();
Spannable wordtoSpan = (Spannable) mTextView.getText();
switch (item.getItemId()) {
case R.id.item_blue:
wordtoSpan.setSpan(new BackgroundColorSpan(Color.BLUE), start
, end,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
singlenton.getInstance().getDatabase().createMarkText(mTextView,Color.BLUE);
return true;
case R.id.item_green:
wordtoSpan.setSpan(new BackgroundColorSpan(Color.GREEN), start, end,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
singlenton.getInstance().getDatabase().createMarkText(mTextView,Color.GREEN);
return true;
case R.id.item_red:
wordtoSpan.setSpan(new BackgroundColorSpan(Color.RED), start, end,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
singlenton.getInstance().getDatabase().createMarkText(mTextView,Color.RED);
return true;
case R.id.item_yellow:
wordtoSpan.setSpan(new BackgroundColorSpan(Color.YELLOW), start, end,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
singlenton.getInstance().getDatabase().createMarkText(mTextView,Color.YELLOW);
return true;
case R.id.item_erase:
wordtoSpan.setSpan(new BackgroundColorSpan(Color.TRANSPARENT), start, end,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
singlenton.getInstance().getDatabase().createMarkText(mTextView,Color.TRANSPARENT);
return true;
}
return false;
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.setTitle("Selecione a cor");
mode.getMenuInflater().inflate(R.menu.menu_text_context, menu);
return true;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
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;
}
}
答案 2 :(得分:0)
如果您需要不带ActionMode的监听器:
import android.content.Context
import android.util.AttributeSet
import android.widget.TextView
class TextViewWithObservableSelection @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0,
defStyleRes: Int = 0
) : TextView(
context,
attrs,
defStyleAttr,
defStyleRes
) {
private var selectedText: String = ""
set(value) {
if (field != value) {
field = value
observer?.invoke(value)
}
}
private var observer: ((String) -> Unit)? = null
set(value) {
field = value
field?.invoke(selectedText)
}
override fun onSelectionChanged(selStart: Int, selEnd: Int) {
super.onSelectionChanged(selStart, selEnd)
val startIndex = minOf(selStart, selEnd)
val endIndex = maxOf(selStart, selEnd)
selectedText = text.toString().substring(startIndex, endIndex)
}
fun observeSelectedText(observer: ((String) -> Unit)?) {
this.observer = observer
}
}