Thursday, May 12, 2016

Code chuyển năm dương lịch sang năm âm lịch 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"
    android:minWidth="25px"
    android:minHeight="25px">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="58.5dp"
        android:id="@+id/myLayout"
        android:weightSum="3"
        android:minHeight="10dp"
        android:layout_marginTop="2dp"
        android:layout_gravity="fill_vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/textView1"
            android:layout_weight="1"
            android:text="Nhập năm"
            android:layout_margin="7dp"
            android:textStyle="italic"
            android:layout_marginTop="9.0dp" />
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/edtNamDuong"
            android:layout_weight="2"
            android:text="0" />
    </LinearLayout>
    <Button
        android:text="Chuyển đổi"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnChuyenDoi" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="44.5dp"
        android:id="@+id/txtvNamAm" />
</LinearLayout>

2.Lớp Main  MainActivity.cs
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using CHUYENDOI_NAMDUONGLICH;
namespace CHUYENDOI_NAMDUONGLICH
{
    [Activity(Label = "CHUYENDOI_NAMDUONGLICH", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        Button chuyenDoi;
        TextView namAm;
        EditText namD;
        
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

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

            chuyenDoi = FindViewById<Button>(Resource.Id.btnChuyenDoi);
            namAm = FindViewById<TextView>(Resource.Id.txtvNamAm);
            namD = FindViewById<EditText>(Resource.Id.edtNamDuong);

           chuyenDoi.Click += delegate 
           {
               int namNhap = 0;
               try
               {
                   namNhap = Int32.Parse(namD.Text);
               }catch(Exception)
               {
                   namD.Text = "0";
               }         
               
              
               Nam_Duong nam = new Nam_Duong(namNhap);
               namAm.Text = string.Format("Năm âm lịch: {0} {1}",nam.getCan(),nam.getChi());
           };
        }
    }
}

3..Lớp Năm Nam_Duong.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 CHUYENDOI_NAMDUONGLICH
{
    class Nam_Duong
    {
        int Nam;
        string can;
        string chi;
        public Nam_Duong(int nam)
        {
            this.Nam = nam;
        }
        public string getCan()
        {
            int a = this.Nam % 10;
            switch(a)
            {
                case 0:
                {
                        this.can = "Canh";
                        break;
                }
                case 1:
                {
                        this.can = "Tân";
                        break;
                    }
                    case 2:
                {
                        this.can = "Nhâm";
                        break;
                    }
                case 3:
                {
                        this.can = "Qúy";
                        break;
                    }
                case 4:
                    {
                        this.can = "Giáp";
                        break;
                    }
                case 5:
                {
                        this.can = "Ất";
                        break;
                    }
                case 6:
                {
                        this.can = "Bính";
                        break;
                    }
                case 7:
                {
                        this.can = "Đinh";
                        break;
                    }
                case 8:
                {
                        this.can = "Mậu";
                        break;
                    }
                case 9:
                {
                        this.can =" Kỷ";
                        break;
                    }
                default:
                {
                        this.can = "Nhập ";
                        break;
                    }
            }
            return this.can;
        }
        public string getChi()
        {
            int b = Nam % 12;
            switch (b)
            {
                case 0:
                    {
                        this.chi = "Thân";
                        break;
                    }
                case 1:
                    {
                        this.chi = "Dậu";
                        break;
                    }
                case 2:
                    {
                        this.chi = "Tuất";
                        break;
                    }
                case 3:
                    {
                        this.chi = "Hợi";
                        break;
                    }
                case 4:
                    {
                        this.chi = "Tý";
                        break;
                    }
                case 5:
                    {
                        this.chi = "Sửu";
                        break;
                    }
                case 6:
                    {
                        this.can = "Dần";
                        break;
                    }
                case 7:
                    {
                        this.chi = "Mão";
                        break;
                    }
                case 8:
                    {
                        this.chi = "Thìn";
                        break;
                    }
                case 9:
                    {
                        this.chi = " Tỵ";
                        break;
                    }
                case 10:
                {
                        this.chi = "Ngọ";
                        break;
                    }
                case 11:
                    {
                        this.chi = "Mùi";
                        break;
                    }
                default:
                    {
                        this.chi = "Sai ";
                        break;
                    }
            }
            return this.chi;
        }
    }
}

0 comments:

Post a Comment