70

これはばかげた質問のように思えるかもしれませんが、私は検索していて、答えを見つけることができません。

私はAndroidアプリケーションを作成していて、その中にいくつかの情報を電子メールで送信するボタンがあります。すべてを1つの段落にまとめたくありません。

メール本文のputExtraに対して私のアプリが行っていることは次のとおりです。

I am the first part of the info being emailed. I am the second part. I am the third part.

これが私がしたいことです:

I am the first part of the info being emailed.
I am the second part.

I am the third part.

それを達成するために、どのように新しい行を文字列に入れるか、またはputExtraメソッドを使用しますか?

ありがとうございました。

4

5 に答える 5

100

試す:

String str = "my string \n my other string";

印刷すると、次のようになります。

my string
my other string

于 2011-08-24T14:04:45.410 に答える
89

System.getProperty("line.separator")を使用して新しい行を取得してみてください。

于 2011-06-17T00:13:07.573 に答える
25

個人的には「 \n 」を使用したいと思います。これは、LinuxまたはAndroidで改行を入れるだけです。

例えば、

String str = "I am the first part of the info being emailed.\nI am the second part.\n\nI am the third part.";

出力

I am the first part of the info being emailed.
I am the second part.

I am the third part.

より一般的な方法は、次を使用することです。

System.getProperty("line.separator")

例えば、

String str = "I am the first part of the info being emailed." + System.getProperty("line.separator") + "I am the second part." + System.getProperty("line.separator") + System.getProperty("line.separator") + "I am the third part.";

上記と同じ出力をもたらします。ここでは、クラスの静的getProperty()メソッドを使用して、特定のOSの「」を取得できます。Systemline.seperator

ただし、ここでのOSは固定されているため、これはまったく必要ありません。つまり、Androidです。したがって、毎回メソッドを呼び出すことは、重くて不必要な操作です。

さらに、これによりコードの長さが長くなり、見た目が乱雑になります。「\n」は甘くてシンプルです。

于 2015-07-21T17:32:32.897 に答える
9

タグで使用<br>します。CDATA例として、私のstrings.xmlファイルには次のような項目が含まれています。

<item><![CDATA[<b>My name is John</b><br>Nice to meet you]]></item>

とプリント

My name is John
Nice to meet you
于 2018-03-22T17:54:04.333 に答える
0

実行時に値を取得しているのと同じ文字列から文字列に改行を追加したい場合は、このKotlinコードが機能します。

str = "<br>"+str?.replace("," , "</br><br>")+"</br>"
value = HtmlCompat.fromHtml(${skill_et_1}",Html.FROM_HTML_MODE_LEGACY)
tv.text = value
于 2021-06-15T18:10:02.713 に答える