Trong bài học này sẽ trình bày các vấn đề về sử lý sự kiện cho các đối tượng.
Nội dung:
I. Tổng quan xử lý sự kiện mức cao và mức thấp:
II. Các kỹ thuật xử lý sự kiện
  1/ Onclick in XML: sử dụng xml để gọi hàm tính tổng
  2/ Inline anonymous listener
  3/ Activity is listener
  4/ Activity in variable
  5/ Explicit listener class
III. Xử lý sự kiện cho: check boxes , radio button, radio groups, spinners, seek bars
  1/ Check boxs, radio buttons
  2/ Radio groups
  3/ Spinners
  4/ Seek bars
VI. Xử lý key và touch
  1/ Xử lý sự kiện key
  2/ Xử lý sự kiện touch
------------------------------------------
I. Tổng quan xử lý sự kiện mức cao và mức thấp:

II. Các kỹ thuật xử lý sự kiện:
1/ Onclick in XML: sử dụng xml để gọi hàm tính tổng

Outline:

- Code xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/so_a"
        android:textSize="20sp"/>

    <EditText
        android:id="@+id/editText_so_a"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number"> <!-- Chỉ cho nhập số -->
        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="@string/so_b" />

    <EditText
        android:id="@+id/editText_so_b"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number"/><!-- Chỉ cho nhập số -->

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="@string/tong" />

    <TextView
        android:id="@+id/textView_ketqua"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />

    <Button
        android:id="@+id/btn_tong"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="btn_tong"
        android:layout_gravity="center"
        android:text="@string/tinh_tong" />

</LinearLayout>

-Chú ý sử dụng xml:

- Mã nguồn java:
public class MainActivity extends Activity {
             EditText eText_a, eText_b;
       TextView textView_kq;

       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);

              eText_a = (EditText) findViewById(R.id.editText_so_a);
              eText_b = (EditText) findViewById(R.id.editText_so_b);
              textView_kq = (TextView) findViewById(R.id.textView_ketqua);
       }
      
       public void btn_tong(View v) {//trùng tên hàm trong xml
         int a = Integer.parseInt( eText_a.getText() + "");        
         int b = Integer.parseInt( eText_b.getText() + "");    
         textView_kq.setText(a + b + "");
       }
       //…………
}

Link download bài 6_1:
https://drive.google.com/file/d/0B8tAQ0_sJKCSeGl0cGptcV81MFE/view?usp=sharing

2/ Inline anonymous listener: Với cách này mỗi button chúng ta sẽ viết một hàm setOnClickListener.
Mã code java:
public class MainActivity extends Activity {
       Button btn_tong;
       EditText eText_a, eText_b;
       TextView textView_kq;
      
       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
             
              btn_tong = (Button) findViewById(R.id.btn_tong);
              eText_a = (EditText) findViewById(R.id.editText_so_a);
              eText_b = (EditText) findViewById(R.id.editText_so_b);
              textView_kq = (TextView) findViewById(R.id.textView_ketqua);
             
              btn_tong.setOnClickListener(new View.OnClickListener() {            
                     @Override
                     public void onClick(View v) {
                           int a = Integer.parseInt(eText_a.getText() + "");
                           int b = Integer.parseInt(eText_b.getText() + "");
                           textView_kq.setText(a + b + "");
                     }
              });
       }
       //......
}

Link download bài 6_2:
https://drive.google.com/file/d/0B8tAQ0_sJKCSMEYwblpYSmF1cEk/view?usp=sharing

3/ Activity is listener:
Trong cách viết sự kiện này thì Activity sẽ implements OnClickListener
Bước 1: import the interface for the listener
             import android.view.View.OnClickListener;
