[ Lập trình Android ] Ví dụ: Xây dựng ứng dụng MÁY TÍNH ĐIỆN TỬ [ ứng dụng Android cơ bản ]

Xây dựng ứng dụng mô phỏng máy tính điện tử đơn giản trên Android:


Để xây dựng ứng dụng trên, chúng ta tiến hành như sau (Cài đặt trên môi trường Eclipse đã plugin SDK, xem thêm tại đây):

Bước 1: Tạo Project Vidu1 với các thành phần như hình vẽ


 Bước 2: Viết nội dung các file:  PhepTinh.java (chứa các xử lý tính toán); Vidu1.Activity.java (file chạy Android); main.xml (thiết kế giao diện); color.xml (thiết lập màu sắc); string.xml (thiết lập các giá trị cho string). Nội dung cụ thể:

+ color.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="title_color">#0000FF</color>
<color name="text_color">#DC143C</color>
<color name="resul_color">#FFD700</color>
<color name="backg_color">#F0F8FF</color>
<color name="btn_color">#FF0000</color>

</resources>

+ string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="str1">MÁY TÍNH ĐIỆN TỬ</string>
    <string name="app_name">Vd1</string>
    <string name="a">a: </string>
    <string name="b">b: </string>
    <string name="kq">Kết quả: </string>
    <string name="cong">+</string>
    <string name="tru">-</string>
    <string name="nhan">x</string>
    <string name="chia">/</string>
    <string name="xoa">Xóa</string>
    <string name="thoat">Thoát</string>
    <string name="tb1">Nhập số thứ nhất</string>
    <string name="tb2">Nhập số thứ hai</string>
     <string name="chao">http://lap-trinh-may-tinh.blogspot.com</string>
</resources>

+ main.xml

<?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"
    android:background="@color/backg_color"
    >
   
       
   <TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"     
     android:textColor="@color/title_color"
     android:text="@string/str1"       
     android:textSize="30dp"
     />

   
   <LinearLayout
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:orientation="horizontal"
       >
       
   <TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"     
     android:textColor="@color/text_color"
     android:text="@string/a"       
     android:textSize="30dp"
     />
   
   <EditText
android:layout_width="200dp"
     android:layout_height="50dp"
     android:textColor="@color/text_color"
     android:id="@+id/so1"
android:hint="@string/tb1"
              
   />
   
   </LinearLayout>
   
   <LinearLayout
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:orientation="horizontal"
       >
   <TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:textColor="@color/text_color"
     android:text="@string/b"       
     android:textSize="30dp"
     />
    <EditText
android:layout_width="200dp"
     android:layout_height="50dp"
     android:textColor="@color/text_color"
     android:id="@+id/so2"
android:hint="@string/tb2"
              
   />
   
   </LinearLayout>
   
   <LinearLayout
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:orientation="horizontal"
       >
   <TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:textColor="@color/resul_color"
     android:text="@string/kq"       
     android:textSize="30dp"
     />
   <EditText
android:layout_width="200dp"
     android:layout_height="50dp"
     android:textColor="@color/resul_color"
     android:id="@+id/ketqua"              
   />   
   </LinearLayout>
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
 <Button   
     android:layout_width="80dp"
     android:layout_height="40dp"
     android:text="@string/cong"
     android:textColor="@color/btn_color"
     android:id="@+id/phepCong"
    />
    <Button   
     android:layout_width="80dp"
     android:layout_height="40dp"
     android:text="@string/tru"
     android:textColor="@color/btn_color"
     android:id="@+id/phepTru"
    />
    <Button   
     android:layout_width="80dp"
     android:layout_height="40dp"
     android:text="@string/nhan"
     android:textColor="@color/btn_color"
     android:id="@+id/phepNhan"
    />
    <Button   
     android:layout_width="80dp"
     android:layout_height="40dp"
     android:text="@string/chia"
     android:textColor="@color/btn_color"
     android:id="@+id/phepChia"
    />
 </LinearLayout>
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
 <Button   
     android:layout_width="80dp"
     android:layout_height="40dp"
     android:text="@string/xoa"
     android:textColor="@color/btn_color"
     android:id="@+id/delete"
    />
    <Button   
     android:layout_width="80dp"
     android:layout_height="40dp"
     android:text="@string/thoat"
     android:textColor="@color/btn_color"
     android:id="@+id/cancel"
    />
 </LinearLayout>
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
  <TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"     
     android:textColor="@color/text_color"
     android:text="@string/chao"       
     android:textSize="12dp"
     />
 </LinearLayout>

