Blog chia sẻ kinh nghiệm lập trình Android

Xuyên suốt quá trình học cơ bản từ hôm  đầu tới giờ, hôm nay mình sẽ demo cho các bạn bài tính tổng hai số nguyên.
 Đầu tiên các bạn tạo project có tên là SumAndroidDemo




 Sau đó các bạn tạo giao diện như sau :



Code : 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFF34"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Demo tính tổng "
        android:textSize="25dp" />

    <LinearLayout
        android:id="@+id/linnerlayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="70"
            android:text="Nhập số A  = " />

        <EditText
            android:id="@+id/edta"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="30"
            android:background="#FFFFF8"
            android:ems="10" >

            <requestFocus />
        </EditText>
    </LinearLayout>
 <LinearLayout
        android:id="@+id/linnerlayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="70"
            android:text="Nhập số B = " />

        <EditText
            android:id="@+id/edtb"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="30"
            android:background="#FFFFF8"
            android:ems="10" >

            <requestFocus />
        </EditText>
    </LinearLayout>
    <TextView
        android:id="@+id/tvSum"
        android:background="#FFFFF3"
        android:layout_marginTop="40dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Tổng hai số vừa nhập là : " />

    <LinearLayout
        android:id="@+id/linnearlayout3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:background="#0033FF"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btsum"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="10"
            android:text="Cộng" />
  <Button
            android:id="@+id/btclear"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="10"
            android:text="Xóa" />
        <Button
            android:id="@+id/btexit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="10"
            android:text="Exit" />
    </LinearLayout>

</LinearLayout>

 _______________________________________


Các bạn chú ý các ID edta,edtb,btsum,btclear,btexit,tvsum.

Sau đó các bạn kết nối các id trong file.xml với main class.

Code :


Chạy ứng dụng và thưởng thức : các bạn nghiên cứu code và tìm hiểu thêm có những chi tiết nhỏ rất hay.






chúc các bạn thành công.
Mọi thắc mắc các bạn liên hệ mình qua địa chỉ Gmail : svk10acntt@gmail.com hoặc Fb: John Ly Phạm



package com.example.sumandroiddemo;

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

public class MainActivity extends Activity {
   EditText edta,edtb;
   TextView tvSum;
   Button btSum,btClear,btExit;
   Context context = this;
   int a,b,sum;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  // kết nối id xml với class
  edta =(EditText)findViewById(R.id.edta);
  edtb =(EditText)findViewById(R.id.edtb);
  btClear =(Button)findViewById(R.id.btclear);
  btExit =(Button)findViewById(R.id.btexit);
  btSum = (Button)findViewById(R.id.btsum);
  tvSum=(TextView)findViewById(R.id.tvSum);
  
  // gọi các hàm
  Tinh_tong();
  clear_data();
  exit_app();
  
 }

 private void exit_app() {
  // TODO Auto-generated method stub
  btExit.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    AlertDialog.Builder b = new  AlertDialog.Builder(context);
    b.setTitle("Thoát");
    b.setMessage("thoát ứng dụng");
    b.setNegativeButton("No", null);
    b.setNeutralButton("Yes", new DialogInterface.OnClickListener() {
     
     @Override
     public void onClick(DialogInterface arg0, int arg1) {
      // TODO Auto-generated method stub
      System.exit(0);
     }
    });
    b.show();
   }
  });
 }

 public void Tinh_tong()
 {
  btSum.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {
    // kiểm tra nhập số liệu có đúng kiểu nguyên không
    try {
     a =Integer.parseInt(edta.getText()+"");
    } catch (Exception e) {
     edta.setText("");
     edta.requestFocus();
     Toast.makeText(getApplication(), "Nhập số tự nhiên", 1).show();
     return;
    }
    
    try {
     b=Integer.parseInt(edtb.getText()+"");
    } catch (Exception e) {
     edtb.setText("");
     edtb.requestFocus();
     Toast.makeText(getApplication(), "Nhập số tự nhiên", 1).show();
     return;
    }
    // bắt đầu tính toán
   // tổng hai số
    sum = a+b;
   //hiện thị tổng
    tvSum.setText(tvSum.getText().toString()+sum);
    
    
   }
  });
 }
 public void clear_data()
 {
  btClear.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {
    edta.setText("");
    edtb.setText("");
    a=b=sum=0;
    tvSum.setText("Tổng hai số vừa nhập là : ");
   }
  });
 }
}

0 comments:

Post a Comment

http://knlaptrinhandroid.blogspot.com/

 
Top