Bước 2a: implement the interface for listener
            public class MainActivity extends Activity implements OnClickListener {
Bước 2b: Override lại hàm onClick
           @Override
       public void onClick(View v) {
              switch(v.getId()) {
                     case R.id.btn_tong:
                          //xử lý ở đây..
                     break;
                     //.....
              }
       }
Bước 3: Set the listeners
           btn_tinhtong.setOnClickListener(this);

Mã nguồn java:
public class MainActivity extends Activity implements OnClickListener{
       Button btn_tinhtong;
       EditText eText_a, eText_b;
       TextView textView_kq;
      
       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
             
              btn_tinhtong = (Button) findViewById(R.id.btn_tong);
              btn_tinhtong.setOnClickListener(this);
              eText_a = (EditText) findViewById(R.id.editText_so_a);
              eText_b = (EditText) findViewById(R.id.editText_so_b);
              textView_kq = (TextView) findViewById(R.id.textView_ketqua);
       }

       @Override
       public void onClick(View v) {
              switch(v.getId()) {
                     case R.id.btn_tong:
                           int a = Integer.parseInt( eText_a.getText() + "");
                           int b = Integer.parseInt( eText_b.getText() + "");
                           textView_kq.setText(a + b + "");
                     break;
                     //.....
              }
             
       }
       //.............
}

Link download bài 6_3:
https://drive.google.com/file/d/0B8tAQ0_sJKCSSTNPNW9KYUpMaTg/view?usp=sharing

4/ Activity in variable: Tương tự như cách 3 Activity Is listener, nhưng  khác ở chỗ thay vì implement interface OnClickListener cho Activity thì nó lại được lưu trữ vào một biến có kiểu Listener trong Activity. Làm cách này thì ta có thể chia sẻ chung một biến sự kiện cho các control (cho nhiều button) khác nhau.
Mã nguồn java:
public class MainActivity extends Activity {
       Button btn_tong;
       EditText eText_a, eText_b;
       TextView textView_kq;
      
       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
             
              btn_tong = (Button) findViewById(R.id.btn_tong);
              eText_a = (EditText) findViewById(R.id.editText_so_a);
              eText_b = (EditText) findViewById(R.id.editText_so_b);
              textView_kq = (TextView) findViewById(R.id.textView_ketqua);
             
              btn_tong.setOnClickListener(myClickListener);
       }
      
       OnClickListener myClickListener = new OnClickListener() {
              @Override
              public void onClick(View v) {
                     if (v == btn_tong) {
                           int a = Integer.parseInt(eText_a.getText() + "");
                           int b = Integer.parseInt(eText_b.getText() + "");
                           textView_kq.setText(a + b + "");
                     }
              }
       };
       //.....
}


Link download bài 6_4:
https://drive.google.com/file/d/0B8tAQ0_sJKCScUs3REtrRFlKZDg/view?usp=sharing

5/ Explicit listener classTrường hợp này ta tách riêng một class đóng vai trò là class sự kiện riêng. Khi nào lượng coding trong ứng dụng khổng lồ và phức tạp thì ta nên tách class sự kiện riêng để dễ quản lý.
Lưu ý: Lớp sử lý sự kiện riêng này private class MyEvent implements OnClickListener cũng nằm trong lớp MainActivity gọi lớp lồng lớp.
Mã java:
public class MainActivity extends Activity {
       Button btn_tong;
       EditText eText_a, eText_b;
       TextView textView_kq;
      
       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
             
              btn_tong = (Button) findViewById(R.id.btn_tong);
              btn_tong.setOnClickListener(new MyEvent());
             
              eText_a = (EditText) findViewById(R.id.editText_so_a);
              eText_b = (EditText) findViewById(R.id.editText_so_b);
              textView_kq = (TextView) findViewById(R.id.textView_ketqua);
       }

       private class MyEvent implements OnClickListener {
              @Override
              public void onClick(View v) {
                     if (v.getId() == R.id.btn_tong) {
                           int a = Integer.parseInt(eText_a.getText() + "");
                           int b = Integer.parseInt(eText_b.getText() + "");
                           textView_kq.setText(a + b + "");
                     }
              }
       }
       //.....
}

Link download bài 6_5:
https://drive.google.com/file/d/0B8tAQ0_sJKCSWURCZFVQUENCTEE/view?usp=sharing

III. Xử lý sự kiện cho: check boxes , radio button, radio groups, spinners, seek bars
  1/ Check box: xử lý sự kiện check box
Bước 1: import android.widget.CompoundButton;

Bước 2a: public class MainActivity extends Activity implements OnCheckedChangeListener {

Bước 2b: Override lại hàm onCheckedChanged
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
       switch(buttonView.getId()) {
              case R.id.CheckBox_music:
                     if (isChecked) {
                           Log.i("Checkbox", "true");
                     } else {
                           Log.i("Checkbox", "false");
                     }
                     break;
              //...
       }
}
   
