gpt4 book ai didi

android - 如何在 TextView 中的选定文本上设置监听器

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:06:00 25 4
gpt4 key购买 nike

我正在使用 ActionMode.Callback 但我需要知道文本何时完成选择...例如

enter image description here

最佳答案

在 .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;
}

}

关于android - 如何在 TextView 中的选定文本上设置监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15091019/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com