有什么方法可以在AlertDialog
中制作文本吗? Android 中的“可选”或“可复制”?还是我必须使用其他小部件而不是 AlertDialog
?
最佳答案
这可以通过多种方式实现。但首先,要更多地自定义对话框,您需要使用 AlertDialog.Builder
-class至 create your custom dialog .这可以由您自己的 View
填充。
以下是三种存档可选文本的方法:
使用 Android 3.0 +
从 API 级别 11 (Android 3.0) 开始,TextView
有一个名为 setTextIsSelectable()
的方法,这看起来很像我通过如下详述的黑客攻击所取得的成就。
// 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
(您喜欢的那个most) 到 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 部分:
<?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>
Java 部分:
// 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();
一些自定义和视觉效果可能会添加到结果中;)
我希望这能让您了解如何完成这项任务。时间不早了,该 sleep 了。
关于android - 从 Android AlertDialog 复制文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7197939/