</LinearLayout>

+ PhepTinh.java:

package txt.vidu1.vidu;

public class PhepTinh {
 public float a,b;
 // khoi tao
 public PhepTinh(float a,float b){
this.a=a;this.b=b;
 }
 // tinh tong
 public float Tong(){
return a+b;
 }
 // tinh hieu
 public float Hieu(){
return a-b;
 }
 public float Nhan(){
return a*b;
 }
 public float Chia(){
if (b==0)
return 0;
return a/b;
 
 }
}

Vidu1.Activity.java

package txt.vidu1.vidu;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Vidu1Activity extends Activity {
    
    protected AlertDialog AlertDialog;

@Override
    public void onCreate(Bundle savedInstanceState) {
        
    super.onCreate(savedInstanceState);
        
        setContentView(R.layout.main);
        // khai bao cac button
        final Button btnCong= (Button) findViewById(R.id.phepCong);
        final Button btnTru=(Button) findViewById(R.id.phepTru);
        final Button btnNhan=(Button) findViewById(R.id.phepNhan);
        final Button btnChia=(Button) findViewById(R.id.phepChia);
        final Button btnXoa=(Button) findViewById(R.id.delete);
        final Button btnThoat=(Button) findViewById(R.id.cancel);
        // khai bao textView
        final TextView txvKQ=(TextView) findViewById(R.id.ketqua);
        
        // Su kien click Phep Cong
        btnCong.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
         float a= Float.valueOf(((EditText) findViewById(R.id.so1)).getText().toString());
         float b= Float.valueOf(((EditText) findViewById(R.id.so2)).getText().toString());
         PhepTinh c=new PhepTinh(a,b);
         String s=String.valueOf(c.Tong());
         txvKQ.setText(s);
        }                
        });
        // Su kien click Phep tru
        btnTru.setOnClickListener(new Button.OnClickListener() {
          public void onClick(View v){
         float a,b;
         a=Float.valueOf(((EditText) findViewById(R.id.so1)).getText().toString());
         b=Float.valueOf(((EditText) findViewById(R.id.so2)).getText().toString());
         PhepTinh t=new PhepTinh(a,b);
         txvKQ.setText(String.valueOf(t.Hieu()));          
          }
        }        
        );
        // Su kien click Phep nhan
        btnNhan.setOnClickListener(new Button.OnClickListener(){
        public void onClick(View v){
        float a,b;
        a=Float.valueOf(((EditText) findViewById(R.id.so1)).getText().toString());
        b=Float.valueOf(((EditText) findViewById(R.id.so2)).getText().toString());
        PhepTinh n=new PhepTinh(a,b);
        txvKQ.setText(String.valueOf(n.Nhan()));
        }
        }
       
        );
        
        // Su kien click Phep chia       
        btnChia.setOnClickListener(new Button.OnClickListener(){
        public void onClick(View v){
        float a,b;
        a=Float.valueOf(((EditText) findViewById(R.id.so1)).getText().toString());
        b=Float.valueOf(((EditText) findViewById(R.id.so2)).getText().toString());
        PhepTinh m=new PhepTinh(a,b);
        txvKQ.setText(String.valueOf(m.Chia()));
        }
        }
       
        );
        // Su kien click Xoa
        btnXoa.setOnClickListener(new Button.OnClickListener(){
        public void onClick(View v){
        final EditText edtSo1 = (EditText) findViewById(R.id.so1);
        edtSo1.setText("");
        final EditText edtSo2 = (EditText) findViewById(R.id.so2);
        edtSo2.setText("");
        txvKQ.setText("");
       
        }
        }        
        );
        // su kien click Thoat
        btnThoat.setOnClickListener(new Button.OnClickListener(){
        public void onClick(View v){
        AlertDialog.Builder atd = new AlertDialog.Builder(Vidu1Activity.this);
        atd.setTitle("Thong bao");
        atd.setMessage("Bạn muốn thoát ?");
        atd.setPositiveButton("Đồng ý", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        finish();        
        }
        });
        atd.show();
        }
        }        
        );
    }

}

