从Android AlertDialog复制文本?
有没有办法让文本在安卓系统中“可选择”或“可复制”呢?或者我必须使用其他小部件而不是从Android AlertDialog复制文本?,android,android-alertdialog,Android,Android Alertdialog,有没有办法让文本在安卓系统中“可选择”或“可复制”呢?或者我必须使用其他小部件而不是AlertDialog?您可以在AlertDialog中放置编辑文本。如果你不想让它看起来像是一个文本框,你可以把它设计成AlertDialog的背景,让它融入其中 希望这有帮助,如果没有,我可以尝试提供其他解决方法。这可以通过几种方式实现。但首先,要进一步自定义对话框,需要使用to。然后,您可以使用自己的视图来填充它 以下是三种归档可选文本的方法: 使用安卓3.0+ 自API Level 11(Android
AlertDialog
?您可以在AlertDialog中放置编辑文本。如果你不想让它看起来像是一个文本框,你可以把它设计成AlertDialog的背景,让它融入其中
希望这有帮助,如果没有,我可以尝试提供其他解决方法。这可以通过几种方式实现。但首先,要进一步自定义对话框,需要使用to。然后,您可以使用自己的
视图来填充它
以下是三种归档可选文本的方法:
使用安卓3.0+
自API Level 11(Android 3.0)以来,TextView
有一个名为的方法,它与我通过黑客攻击实现的方法非常相似,如下所述
// The TextView to show your Text
TextView showText = new TextView(this);
showText.setText("Some selectable text goes here.");
showText.setTextIsSelectable(true);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// Build the Dialog
builder.setView(showText)
.setTitle("Selectable text")
.setCancelable(true)
.show();
这个解决方案的问题是,它只适用于运行安卓3.0及更高版本的设备,而其他两个解决方案也适用于安卓1.5(我使用的是安卓2.2)
复制到剪贴板
由于标记文本的目的(大多数情况下)是复制文本,您只需将onlongclickliester
或简单的onClickListener
(您最喜欢的一个)添加到TextView
,并将其显示的文本复制到系统的剪贴板
这方面的小例子:
// Get our tools
AlertDialog dialog;
AlertDialog.Builder builder;
// The TextView to show your Text
TextView showText = new TextView(this);
showText.setText("Some selectable text goes here.");
// Add the Listener
showText.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// Copy the Text to the clipboard
ClipboardManager manager =
(ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
TextView showTextParam = (TextView) v;
manager.setText( showTextParam.getText() );
// Show a message:
Toast.makeText(v.getContext(), "Text in clipboard",
Toast.LENGTH_SHORT)
.show();
return true;
}
});
// Build the Dialog
builder = new AlertDialog.Builder(this);
builder.setView(showText);
dialog = builder.create();
// Some eye-candy
dialog.setTitle("Selectable text");
dialog.setCancelable(true);
dialog.show();
此示例将复制系统剪贴板中显示的所有文本
使编辑文本
看起来像文本视图
由于在3.0之前的Android版本上没有真正简单的方法可以选择TextView
的文本,因此您可以使用EditText
(默认情况下具有可选择的文本)并使其看起来像TextView
这需要一些XML布局(以获得TextView
的样式),但可能会让您获得最自然的外观:
XML部分:
可能会在结果中添加一些自定义设置和吸引眼球的内容;)
我希望这能让你了解如何完成这项任务。现在已经很晚了,该睡觉了。这有点不妥,因为它假设您知道默认的AlertDialog布局(请参阅),但如果您想这样做,可以执行以下操作:
alertDialog.show();
try {
TextView textView = (TextView)dialog.getWindow().getDecorView().findViewById(android.R.id.message);
textView.setTextIsSelectable(true);
}
catch(Exception e) {
// Oups!
}
我知道这是一个老生常谈的问题,但这里是我迄今为止使用的最好的解决方案:
//..... creating the AlertDialog and showing it.
dialog.show();
// after showing it, we fetch the message TextView container
View messageView = dialog.findViewById(android.R.id.message);
if (messageView instanceof TextView) {
((TextView) messageView).setTextIsSelectable(true);
}
alertDialog.show();
try {
TextView textView = (TextView)dialog.getWindow().getDecorView().findViewById(android.R.id.message);
textView.setTextIsSelectable(true);
}
catch(Exception e) {
// Oups!
}
//..... creating the AlertDialog and showing it.
dialog.show();
// after showing it, we fetch the message TextView container
View messageView = dialog.findViewById(android.R.id.message);
if (messageView instanceof TextView) {
((TextView) messageView).setTextIsSelectable(true);
}