Android Studio:如何在一行中从 TextView 或 EditText 复制文本

Android Studio : How to copy a text from TextView or EditText in a single Line

我正在制作一个加密文本并自动复制的应用程序。

这是 XML :

  <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <EditText
            android:id="@+id/txt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Enter Text To Encrypt or To Decrypt"
            android:inputType="text" />

        <EditText
            android:id="@+id/txtkey"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Enter KEY"
            android:inputType="text" />

        <EditText
            android:id="@+id/txtsalt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Enter SALT"
            android:inputType="text" />

        <EditText
            android:id="@+id/txtres"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Result"
            android:clickable="false"
            android:inputType="none"
            android:singleLine="true" />

        <Button
            android:id="@+id/btnencrypt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Encrypt" />

        <Button
            android:id="@+id/btndecrypt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Decrypt" />

        <Button
            android:id="@+id/btnclear"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Clear" />

    </LinearLayout>

这里是Btnencrypt代码,它加密+复制到密文

        btnencrypt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String encrypted = Encrypt(txt.getText().toString(), txtkey.getText().toString(),txtsalt.getText().toString());
            txtres.setText(encrypted);
            ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
            ClipData clip = ClipData.newPlainText("label", txtres.getText().toString().trim());
            clipboard.setPrimaryClip(clip);
            Toast.makeText(getApplicationContext(), "Encrypted Text Copied", Toast.LENGTH_SHORT).show();
            txt.setText("");
        }
    });

当我将加密文本传递到 Notepad++ 时,它采用一种奇怪的格式:2 行文本和一个空行。我想要的是将整个文本放在一行中,请参见下图

http://hpics.li/e355430

编辑:

通过添加 .trim() ,无用的行被删除但我仍然有 2 行文本而不是一行

btnencrypt.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        String encrypted = Encrypt(txt.getText().toString().trim(), txtkey.getText().toString().trim(),txtsalt.getText().toString().trim());
        txtres.setText(encrypted.trim());
        ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
        ClipData clip = ClipData.newPlainText("label", txtres.getText().toString());
        clipboard.setPrimaryClip(clip);
        Toast.makeText(getApplicationContext(), "Encrypted Text Copied", Toast.LENGTH_SHORT).show();
        txt.setText("");
    }
});

当您尝试从对象中获取文本时必须使用 trim()...

 txtres.setText(Decrypt(txt.getText().toString().trim(), txtkey.getText().toString().trim(),txtsalt.getText().toString().trim()));
        ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
        ClipData clip = ClipData.newPlainText("label", txtres.getText().toString().trim());
        clipboard.setPrimaryClip(clip);

能不能简单去掉格式?

ClipData.newPlainText("label", textView.getText().toString().replaceAll("\n", "").trim());

将以下参数添加到 textView 检查下面的代码

    String encrypted = Encrypt(txt.getText().toString(), txtkey.getText().toString(),txtsalt.getText().toString());
    txtres.setEllipsize(TextUtils.TruncateAt.MARQUEE);
    txtres.setSingleLine(true);
    txtres.setMaxLines(1);
    txtres.setLines(1);
    txtres.setMarqueeRepeatLimit(-1); // marquee forever
    txtres.setHorizontalFadingEdgeEnabled(true);
    txtres.setHorizontallyScrolling(true);
    txtres.setFocusable(true);
    txtres.setFocusableInTouchMode(true);
    txtres.setSelected(true);
    txtres.setLayoutParams(params);
    txtres.setTextSize(30);
    txtres.setText(encrypted.trim());
    ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
    ClipData clip = ClipData.newPlainText("label", txtres.getText().toString());
    clipboard.setPrimaryClip(clip);
    Toast.makeText(getApplicationContext(), "Encrypted Text Copied", Toast.LENGTH_SHORT).show();
    txt.setText("");