Bước 3: Set the listeners
             checkbox_music.setOnCheckedChangeListener(this);

  2/ Radio groups:

  3/ Spinners:

  4/ Seek bars:
  ----------------------
Ví dụ 6: Xây dựng dụng như sau, khi bấm vào button nào thì TextView bên dưới sẽ đổi màu theo.
Outline:
Mã XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.exampleevenlistener.MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/btn_red"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Red"
            android:textColor="#FF0000" />

        <Button
            android:id="@+id/btn_green"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Green"
            android:textColor="#00FF00" />

        <Button
            android:id="@+id/btn_blue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Blue"
            android:textColor="#0000FF" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <Button
            android:id="@+id/btn_random"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Random"
            android:textColor="#FF0000" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <RadioGroup
            android:id="@+id/radioGroupColor"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/rd_red"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:checked="true"
                android:text="Red" />

            <RadioButton
                android:id="@+id/rd_green"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Green" />

            <RadioButton
                android:id="@+id/rd_blue"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Blue" />
        </RadioGroup>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textViewColor"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#FF0000" />
    </LinearLayout>
</LinearLayout>

Mã java: MainActivity.java
//...
public class MainActivity extends Activity implements OnClickListener, OnCheckedChangeListener {
       private Button btnRed;
       private Button btnGreen;
       private Button btnBlue;
       private Button btnRandom;
       private TextView tvColoring;
       private RadioGroup rdColor;
       private RadioButton rdbRed;
       private RadioButton rdbGreen;
       private RadioButton rdbBlue;
      
       private final static int[] mColorChoices = {Color.RED, Color.GREEN, Color.BLUE };
      
       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              //get references to the widgets
              btnRed = (Button) findViewById(R.id.btn_red);
              btnGreen = (Button) findViewById(R.id.btn_green);
              btnBlue = (Button) findViewById(R.id.btn_blue);
              btnRandom = (Button) findViewById(R.id.btn_random);
              rdbRed = (RadioButton) findViewById(R.id.rd_red);
              rdbGreen = (RadioButton) findViewById(R.id.rd_green);
              rdbBlue = (RadioButton) findViewById(R.id.rd_blue);
     
              rdColor = (RadioGroup) findViewById(R.id.radioGroupColor);
              tvColoring = (TextView) findViewById(R.id.textViewColor);
     
              //set the listeners
              btnRed.setOnClickListener(this);
              btnGreen.setOnClickListener(this);
              btnBlue.setOnClickListener(this);
              btnRandom.setOnClickListener(this);

              rdColor.setOnCheckedChangeListener(this);
              tvColoring.setOnClickListener(this);
       }

       //Event handler for the button
       @Override
       public void onClick(View v) {
              switch(v.getId()) {
                     case R.id.btn_red:
                           changeColor(Color.RED);
                           break;
                     case R.id.btn_green:
                           changeColor(Color.GREEN);
                           break;
                          
                     case R.id.btn_blue:
                           changeColor(Color.BLUE);
                           break;
                     case R.id.btn_random:
                           Random random = new Random();
                           int numRandom = random.nextInt(mColorChoices.length);
                           changeColor(mColorChoices[numRandom]);
                           Log.i("EventListener: ", numRandom + "");
                           break;       
                     case R.id.textViewColor:
                           changeColor(Color.WHITE);
                           break;
              }
       }
      
       public void changeColor(int color) {
              tvColoring.setBackgroundColor(color);
       }
      
       //Event handler for the RadioGroup
       @Override
       public void onCheckedChanged(RadioGroup group, int checkedId) {
              Log.i("Radio button blue change", "checked");
              switch(checkedId) {
                     case R.id.rd_red:
                           changeColor(Color.RED);
                     break;       
                     case R.id.rd_green:
                           changeColor(Color.GREEN);
                     break;
                     case R.id.rd_blue:
                           changeColor(Color.BLUE);
                     break;              
              }
       }     
       //….
}
Link download ví dụ này:
https://drive.google.com/file/d/0B8tAQ0_sJKCSOGtacVAzS1FwM3M/view?usp=sharing

Ví dụ 7: Thiết kế giao diện thông tin cá nhân như hình dưới
Outline:

