Menu

2/17/19

Demo03- Phép tính đơn giản.


Yêu cầu bài toán:
-Nhập vào 2 số nguyên a, và b. Xuất kết quả khi thực hiện phép toán cộng-trừ.


Xây dựng text trong file string.xml.

<resources>
    <string name="app_name">PhepToanDonGian</string>
    <string name="txt1">Nhap So Thu 1</string>
    <string name="txt2">Nhap So Thu 2</string>
    <string name="ex1">ex: 6</string>
    <string name="ex2">ex: 8</string>
    <string name="btn1">Tong</string>
    <string name="btn2">Hieu</string>
    <string name="txtkq">Ket Qua:</string>
</resources>

Thiết kế giao diện file layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/txtnhapso1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:layout_margin="10dp"
        android:text="@string/txt1" />

    <EditText
        android:id="@+id/editnhapso1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        tools:ignore="Autofill"
        android:hint="@string/ex1"
        android:ems="5"
        android:inputType="number" />
    <TextView
        android:id="@+id/txtnhapso2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:textSize="30sp"
        android:text="@string/txt2" />

    <EditText
        android:id="@+id/editnhapso2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        tools:ignore="Autofill"
        android:hint="@string/ex2"
        android:ems="5"
        android:inputType="number" />

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAllCaps="false"
        android:layout_margin="10dp"
        android:text="@string/btn1" />
    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAllCaps="false"
        android:layout_margin="10dp"
        android:text="@string/btn2" />

    <TextView
        android:id="@+id/txtkq"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:layout_margin="10dp"
        android:text="@string/txtkq" />

</LinearLayout>

Code mã nguồn tính toán: source.java


package com.example.demo03;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private Button btn1,btn2;
    EditText so1,so2;
    TextView kq;
    int tong,hieu;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //binding doi tuong
        so1=(EditText)findViewById(R.id.editnhapso1);
        so2=(EditText)findViewById(R.id.editnhapso2);
        kq=(TextView)findViewById(R.id.txtkq);
        btn1=(Button)findViewById(R.id.btn1);
        btn2=(Button)findViewById(R.id.btn2);

        btn1.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                String s1=so1.getText().toString();
                String s2=so2.getText().toString();
                tong=Integer.parseInt(s1)+Integer.parseInt(s2);
                kq.setText("Ket Qua "+tong);

            }
        });
        btn2.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                String s1=so1.getText().toString();
                String s2=so2.getText().toString();
                hieu=Integer.parseInt(s1)-Integer.parseInt(s2);
                kq.setText("Ket Qua "+hieu);

            }
        });

    }
}




































No comments:

Post a Comment