在Android的可点击的链接和复制/粘贴菜单EditView中 [英] Clickable links and copy/paste menu in EditView in android
问题描述
我有一个的EditText
查看我的Android应用程序。我需要的是内在联系,这意味着我需要在的EditText
某些按钮或跨度和的onClick
来这个按钮我可以做一些动作(而不是重定向到网页)。
我意识到这个按钮与 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());
在这种方法的链接将不会工作,但显示正常longClick菜单。
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中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!