Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

จะสร้างข้อความการแบ่งหน้าใน Android โดยใช้ Kotlin ได้อย่างไร?


ตัวอย่างนี้สาธิตวิธีสร้างข้อความการแบ่งหน้าใน Android โดยใช้ Kotlin

ขั้นตอนที่ 1 − สร้างโครงการใหม่ใน Android Studio ไปที่ไฟล์ ⇒ โครงการใหม่และกรอกรายละเอียดที่จำเป็นทั้งหมดเพื่อสร้างโครงการใหม่

ขั้นตอนที่ 2 − เพิ่มรหัสต่อไปนี้ใน res/layout/activity_main.xml

  <ปุ่ม android:id="@+id/buttonBack" style="?android:attr/buttonBarButtonStyle" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background ="@android:color/transparent" /> <ปุ่ม android:id="@+id/buttonForward" style="?android:attr/buttonBarButtonStyle" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@android:color/transparent" />  

ขั้นตอนที่ 3 − เพิ่มรหัสต่อไปนี้ใน src/MainActivity.kt

<ก่อนหน้า>นำเข้า android.graphics.Colorimport android.graphics.Typefaceimport android.os.Bundleimport android.text.Htmlimport android.text.Spannableimport android.text.SpannableStringimport android.text.TextUtilsimport android.text.style.ForegroundColorSpanimport android.text style.RelativeSpanimport android.text.style.StyleSpanimport android.view.ViewTreeObserverimport android.widget.Buttonimport android.widget.TextViewimport androidx.appcompat.app.AppCompatActivityclass MainActivity :AppCompatActivity () { ปุ่ม lateinit var ปุ่มย้อนกลับ:ปุ่มสำหรับปุ่ม lateinit latein ส่วนตัว var textView:TextView การแบ่งหน้า var ส่วนตัว:การแบ่งหน้า? =null lateinit ส่วนตัว var charSequence:CharSequence ส่วนตัว var currentIndex =0 แทนที่ความสนุกบน Create (savedInstanceState:Bundle?) { super.onCreate (savedInstanceState) setContentView (R.layout.activity_main) title ="KotlinApp" buttonBack =findViewById (R.id. buttonBack) buttonForward =findViewById(R.id.buttonForward) textView =findViewById(R.id.tv) val htmlString =Html.fromHtml(getString(R.string.html_string)) val spanString =SpannableString(getString(R.string.long_string) )) spanString.setSpan(ForegroundColorSpan(Color.BLUE), 0, 24, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) spanString.setSpan(RelativeSizeSpan(2f), 0, 24, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) spanString.setSpan.styleSpan(สไตล์สแปน) , 0, 24, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) spanString.setSpan(ForegroundColorSpan(Color.BLUE), 536, spanString.length,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) spanString.setSpan(RelativeSizeSpan(2f), 536, spanStri ng.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) spanString.setSpan(StyleSpan(Typeface.MONOSPACE.style), 536, spanString.length,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) charSequence =TextUtils.concat(htmlString, spanString.lengthObserver.Onserver.concat(htmlString, spanLaserview.Observer.Onserver.Concat) .OnGlobalLayoutListener { แทนที่ fun onGlobalLayout () { // การลบตัวฟังเลย์เอาต์เพื่อหลีกเลี่ยงการโทรหลายครั้ง textView.viewTreeObserver.removeOnGlobalLayoutListener (นี้) การแบ่งหน้า =การแบ่งหน้า ( charSequence, textView.width, textView.height, textView.painting, textView. lineSpacingExtra, textView.includeFontPadding ) อัปเดต () } }) buttonBack.setOnClickListener { currentIndex =if ((currentIndex> 0)) currentIndex - 1 อื่น 0 อัปเดต () } buttonForward.setOnClickListene r { currentIndex =if ((currentIndex

ขั้นตอนที่ 4 − สร้างคลาส Kotlin และเพิ่มรหัสต่อไปนี้ -

<ก่อนหน้า>นำเข้า android.graphics.Colorimport android.graphics.Typefaceimport android.os.Bundleimport android.text.Htmlimport android.text.Spannableimport android.text.SpannableStringimport android.text.TextUtilsimport android.text.style.ForegroundColorSpanimport android.text style.RelativeSpanimport android.text.style.StyleSpanimport android.view.ViewTreeObserverimport android.widget.Buttonimport android.widget.TextViewimport androidx.appcompat.app.AppCompatActivityclass MainActivity :AppCompatActivity () { ปุ่ม lateinit var ปุ่มย้อนกลับ:ปุ่มสำหรับปุ่ม lateinit latein ส่วนตัว var textView:TextView การแบ่งหน้า var ส่วนตัว:การแบ่งหน้า? =null lateinit ส่วนตัว var charSequence:CharSequence ส่วนตัว var currentIndex =0 แทนที่ความสนุกบน Create (savedInstanceState:Bundle?) { super.onCreate (savedInstanceState) setContentView (R.layout.activity_main) title ="KotlinApp" buttonBack =findViewById (R.id. buttonBack) buttonForward =findViewById(R.id.buttonForward) textView =findViewById(R.id.tv) val htmlString =Html.fromHtml(getString(R.string.html_string)) val spanString =SpannableString(getString(R.string.long_string) )) spanString.setSpan(ForegroundColorSpan(Color.BLUE), 0, 24, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) spanString.setSpan(RelativeSizeSpan(2f), 0, 24, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) spanString.setSpan.styleSpan(สไตล์สแปน) , 0, 24,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) spanString.setSpan(ForegroundColorSpan(Color.BLUE), 536, spanString.length,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) spanString.setSpan(RelativeSizeSpan(2f), 536, spanStrin g.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) spanString.setSpan(StyleSpan(Typeface.MONOSPACE.style), 536, spanString.length,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) charSequence =TextUtils.concat(htmlString, spanLaserListObserver:Observer.Concat(htmlString, spanLaserListObserver:.OnGlobalLayoutListener { แทนที่ fun onGlobalLayout () { // การลบตัวฟังเลย์เอาต์เพื่อหลีกเลี่ยงการโทรหลายครั้ง textView.viewTreeObserver.removeOnGlobalLayoutListener (นี้) การแบ่งหน้า =การแบ่งหน้า ( charSequence, textView.width, textView.height, textView.painting, textView. lineSpacingExtra, textView.includeFontPadding ) อัปเดต () } }) buttonBack.setOnClickListener { currentIndex =if ((currentIndex> 0)) currentIndex - 1 อื่น 0 อัปเดต () } buttonForward.setOnClickListener { currentIndex =if ((currentIndex

ขั้นตอนที่ 5 − เพิ่มรหัสต่อไปนี้ใน androidManifest.xml

     <หมวดหมู่ android:name=" android.intent.category.LAUNCHER" />   

มาลองเรียกใช้แอปพลิเคชันของคุณกัน ฉันคิดว่าคุณได้เชื่อมต่ออุปกรณ์มือถือ Android จริงกับคอมพิวเตอร์ของคุณ ในการรันแอพจาก android studio ให้เปิดไฟล์กิจกรรมของโปรเจ็กต์ของคุณแล้วคลิก Run จะสร้างข้อความการแบ่งหน้าใน Android โดยใช้ Kotlin ได้อย่างไร? ไอคอนจากแถบเครื่องมือ เลือกอุปกรณ์มือถือของคุณเป็นตัวเลือก จากนั้นตรวจสอบอุปกรณ์มือถือของคุณซึ่งจะแสดงหน้าจอเริ่มต้นของคุณ

จะสร้างข้อความการแบ่งหน้าใน Android โดยใช้ Kotlin ได้อย่างไร?