Mã XML:
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.eventlistener.MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#1F45FC"
        android:gravity="center"
        android:text="Thông tin cá nhân"
        android:textColor="#FFFFFF"
        android:textSize="25sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Họ tên:"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/editText_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="4"
            android:ems="10" >

            <requestFocus />
        </EditText>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#00BFFF"
            android:gravity="center"
            android:text="Giới tính"
            android:textSize="20sp" />

        <RadioGroup
            android:id="@+id/radioGroup_sex"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/radio_sexmale"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:checked="true"
                android:text="Nam" />

            <RadioButton
                android:id="@+id/radio_sexfemale"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Nữ" />

            <RadioButton
                android:id="@+id/radio_sexother"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Khác" />

        </RadioGroup>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/TextView01"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#00BFFF"
            android:gravity="center"
            android:text="Sở thích"
            android:textSize="20sp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <CheckBox
            android:id="@+id/CheckBox_music"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Nghe Nhạc" />

        <CheckBox
            android:id="@+id/CheckBox_sport"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Thể thao" />

        <CheckBox
            android:id="@+id/checkBox_coding"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Lập trình" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/TextView02"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#00BFFF"
            android:gravity="center"
            android:text="Thông tin khác"
            android:textSize="20sp" />
    </LinearLayout>

    <EditText
        android:id="@+id/editText_information"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textMultiLine"
        android:maxLines="5"
        android:minLines="5" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center|bottom"
        android:orientation="vertical" >

        <Button
            android:id="@+id/btn_send"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Gởi thông tin" />

        <TextView
            android:id="@+id/textView_bottombar"
            android:layout_width="match_parent"
            android:layout_height="20dp"
            android:background="#1F45FC" />

    </LinearLayout>
</LinearLayout>
Mã java:
package com.eventlistener;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioGroup;

public class MainActivity extends Activity implements OnClickListener, OnCheckedChangeListener, android.widget.RadioGroup.OnCheckedChangeListener {
       CheckBox checkbox_music, checkbox_sport, checkbox_coding;
       RadioButton rdb_sexmale, rdb_sexfemale, rdb_sexother;
       RadioGroup radioGroup_sex;
       Button btn_send;
      
       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              checkbox_music = (CheckBox) findViewById(R.id.CheckBox_music);
              checkbox_sport = (CheckBox) findViewById(R.id.CheckBox_sport);
              checkbox_coding = (CheckBox) findViewById(R.id.checkBox_coding);
             
              radioGroup_sex = (RadioGroup) findViewById(R.id.radioGroup_sex);
              rdb_sexmale = (RadioButton) findViewById(R.id.radio_sexmale);
              rdb_sexfemale = (RadioButton) findViewById(R.id.radio_sexfemale);
              rdb_sexother = (RadioButton) findViewById(R.id.radio_sexother);

              btn_send = (Button) findViewById(R.id.btn_send);
              radioGroup_sex.setOnCheckedChangeListener(this);
             
              checkbox_music.setOnCheckedChangeListener(this);
              checkbox_sport.setOnCheckedChangeListener(this);
              checkbox_coding.setOnCheckedChangeListener(this);
              btn_send.setOnClickListener(this);
       }

       @Override
       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
              switch(buttonView.getId()) {
                     case R.id.CheckBox_music:
                           if (isChecked) {
                                  Log.i("Checkbox", "true");
                           } else {
                                  Log.i("Checkbox", "false");
                           }
                           break;
                     //...
              }
       }
      
       //Handler event button
       @Override
       public void onClick(View v) {
              String result = "";
              if (v.getId() == R.id.btn_send) {
                     if (checkbox_music.isChecked()) {
                           result += "Music ";
                     }
                     if (checkbox_sport.isChecked()) {
                           result += "Sport ";
                     }
                     if (checkbox_coding.isChecked()) {
                           result += "Coding ";
                     }
                     Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
              }
       }            

       //Event handler for the RadioGroup
       @Override
       public void onCheckedChanged(RadioGroup group, int checkedId) {
              Log.i("Radio button change", "checked");
              String radioSelect = "";
              switch(checkedId){               
                     case R.id.radio_sexmale:
                           radioSelect += "Male";
                           break;
                     case R.id.radio_sexfemale:
                           radioSelect += "Female";
                           break;
                     case R.id.radio_sexother:
                           radioSelect += "Other";
                           break;
              }
              Toast.makeText(getApplicationContext(), radioSelect, Toast.LENGTH_LONG).show();
       }
       //...
}

