Thursday, May 12, 2016

Code tính diện tích, chu vi hình chữ nhật trong android bằng C#

1 File Main.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:orientation="vertical"
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout1">
        <LinearLayout
            android:orientation="vertical"
            android:minWidth="25px"
            android:minHeight="25px"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/linearLayout2">
            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/linearLayout3"
                android:weightSum="3"
                android:layout_gravity="center_vertical">
                <TextView
                    android:text="Chiều dài"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:id="@+id/tvChieuDai"
                    android:layout_weight="1"
                    android:gravity="center_vertical"
                    android:layout_margin="5dp" />
                <EditText
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:id="@+id/edtChieuDai"
                    android:layout_weight="2"
                    android:hint="Nhập chiều dài"
                    android:layout_margin="5dp" />
            </LinearLayout>
            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/linearLayout4"
                android:weightSum="3"
                android:layout_gravity="center_vertical">
                <TextView
                    android:text="Chiều rộng"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:id="@+id/tvChieuRong"
                    android:layout_weight="1"
                    android:gravity="center_vertical"
                    android:layout_margin="5dp" />
                <EditText
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:id="@+id/edtChieuRong"
                    android:layout_weight="2"
                    android:hint="Nhập chiều rộng"
                    android:layout_margin="5dp" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="2"
        android:id="@+id/linearLayout5">
        <Button
            android:text="Tính diện tích"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/btntinhDT"
            android:layout_weight="1" />
        <Button
            android:text="Tính chu vi"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/btntinhCV"
            android:layout_weight="1" />
    </LinearLayout>
    <TextView
        android:text="Diện tích hình chữ nhật"
        android:layout_width="match_parent"
        android:layout_height="388.5dp"
        android:id="@+id/tvKetQua"
        android:layout_marginBottom="9.0dp"
        android:layout_margin="5dp"
        android:gravity="center"
        android:textSize="25dp" />

</LinearLayout>

2.Lớp MainActivity.cs
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using HinhCN.Resources.Active;
namespace HinhCN
{
    [Activity(Label = "HinhCN_LinhTran", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
       // int count = 1;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            // Get our button from the layout resource,
            // and attach an event to it
            //get id from main.axml
            EditText edtChieuDai = FindViewById<EditText>(Resource.Id.edtChieuDai);
            EditText edtChieuRong = FindViewById<EditText>(Resource.Id.edtChieuRong);

            TextView tvChieuDai = FindViewById<TextView>(Resource.Id.tvChieuDai);
            TextView tvChieuRong = FindViewById<TextView>(Resource.Id.tvChieuRong);
            TextView tvKetQua = FindViewById<TextView>(Resource.Id.tvKetQua);


            Button btntinhDienTich = FindViewById<Button>(Resource.Id.btntinhDT);
            Button btntinhChuVi = FindViewById<Button>(Resource.Id.btntinhCV);

            btntinhDienTich.Click += delegate {
                int chieudai = Int32.Parse(edtChieuDai.Text.ToString());
                int chieuRong = Int32.Parse(edtChieuRong.Text.ToString());
                HinhCNN hcn = new HinhCNN(chieudai, chieuRong);
                tvKetQua.Text = string.Format("Dien tich cua hinh chu nhat la:{0}", hcn.tinhDienTich());
            };
            btntinhChuVi.Click += delegate {
                int chieudai = Int32.Parse(edtChieuDai.Text.ToString());
                int chieuRong = Int32.Parse(edtChieuRong.Text.ToString());
                HinhCNN hcn = new HinhCNN(chieudai, chieuRong);
                tvKetQua.Text = string.Format("Dien tich cua hinh chu nhat la:{0}", hcn.tinhChuVi());
            };
        }
    }
}

3.Lớp HinhCNN.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace HinhCN.Resources.Active
{
    class HinhCNN
    {
        private int chieuRong;
        private int chieuDai;

        public HinhCNN(int cdai, int crong)
        {
            this.chieuDai = cdai;
            this.chieuRong = crong;
        }

        public void setChieuDai(int cdai)
        {
            this.chieuDai = cdai;
        }
        public int getChieuDai()
        {
            return this.chieuDai;
        }

        public void setChieuRong(int crong)
        {
            this.chieuRong = crong;
        }

        public int getChieuRong()
        {
            return this.chieuRong;
        }

        public int tinhDienTich()
        {
            return this.chieuRong * this.chieuDai;
        }
        public int tinhChuVi()
        {
            return (this.chieuDai + this.chieuRong) * 2;
        }
    }
}

Link full projecthttps://drive.google.com/drive/u/0/my-drive

0 comments:

Post a Comment