วันจันทร์ที่ 18 มิถุนายน พ.ศ. 2555

Android Button Example Code

--> Android Button Example Code


Java code AndroidButtonActivity.java

package android.example.button;

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

public class AndroidButtonActivity extends Activity {
     
      int count1=0;
      int count2=0;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        Button btn1 = (Button)findViewById(R.id.button1);      
        Button btn2 = (Button)findViewById(R.id.button2);       
        final ToggleButton btnToggle = (ToggleButton)findViewById(R.id.toggleButton);
  
        final TextView tv1 = (TextView)findViewById(R.id.textView1);
        final TextView tv2 = (TextView)findViewById(R.id.textView2);
        final TextView tv3 = (TextView)findViewById(R.id.textView3);

        btn1.setOnClickListener(new View.OnClickListener() {
                  public void onClick(View v) {                  
                        // Do Something When Button Click                    
                        count1++;
                        tv1.setText("Normal Button Click " + count1);           
                  }  
        });
       
        btn2.setOnClickListener(new View.OnClickListener() {
                  public void onClick(View v) {                  
                        // Do Something When Button Click
                        count2++;                    
                        tv2.setText("Small Button Click " + count2);            
                  }  
        });
              
        btnToggle.setOnClickListener(new View.OnClickListener() {
                  public void onClick(View v) {
               
                        if(btnToggle.isChecked()){         
                              tv3.setText("Toggle Button ON ");
                        }  
                        else{            
                              tv3.setText("Toggle Button OFF");
                        }
                  }
        });
    }
}

 Android Button Example code - AndroidMainifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="android.example.button"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".AndroidButtonActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


Download Code
Android Button Example Code 

More Info
Android Menu Example Code
-->


Android Control ควบคุมอุปกรณ์ต่างๆ ด้วย Android

สอนเขียน Android  สอนเขียนโปรแกรม Android