【发布时间】:2011-08-25 22:24:30
【问题描述】:
有什么方法可以使AlertDialog 中的文本在Android 中“可选择”或“可复制”?还是我必须使用其他小部件而不是 AlertDialog?
【问题讨论】:
标签: android android-alertdialog
有什么方法可以使AlertDialog 中的文本在Android 中“可选择”或“可复制”?还是我必须使用其他小部件而不是 AlertDialog?
【问题讨论】:
标签: android android-alertdialog
这可以通过多种方式实现。但首先,要更多地自定义对话框,您需要使用AlertDialog.Builder-class 到create your custom dialog。然后可以由您自己的Views 填充。
以下是归档可选文本的三种方法:
从 API-Level 11 (Android 3.0) 开始,TextView 有一个名为 setTextIsSelectable() 的方法,这看起来很像我通过 hack 获得的结果,详情如下。
// 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();
这个解决方案的问题在于,它只适用于运行 Android 3.0 及更高版本的设备,而其他两个解决方案也适用于 Android 1.5(我使用的是 Android 2.2)。
由于标记文本的目的是(大部分时间)复制它,您可以简单地添加一个onLongClickListener 或一个简单的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();
此示例将复制系统剪贴板中所有显示的文本。
EditText 看起来像TextView
由于没有真正简单的方法可以在 3.0 之前的 Android 版本上选择 TextView 的文本,因此您可以使用 EditText(默认情况下具有可选文本)并使其看起来像 TextView .
这需要一些 XML 布局(以获得 TextView 的样式),但可能会为您提供最原生的外观和感觉:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- Make the EditText look like a TextView -->
<EditText android:id="@+id/showText"
style="?android:attr/textViewStyle"
android:background="@null"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
// Get our tools
AlertDialog dialog;
AlertDialog.Builder builder;
// The EditText to show your Text
LayoutInflater inflater =
(LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.dialog,
(ViewGroup) this.findViewById(R.id.showText));
EditText showText = (EditText) layout.findViewById(R.id.showText);
showText.setText("Some selectable text goes here.");
// Build the Dialog
builder = new AlertDialog.Builder(this);
builder.setView(layout);
dialog = builder.create();
// Some eye-candy
dialog.setTitle("Selectable text");
dialog.setCancelable(true);
dialog.show();
可能会在结果中添加一些自定义和吸引眼球的东西;)
我希望这能让您了解如何完成这项任务。时间不早了,该睡觉了。
【讨论】:
这有点骇人听闻,因为它假定您知道默认的 AlertDialog 布局(请参阅 Where can I find default AlertDialog layout xml file?),但如果您想这样做,可以执行以下操作:
alertDialog.show();
try {
TextView textView = (TextView)dialog.getWindow().getDecorView().findViewById(android.R.id.message);
textView.setTextIsSelectable(true);
}
catch(Exception e) {
// Oups!
}
【讨论】:
您可以在 AlertDialog 中放置一个 EditText。如果您不希望它看起来像其中有一个文本框,您可以将其设置为 AlertDialog 的背景以使其融入其中。
http://developer.android.com/guide/topics/ui/dialogs.html
希望对您有所帮助,如果没有,我可以尝试提供其他解决方案。
【讨论】:
我知道这是一个老问题,但这是我迄今为止使用过的最佳解决方案:
//..... 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);
}
【讨论】: