android - 从 Android AlertDialog 复制文本?

标签 android android-alertdialog

有什么方法可以在AlertDialog 中制作文本吗? Android 中的“可选”或“可复制”?还是我必须使用其他小部件而不是 AlertDialog

最佳答案

这可以通过多种方式实现。但首先,要更多地自定义对话框,您需要使用 AlertDialog.Builder-classcreate 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/

相关文章:

android - 在 Flex 中读取 Android 联系人?

java - Android MVVM/Repository 如何强制 LiveData 从存储库更新?

android - 无法访问来自不同类的值

android - 如何获取android Ice Cream Sandwich 的警报对话框主题?

android - 未显示警报对话标题和消息

java - 如何使用 Retrofit android 设置工具栏标题?

android - 改造不会在断点处停止

android - 将 gradle 设置为 Android O (API 26)

android - AlertDialog 自定义标题有黑色边框

android - 在 ListViewItem 的按钮单击中创建 AlertDialog 时,如何从 AlertDialog 的单击处理程序获取 ListViewItem 的 ViewHolder?