จะแสดงสัญลักษณ์เครื่องหมายคำพูดคู่ (") ใน TextView ได้อย่างไร?


97

ฉันกำลังพยายามแสดงคำบางคำในเครื่องหมายอัญประกาศคู่ในมุมมองข้อความในไฟล์ xml แต่มันไม่ทำงาน กรุณาช่วยฉัน

    <TextView 
    style="@style/TextStyle" 
    android:text="message "quote string 1" and "quote string 2" end message" 
    android:id="@+id/lblAboutPara3" 
    android:autoLink="web"/>    

มีใครรู้วิธีแก้ปัญหานี้บ้าง .............

คำตอบ:


193

ในการstrings.xmlหลีกเลี่ยงอักขระพิเศษ (เช่นเครื่องหมายอัญประกาศคู่) ด้วยแบ็กสแลช:

"message \"quote string 1\" and \"quote string 2\" end message"

แต่ในมุมมอง xml (เช่นlayout.xml) คุณต้องใช้เอนทิตีอักขระ HTML (เช่น&quot;):

"message &quot;quote string 1&quot; and &quot;quote string 2&quot; end message"

ดูข้อมูลเพิ่มเติมได้ที่http://developer.android.com/guide/topics/resources/string-resource.html


6
ใช้งานได้ใน strings.xml <string name = "double_quote"> \ "</string> แต่ไม่ใช่ใน layout.xml
Tyler Davis

คำตอบนี้ไม่ถูกต้องเนื่องจาก OP ถามเฉพาะเกี่ยวกับ "เครื่องหมายคำพูดคู่ในมุมมองข้อความในไฟล์ xml" ไม่ใช่ใน strings.xml
Patrick

71

ใช้&quot;สัญลักษณ์เพื่อแก้ปัญหาฮาร์ดโค้ดนี้ :)

android:text="message &quot;quote string 1&quot;" 

1
นี้จะต้องเป็น ans ที่ถูกต้องถ้าคุณต้องการ"สัญลักษณ์ในไฟล์รูปแบบโดยไม่ต้องใช้string.xml .
Jaydipsinh Zala

1
ความจริงที่ว่าคุณไม่สามารถใช้สิ่งนี้strings.xmlได้เป็นปัญหาใหญ่
Mike Baxter

14

ใช้escape characters. เพื่อแสดงการใช้เครื่องหมายคำพูดคู่\"

รหัสของคุณจะเป็น

android:text="message \"quote string 1\" and "quote string 2\" end message" 

10

โปรดลอง

<TextView 
style="@style/TextStyle" 
android:text='message \"quote string 1\" and \"quote string 2\" end message' 
android:id="@+id/lblAboutPara3" 
android:autoLink="web"/> 




2

หากคุณมีเครื่องหมายอัญประกาศคู่ในสตริงของคุณคุณต้องหลีกเลี่ยง(\ ") การล้อมสตริงด้วยเครื่องหมายคำพูดเดี่ยวจะไม่ทำงาน

ใน strings.xml

<string name="good_example">This is a \"good string\".</string>

ที่มา : http://developer.android.com/guide/topics/resources/string-resource.html


1

ใช้เครื่องหมายคำพูดเดี่ยวเพื่อปิดข้อความและคุณสามารถใช้เครื่องหมายคำพูดคู่ได้มากเท่าที่คุณต้องการภายในสตริง

android:text='message "quote string 1" and "quote string 2" end message'
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.