Link download ví dụ này:

VI. Xử lý key và touch
  1/ Xử lý sự kiện key:

  2/ Xử lý sự kiện touch:

Bài tập:
1. Xây dựng ứng dụng máy tính cá nhân
Outline:

Mã XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp" >

    <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="vertical"
       android:layout_marginBottom="5dp">

        <EditText
            android:id="@+id/editText_input"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:editable="false"
            android:ems="10"
            android:gravity="right" >

              <requestFocus />
        </EditText>

        <EditText
            android:id="@+id/editText_result"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:editable="false"
            android:ems="10"
            android:gravity="right" />

    </LinearLayout>

    <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_xoa"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="C" />

        <Button
            android:id="@+id/btn_del"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="Del" />

        <Button
            android:id="@+id/btn_cong"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="+" />

    </LinearLayout>

    <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal" >

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

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

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

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

    </LinearLayout>

    <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal" >

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

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

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

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

    </LinearLayout>

    <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal" >

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

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

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

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

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/btn_0"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="0" />

        <Button
            android:id="@+id/btn_cham"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="." />

        <Button
            android:id="@+id/btn_tinh"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="=" />

    </LinearLayout>

</LinearLayout>

Mã java: xem link bên dưới

Hàm tính giá trị biểu thức với chuỗi string biểu thức truyền vào:
public static double Tinh(final String str) {
   
return new Object() {
       
int pos = -1, ch;

       
void nextChar() {
           
ch = (++pos < str.length()) ? str.charAt(pos) : -1;
        }

       
boolean eat(int charToEat) {
           
while (ch == ' ') nextChar();
           
if (ch == charToEat) {
                nextChar();
               
return true;
            }
           
return false;
        }

       
double parse() {
            nextChar();
           
double x = parseExpression();
           
if (pos < str.length()) throw new RuntimeException("Unexpected: " + (char)ch);
           
return x;
        }

       
// Grammar:
        // expression = term | expression `+` term | expression `-` term
        // term = factor | term `*` factor | term `/` factor
        // factor = `+` factor | `-` factor | `(` expression `)`
        //        | number | functionName factor | factor `^` factor

       
double parseExpression() {
           
double x = parseTerm();
           
for (;;) {
               
if      (eat('+')) x += parseTerm(); // addition
               
else if (eat('-')) x -= parseTerm(); // subtraction
               
else return x;
            }
        }

       
double parseTerm() {
           
double x = parseFactor();
           
for (;;) {
               
if      (eat('*')) x *= parseFactor(); // multiplication
               
else if (eat('/')) x /= parseFactor(); // division
               
else return x;
            }
        }

       
double parseFactor() {
           
if (eat('+')) return parseFactor(); // unary plus
           
if (eat('-')) return -parseFactor(); // unary minus

           
double x;
           
int startPos = this.pos;
           
if (eat('(')) { // parentheses
               
x = parseExpression();
                eat(
')');
            }
else if ((ch >= '0' && ch <= '9') || ch == '.') { // numbers
               
while ((ch >= '0' && ch <= '9') || ch == '.') nextChar();
                x = Double.parseDouble(
str.substring(startPos, this.pos));
            }
else if (ch >= 'a' && ch <= 'z') { // functions
               
while (ch >= 'a' && ch <= 'z') nextChar();
                String func =
str.substring(startPos, this.pos);
                x = parseFactor();
               
if (func.equals("sqrt")) x = Math.sqrt(x);
               
else if (func.equals("sin")) x = Math.sin(Math.toRadians(x));
               
else if (func.equals("cos")) x = Math.cos(Math.toRadians(x));
               
else if (func.equals("tan")) x = Math.tan(Math.toRadians(x));
               
else throw new RuntimeException("Unknown function: " + func);
            }
else {
               
throw new RuntimeException("Unexpected: " + (char)ch);
            }

           
if (eat('^')) x = Math.pow(x, parseFactor()); // exponentiation

           
return x;
        }
    }.parse();
}



Link download bài máy tính:
https://drive.google.com/file/d/0B8tAQ0_sJKCSbnZ2MkxYYktOcGs/view?usp=sharing