วันอาทิตย์ที่ 11 มีนาคม พ.ศ. 2555

Android Button

ถ้าขี้เกียจCopy codeจากด้านล่าง และออกแบบ หน้าจอ,  E-mail มาขอ source ไปเล่นก็ได้ครับ













สร้าง Folder layout-land ขึ้นมาใหม่ แล้ว copy main.xml ใ้น layout ที่เราออกแบบในแนวตั้งไว้แล้ว มาแก้ไขในหอยู่ในแนวนอน










Source code


File..... AndroidButtonExampleActivity.java
package com.example.button;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Android_Button_ExampleActivity extends Activity implements OnClickListener{
    /** Called when the activity is first created. */
    private Button one1,two,three,four,five,six,seven,eight,nine,zero,reset;
    public TextView textview1,textview2;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        //link variable to each component in form
        one1 = (Button)findViewById(R.id.button1);
        two =(Button)findViewById(R.id.button2);
        three = (Button)findViewById(R.id.button3);
        four = (Button)findViewById(R.id.button4);
        five = (Button)findViewById(R.id.button5);
        six = (Button)findViewById(R.id.button6);
        seven = (Button)findViewById(R.id.button7);
        eight = (Button)findViewById(R.id.button8);
        nine = (Button)findViewById(R.id.button9);
        zero = (Button)findViewById(R.id.button0);
        reset = (Button)findViewById(R.id.buttonReset);
        textview1 = (TextView)findViewById(R.id.textView1);
        textview2 = (TextView)findViewById(R.id.textView2);
       
        textview1.setText("ปุ่ม one ถูกกด");
       
        // set listener on click
        one1.setOnClickListener(this);
         two.setOnClickListener(this);
        three.setOnClickListener(this);
        four.setOnClickListener(this);
        five.setOnClickListener(this);
        six.setOnClickListener(this);
        seven.setOnClickListener(this);
        eight.setOnClickListener(this);
        nine.setOnClickListener(this);
        zero.setOnClickListener(this);
        reset.setOnClickListener(this);
       
    }
   
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId()){
        case R.id.button1:
            textview1.setText("ปุ่ม one ถูกกด");   
            textview2.setText("1");
            break;
        case R.id.button2:
            textview1.setText("ปุ่ม two ถูกกด");
            textview2.setText("2");
            break;
        case R.id.button3:
            textview1.setText("ปุ่ม three ถูกกด");
            textview2.setText("3");
            break;
        case R.id.button4:
            textview1.setText("ปุ่ม four ถูกกด");
            textview2.setText("4");
            break;
        case R.id.button5:
            textview1.setText("ปุ่ม five ถูกกด");
            textview2.setText("5");
            break;
        case R.id.button6:
            textview1.setText("ปุ่ม six ถูกกด");
            textview2.setText("6");
            break;
        case R.id.button7:
            textview1.setText("ปุ่ม seven ถูกกด");
            textview2.setText("7");
            break;
        case R.id.button8:
            textview1.setText("ปุ่ม eight ถูกกด");
            textview2.setText("8");
            break;
        case R.id.button9:
            textview1.setText("ปุ่ม nine ถูกกด");
            textview2.setText("9");
            break;
        case R.id.button0:
            textview1.setText("ปุ่ม zero ถูกกด");
            textview2.setText("0");
            break;
        case R.id.buttonReset:
            textview1.setText("ปุ่ม reset ถูกกด");
            textview2.setText("");
            break;
           
        }//end select
    }//end onclick
}



File Main.xml ใน Folder layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="TextView" android:layout_gravity="center" android:textSize="40dp" android:layout_margin="10dp" android:background="#E6FE80" android:textColor="#000000"/>

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" android:layout_marginTop="10dp">

        <Button
            android:id="@+id/button1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="One" android:layout_gravity="center" android:padding="2dp" android:layout_weight="1"/>

        <Button
            android:id="@+id/button2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Two" android:layout_gravity="center" android:padding="2dp" android:layout_weight="1"/>

        <Button
            android:id="@+id/button3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Three" android:layout_gravity="center" android:padding="2dp" android:layout_weight="1"/>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/button4"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Four" android:layout_weight="1" android:layout_gravity="center" android:padding="2dp"/>

        <Button
            android:id="@+id/button5"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Five" android:layout_weight="1" android:layout_gravity="center" android:padding="2dp"/>

        <Button
            android:id="@+id/button6"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Six" android:layout_weight="1" android:layout_gravity="center" android:padding="2dp"/>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/button7"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Seven" android:layout_weight="1" android:layout_gravity="center" android:padding="2dp"/>

        <Button
            android:id="@+id/button8"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Eight" android:layout_weight="1" android:layout_gravity="center" android:padding="2dp"/>

        <Button
            android:id="@+id/button9"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Nine" android:layout_weight="1" android:layout_gravity="center" android:padding="2dp"/>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/button0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="zero" android:layout_weight="1"/>

        <Button
            android:id="@+id/buttonReset"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Reset" android:layout_weight="1"/>

    </LinearLayout>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="100dp" android:text="1" android:layout_gravity="center" android:textStyle="bold"/>

</LinearLayout>



File Main.xml ใน Folder layout-land

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="#E6FE80"
        android:text="TextView"
        android:textColor="#000000"
        android:textSize="40dp" />

    <LinearLayout
        android:id="@+id/linearLayout6"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1" >

        <LinearLayout
            android:id="@+id/linearLayout5"
            android:layout_width="286dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical" >

            <LinearLayout
                android:id="@+id/linearLayout1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:layout_weight="1" >

                <Button
                    android:id="@+id/button1"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_weight="1"
                    android:padding="2dp"
                    android:text="One" />

                <Button
                    android:id="@+id/button2"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_weight="1"
                    android:padding="2dp"
                    android:text="Two" />

                <Button
                    android:id="@+id/button3"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_weight="1"
                    android:padding="2dp"
                    android:text="Three" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/linearLayout2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1" >

                <Button
                    android:id="@+id/button4"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_weight="1"
                    android:padding="2dp"
                    android:text="Four" />

                <Button
                    android:id="@+id/button5"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_weight="1"
                    android:padding="2dp"
                    android:text="Five" />

                <Button
                    android:id="@+id/button6"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_weight="1"
                    android:padding="2dp"
                    android:text="Six" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/linearLayout3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1" >

                <Button
                    android:id="@+id/button7"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_weight="1"
                    android:padding="2dp"
                    android:text="Seven" />

                <Button
                    android:id="@+id/button8"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_weight="1"
                    android:padding="2dp"
                    android:text="Eight" />

                <Button
                    android:id="@+id/button9"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_weight="1"
                    android:padding="2dp"
                    android:text="Nine" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/linearLayout4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1" >

                <Button
                    android:id="@+id/button0"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="zero" />

                <Button
                    android:id="@+id/buttonReset"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Reset" />
            </LinearLayout>
        </LinearLayout>

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="1"
            android:textSize="100dp"
            android:textStyle="bold" />

    </LinearLayout>

</LinearLayout>


หมดแล้ว...........สุดท้ายอย่าลืม copy รูปภาพ button1.png ไปไว้ที่ drawable-ldpi , drawable-mdpi และ drawable-hdpi

ไม่มีความคิดเห็น:

แสดงความคิดเห็น