*******

Một số tài liệu và khoá học bổ ích dành cho bạn: 

# Giáo trình: Lập Trình Android [Click để xem]

# Khoá học online:  Lập trình Android toàn tập [Click để xem]

Categories

AI (13) AI programming (1) ASP (1) Android (31) App Honeygain (4) Assembly (17) Biểu diễn thuật toán (1) Bubble-Sort (1) Bài giảng (2) Bài giảng lập trình C và Cpp (21) Bài viết hay (104) Bản đồ tư duy (1) C Plus Plus (103) C/C++ (16) CDSL phân tán (1) CSS (2) Cơ sở dữ liệu (11) Danh ngôn lập trình (1) Datamining (4) Genetic Algorithm (1) Giáo trình (2) Giải thuật tiến hóa - thuật toán di truyền (2) Google App Engine (2) Góc học tập (34) HTML (1) Hướng dẫn kiếm tiền online tại nhà (6) Hướng dẫn sử dụng Emu8086 (1) Học lập trình (131) Học lập trình C và CPP qua ví dụ (17) Java (54) Java Căn bản (6) JavaScript (5) Kỹ năng đọc hiệu quả (1) Kỹ thuật lập trình (16) Kỹ thuật đồ họa máy tính (10) Lý thuyết Cơ sở dữ liệu (2) Lý thuyết đồ thị (11) Lập trình Cơ sở dữ liệu (2) Lập trình Python (2) Lập trình căn bản (8) Lập trình hướng đối tượng với Java (7) Lập trình mobile (7) Lập trình mạng (6) Lập trình nhúng (1) Lập trình trí tuệ nhân tạo (2) ML (1) MMO (6) MS Access (1) Machine learning (2) Mạng máy tính (1) Mẹo tìm kiếm trên Google (1) Nghiên cứu khoa học (2) Ngôn ngữ lập trình (2) Những cuốn sách hay mà bạn nên đọc khi còn trẻ (1) Pascal (3) Phương pháp tính toán tối ưu (2) Phương pháp tối ưu (2) Quản lý dự án CNTT (1) SEO (1) SQL (5) Swift (9) Sách hay (4) Thiết kế Web (2) Thuật toán (51) Thuật toán Sắp Xếp -Sort (9) Thuật toán Tìm kiếm - Search (5) Thuật toán di truyền (4) Thực hành Android (2) Tin học văn phòng (5) Tiện ích máy tính (3) Toán rời rạc (13) Treo máy kiếm tiền (3) Trí tuệ nhân tạo (18) Tài liệu tham khảo (4) Tìm hiểu Blockchain (2) Tự học Android (3) Tự học Android qua ví dụ (1) Tự học JavaScript (1) Tự học lập trình (7) Tự học lập trình Android (17) Tự học lập trình C và CPP (14) Tự học lập trình java qua các ví dụ (8) XML (1) blockchain (2) bài giảng quản lý dự án CNTT (1) bài tập java (3) bài tập lập trình (4) cấu trúc dữ liệu giải thuật (15) hướng dẫn viết báo (1) học lập trình Java (11) học máy (5) hợp ngữ (8) lập trình viên (3) phưng pháp đơn hình (2) thuật toán AI (2) tài liệu CNTT miễn phí (3) tính toán tối ưu (1) tự học lập trình iOS (8) tự học lập trình python (1) ví dụ Assembly (1) Đại số gia tử và ứng dụng (1) Đồ họa (4)