รับทําเว็บไซต์ รับทําseo
บทความที่น่าสนใจ

บทความ ที่น่าสนใจ

Android Application : การนับจำนวนตัวอักษรใน TextView

     ในบทความนี้ จะแสดงให้เห็นถึงตัวอย่างการนับจำนวนตัวอักษรใน TextView โดยจะแสดงให้เห็นถึงการเขียนคำสั่งภายใน Class Activity, การเขียนคำสั่งภายในไฟล์ XML Layout, และการประกาศ Activity ไว้ภายในไฟล์ AndroidManifest.xml ดังนี้


    การเขียนคำสั่งภายใน Class Activity

        package test.me.activity;
         
        import android.app.Activity;
        import android.os.Bundle;
        import android.widget.TextView;
        import android.widget.Toast;
         
        public class MainActivity extends Activity
        {
            public void onCreate ( Bundle savedInstanceState )
            {
                super.onCreate ( savedInstanceState );
                 
                this.setContentView ( R.layout.activity_main );
                 
                 
                TextView label = ( TextView ) this.findViewById ( R.id.label );
                 
                int countCharacter = label.length ( );
                 
                String countChar = Integer.toString ( countCharacter );
                 
                 
                Toast toast = Toast.makeText ( this, countChar, Toast.LENGTH_LONG );
                 
                toast.show ( );
            }
        }


    จากคำสั่งข้างต้น สามารถอธิบายได้ว่า คำสั่ง TextView label = ( TextView ) this.findViewById ( R.id.label ); ในบรรทัดที่ 17 เป็นการสร้าง Object TextView โดยการไปค้นหามาจาก XML Layout และคำสั่ง int countCharacter = label.length ( ); ในบรรทัดที่ 19 เป็นการคืนค่าจำนวนตัวอักษรใน TextView


    การเขียนคำสั่งภายในไฟล์ XML Layout

        <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
         
            <TextView
            android:id="@+id/label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="Hello World" />
         
        </RelativeLayout>


    จากคำสั่งข้างต้น สามารถอธิบายได้ว่า element <TextView> มี id คือ label (ประกาศไว้ในบรรทัดที่ 7) ซึ่งถูกใช้อ้างอิงใน Class Activity ข้างต้น


    การประกาศ Activity ไว้ภายในไฟล์ AndroidManifest.xml

        <?xml version="1.0" encoding="utf-8"?>
        <manifest ... >
            <application ... >
                <activity android:name="test.me.activity.MainActivity" ... >
                    <intent-filter>
                        <action android:name="android.intent.action.MAIN" />
                        <category android:name="android.intent.category.LAUNCHER" />
                    </intent-filter>
                </activity>
            </application>
        </manifest>
     

    จากคำสั่งข้างต้น สามารถอธิบายได้ว่า มีการประกาศ Activity ชื่อ MainActivity ที่อยู่ใน Package test.me.activity ไว้ภายในไฟล์ AndroidManifest.xml ในบรรทัดที่ 4

     

บทความที่น่าสนใจ

บทความ ล่าสุด

บทความ ความรู้ด้านไอที, คอมพิวเตอร์ Techonlogy, Gadget, ความรู้เกี่ยวกับคอมพิวเตอร์ กับทาง SoftMelt.com