Để 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;
}
}
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();
}
}
);
}
}
/**********/
>>> Download Project Vidu1 tại đây
Lưu ý: Sau 5s, click BỎ QUA QUẢNG CÁO (SKIP AD)
0 nhận xét:
Đăng nhận xét