Showing posts with label Android Xamarin. Show all posts
Showing posts with label Android Xamarin. Show all posts
Wednesday, October 26, 2016
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 project: https://drive.google.com/drive/u/0/my-drive
<?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 project: https://drive.google.com/drive/u/0/my-drive
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;
}
}
}