[英]How to make a TextView clickable in android
I am trying to make text in TextView
clickable (allow copy to clipboard) and links also clickable but without any success.我正在尝试使
TextView
中的文本可单击(允许复制到剪贴板)并且链接也可单击但没有任何成功。
Here is my MainActivity.xml code:这是我的MainActivity.xml代码:
android:enabled="true"
android:textIsSelectable="true"
android:focusable="true"
android:longClickable="true"
android:linksClickable="true"
Here is the MainActivity Kotlin file:这是MainActivity Kotlin 文件:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Any help would be appreciated.任何帮助,将不胜感激。
just add onclicklistner to your textview.只需将 onclicklistner 添加到您的文本视图中。
For your reference供你参考
textView.setOnClickListener((View view) -> {
//Your code
});
Use the following code so when you click on a TextView, the text will be copied to the clipboard :使用以下代码,以便当您单击 TextView 时,文本将被复制到剪贴板:
myTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("copy", myTextView.getText().toString());
clipboard.setPrimaryClip(clip);
Toast.makeText(MainActivity.this, "Copied To Clipboard", Toast.LENGTH_SHORT).show();
}
});
Try this尝试这个
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/no"
style="?android:attr/borderlessButtonStyle"
/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.