【发布时间】:2019-04-03 15:40:36
【问题描述】:
我正在使用 RecycleView 开发一个聊天应用程序,我设法使消息上的链接可点击,但不是我想要的方式。
这是我的听众:
recyclerView.addOnItemTouchListener(new Chat_LVAdapter.RecyclerClickListener(this, recyclerView, new ClickListener() {
@Override
public void onClick(View view, final int position) {
Toast.makeText(Chat.this, "CLICK!", Toast.LENGTH_SHORT).show();
TextView textView = view.findViewById(R.id.textView_chati_message);
textView.setLinksClickable(true);
Linkify.addLinks(textView, Linkify.WEB_URLS);
textView.setMovementMethod(LinkMovementMethod.getInstance());
/*Intent intent = new Intent(Chat.this, WebViewer.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("url_web", extractUrls(textView.getText().toString()));
startActivity(intent);*/
}
@Override
public void onLongClick(View view, int position) {
TextView textView = view.findViewById(R.id.textView_chati_message);
String msg = textView.getText().toString();
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
if(clipboard != null){
ClipData clip = ClipData.newPlainText("msg", msg);
clipboard.setPrimaryClip(clip);
}
Toast.makeText(Chat.this, R.string.texto_copiado, Toast.LENGTH_LONG).show();
}
}));
这是 TextView xml:
<TextView
android:id="@+id/textView_chati_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="web"
android:focusable="true"
android:clickable="true"
android:textColor="#000"
android:textSize="16sp" />
问题来自onClick 方法。我可以看到Toast,所以该方法有效,但无法打开链接。它仅在我双击或长按时才有效。如果我使用注释代码,使用Intent 启动WebView,它也可以工作。但我希望能够将Linkify 用于电子邮件等。
我错过了什么吗?为什么Toast 有效,而Linkify 无效?
【问题讨论】:
标签: android url textview listener linkify