问题描述
我的 Android 应用中有一个 EditText
视图.我需要其中的内部链接",这意味着我需要在 EditText
中使用一些按钮或跨度,并且使用 onClick
到此按钮我可以执行一些操作(而不是重定向到 Web页).我用这样的 ClickableSpan()
实现了这个按钮
I have an EditText
view in my Android app. I need "inner links" in it, this means that I need some buttons or span inside EditText
and with onClick
to this button I can do some actions (not redirect to web page).I realized this buttons with ClickableSpan()
like this
linkWord = "my link";
link = new SpannableString(linkWord);
cs = new ClickableSpan(){
private String w = linkWord;
@Override
public void onClick(View widget) {
wrd.setText(w);
}
};
link.setSpan(cs, 0, linkWord.length(), 0);
et.append(link);
为了使这个跨度可点击,我使用了
For make this span clickable I used
et.setMovementMethod(LinkMovementMethod.getInstance());
内部链接"工作正常,但在使用 et.setMovementMethod()
后,在 OnLongClick
菜单上禁用复制和粘贴项目.这是一个问题,因为我需要 EditText
中的链接"并同时从该视图中复制文本.
"Inner links" works fine, but after using et.setMovementMethod()
copy and paste items are disable on OnLongClick
menu. And this is a problem, because I need "links" in EditText
and copy text from this view in the same time.
我有想法在侦听器 OnLongClickListener
中设置类似 removeMovementMethod()
的东西,用于临时禁用链接"功能并使用带有复制/粘贴的菜单,并在处理文本后打开再次 setMovementMethod()
方法.但我不知道如何实现这一点.
I have idea to set in listener OnLongClickListener
something like removeMovementMethod()
for temporary disable "links" function and use menu with copy/paste and after coping text switch on setMovementMethod()
method again. But I don't know how to realize this.
你能帮我吗?您可能还有其他一些方法...
Can you help me? You may be there are some another ways...
谢谢!
推荐答案
我解决了这个问题,也许这对某人来说会很有趣...
I solved this problem and may be this will be interesting for someone...
对于我使用的 EditText 中的可点击链接
For clickable links inside EditText I used
et.setMovementMethod(LinkMovementMethod.getInstance());
在这种情况下,在 longClick 菜单中没有复制/粘贴项目.为了激活它们,我需要回到正常的 EditText 状态,我可以这样做:
in this case in longClick menu there are not copy/paste items.For activate them I need back to normal EditText state, I can do it with:
et.setMovementMethod(ArrowKeyMovementMethod.getInstance());
此方法后链接将不起作用但出现正常的长按菜单.
After this method links will not work but appear normal longClick menu.
因此我在上下文菜单中添加了新项目并在这两个选项之间切换:
Therefore I added new item to the context menu and switched between this two options:
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
if(et.getSelectionStart() == -1){ // in case of setMovementMethod(LinkMovementMethod.getInstance())
menu.add(0, 1, 0, "Enable copy");
}
else{
menu.add(0, 2, 0, "Enable links");
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1:
et.setMovementMethod(ArrowKeyMovementMethod.getInstance());
et.setSelection(0, 0);
//re-register EditText for context menu:
unregisterForContextMenu(et);
registerForContextMenu(et);
break;
case 2:
et.setMovementMethod(LinkMovementMethod.getInstance());
break;
}
return true;
}
我还为上下文菜单注册了 EditText:
Also I registered EditText for context menu:
registerForContextMenu(et);
希望这会对某人有所帮助!
Have a hope that this will help someone!
这篇关于android中EditView中的可点击链接和复制/粘贴菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!