Xamarin实现一个进制转换器
先上图吧!
代码实现其实很简单:
先创建一个Transform类(用来作为为进制转换的工具)
using System;
namespace Conversion
{
class Transform
{
internal string TenToBinary(long value)//将十进制转换为二进制
{
return Convert.ToString(value, 2);
}
internal string TenToEight(long value)//将十进制转换为八进制
{
return Convert.ToString(value, 8);
}
internal string TenToSixteen(long value)//将十进制转换为十六进制
{
return Convert.ToString(value, 16);
}
internal string BinaryToEight(long value)//将二进制转换为八进制
{
return Convert.ToString(
Convert.ToInt64(value.ToString(), 2), 8);
}
internal string BinaryToTen(long value)//将二进制转换为十进制
{
return Convert.ToInt64(
value.ToString(), 2).ToString();
}
internal string BinaryToSixteen(long value)//将二进制转换为十六进制
{
return Convert.ToString(
Convert.ToInt64(value.ToString(), 2), 16);
}
internal string EightToBinary(long value)//将八进制转换为二进制
{
return Convert.ToString(
Convert.ToInt64(value.ToString(), 8), 2);
}
internal string EightToTen(long value)//将八进制转换为十进制
{
return Convert.ToInt64(
value.ToString(), 8).ToString();
}
internal string EightToSixteen(long value)//将八进制转换为十六进制
{
return Convert.ToString(
Convert.ToInt64(value.ToString(), 8), 16);
}
internal string SixteenToBinary(string value)//将十六进制转换为二进制
{
return Convert.ToString(
Convert.ToInt64(value, 16), 2);
}
internal string SixteenToEight(string value)//将十六进制转换为八进制
{
return Convert.ToString(
Convert.ToInt64(value, 16), 8);
}
internal string SixteenToTen(string value)//将十六进制转换为十进制
{
return Convert.ToUInt64(value, 16).ToString();
}
}
}
然后在MainActivity中实现功能:
using Android.App;
using Android.Widget;
using Android.OS;
using System;
namespace Conversion
{
[Activity(Label = "Conversion", MainLauncher = true,Icon ="@drawable/book")]
public class MainActivity : Activity
{
Button btnTransform;
private EditText edtInput,edtOutput;
RadioButton twoC,eightC,sixteenC, twoC2, eightC2, sixteenC2,tenC,tenC2;
Spinner spwhere;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
edtInput = FindViewById<EditText>(Resource.Id.edtinput);
edtOutput = FindViewById<EditText>(Resource.Id.edtoutput);
btnTransform = FindViewById<Button>(Resource.Id.btnTransform);
twoC = FindViewById<RadioButton>(Resource.Id.twoC);
eightC = FindViewById<RadioButton>(Resource.Id.eightC);
sixteenC = FindViewById<RadioButton>(Resource.Id.sixteenC);
twoC2 = FindViewById<RadioButton>(Resource.Id.twoC2);
eightC2 = FindViewById<RadioButton>(Resource.Id.eightC2);
sixteenC2 = FindViewById<RadioButton>(Resource.Id.sixteenC2);
tenC = FindViewById<RadioButton>(Resource.Id.tenC);
tenC2 = FindViewById<RadioButton>(Resource.Id.tenC2);
var comments = FindViewById<EditText>(Resource.Id.edtinput);
var comments2 = FindViewById<EditText>(Resource.Id.edtoutput);
//选择条
btnTransform.Click += ButtenTransform;
comments.Click += delegate
{
comments.Text = "";
};
comments2.Click += delegate
{
comments2.Text = "";
};
}
private void ButtenTransform(object sender,EventArgs args)
{
try
{
Action();//调用Action方法进行转换操作
}
catch (Exception)
{
Toast.MakeText(this, "输入错误请重试,错误", ToastLength.Short).Show();
}
}
/// <summary>
/// 进制转换
/// </summary>
private void Action()
{
if (!sixteenC.Checked)
{
long p_line_value;
if (long.TryParse(edtInput.Text, out p_line_value))//判断输入的是否是正确的赋值
{
if (tenC.Checked)
{
if (tenC2.Checked)
{
edtOutput.Text = edtInput.Text;
}
else if (twoC2.Checked)
{
edtOutput.Text = new Transform().TenToBinary(long.Parse(edtInput.Text));
}
else if (eightC2.Checked)
{
edtOutput.Text = new Transform().TenToEight(long.Parse(edtInput.Text));
}
else if(sixteenC2.Checked)
{
edtOutput.Text = new Transform().TenToSixteen(long.Parse(edtInput.Text));
}
else
{
Toast.MakeText(this, "输入错误,重试!", ToastLength.Short).Show();
}
}
else if (twoC.Checked)
{
if (tenC2.Checked)
{
edtOutput.Text = new Transform().BinaryToTen(long.Parse(edtInput.Text));
}
else if (twoC2.Checked)
{
edtOutput.Text = edtInput.Text;
}
else if (eightC2.Checked)
{
edtOutput.Text = new Transform().BinaryToEight(long.Parse(edtInput.Text));
}
else if (sixteenC2.Checked)
{
edtOutput.Text = new Transform().BinaryToSixteen(long.Parse(edtInput.Text));
}
else
{
Toast.MakeText(this, "输入错误,重试!", ToastLength.Short).Show();
}
}
else if(eightC.Checked)
{
if (tenC2.Checked)
{
edtOutput.Text = new Transform().EightToTen(long.Parse(edtInput.Text));
}
else if (twoC2.Checked)
{
edtOutput.Text = new Transform().EightToBinary(long.Parse(edtInput.Text));
}
else if (eightC2.Checked)
{
edtOutput.Text = edtInput.Text;
}
else if (sixteenC2.Checked)
{
edtOutput.Text = new Transform().EightToSixteen(long.Parse(edtInput.Text));
}
else
{
Toast.MakeText(this, "输入错误,重试!", ToastLength.Short).Show();
}
}
}
}
else
{
if (tenC2.Checked)
{
edtOutput.Text = new Transform().SixteenToTen(edtInput.Text);
}
else if (twoC2.Checked)
{
edtOutput.Text = new Transform().SixteenToBinary(edtInput.Text);
}
else if (eightC2.Checked)
{
edtOutput.Text = new Transform().SixteenToEight(edtInput.Text);
}
else if (sixteenC2.Checked)
{
edtOutput.Text = edtInput.Text;
}
else
{
Toast.MakeText(this, "输入错误,重试!", ToastLength.Short).Show();
}
}
}
}
}
布局代码送上:
<?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="horizontal"
android:minWidth="25px"
android:minHeight="80px"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayoutForName">
<TextView
android:text="输入:"
android:layout_width="81.5dp"
android:layout_height="match_parent"
android:id="@+id/textViewNme"
android:textAllCaps="true"
android:textSize="20dp"
android:gravity="center" />
<EditText
android:layout_width="291.0dp"
android:layout_height="match_parent"
android:id="@+id/edtinput" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout1"
android:paddingTop="10dp"
android:paddingEnd="20dp"
android:gravity="center">
<RadioGroup
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/radioGroup1"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="十进制"
android:textSize="15dp"
android:id="@+id/tenC" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="二进制"
android:textSize="15dp"
android:id="@+id/twoC" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="八进制"
android:textSize="15dp"
android:id="@+id/eightC" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="十六进制"
android:textSize="15dp"
android:id="@+id/sixteenC" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout2">
<TextView
android:text="输出:"
android:layout_width="81.5dp"
android:layout_height="match_parent"
android:id="@+id/textViewName"
android:textAllCaps="true"
android:textSize="20dp"
android:gravity="center" />
<EditText
android:layout_width="291.0dp"
android:layout_height="match_parent"
android:id="@+id/edtoutput" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout2"
android:paddingTop="10dp"
android:paddingEnd="20dp"
android:gravity="center">
<RadioGroup
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/radioGroup2"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="十进制"
android:textSize="15dp"
android:id="@+id/tenC2" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="二进制"
android:textSize="15dp"
android:id="@+id/twoC2" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="八进制"
android:textSize="15dp"
android:id="@+id/eightC2" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="十六进制"
android:textSize="15dp"
android:id="@+id/sixteenC2" />
</RadioGroup>
</LinearLayout>
<Button
android:text="转 换"
android:layout_width="match_parent"
android:layout_height="75.0dp"
android:id="@+id/btnTransform"
android:textSize="25dp"
android:layout_marginTop="13.0dp" />
</LinearLayout>
Xamarin实现一个进制转换器相关推荐
- python用thinker库制作一个进制转换器(可打包exe)
进制类型分为: 二进制 字母B表示 八进制 字母O表示 十进制 字母D表示 十六机制 字母H表示 进制转换之间很麻烦,还得计算,如果可以做一个进制转换器多nice,其实也不难,就利用一个tkinter ...
- 练习:自撸整数进制转换器(二、八、十六进制转十进制)
我的CSDN主页 My Python 学习个人备忘录 我的HOT博 整数进制转换器(二.八.十六进制转十进制) 题目 代码运行效果 我的解题思路 转换器完整代码 题目 跟着寒佬整数进制转换,生发自 ...
- C++实现数字进制转换器
C++实现数字进制转换器 刷题中遇到一些进制转化的题目,由于从十进制转化为其他进制(十以内)方法完全相同,因此总结为一个进制转换器作为练习,当作一个练习记录.代码没有经过优化,请诸位大佬轻喷. 十进制 ...
- android进制之间的转换器,一个Android平台的16进制转换器
<一个Android平台的16进制转换器>由会员分享,可在线阅读,更多相关<一个Android平台的16进制转换器(1页珍藏版)>请在人人文库网上搜索. 1.一个An droi ...
- 用java编写进制转换器_JAVA 简单进制转换器
JAVA作业写了个进制转换器...写出来的东西还蛮怪的 /* * Author:graykido * Coding:GBK * */ package Caculater; import javafx. ...
- C++ 进制转换器:二进制、十进制、十六进制
做了一个C++进制转换器,支持10转2.10转16.2转10.16转10.10转2.2转16,解析都写在代码里哦! #include <bits/stdc++.h> using names ...
- 进制转换器java程序_Java实现复杂的进制转换器功能示例
本文实例讲述了Java实现复杂的进制转换器功能.分享给大家供大家参考,具体如下: 这是用java写的进制转换器,包括10.2.8.16进制之间共12种的相互转换.输入一个要转换的数之后,按提示选择所采 ...
- 进制转换器的c代码实现
1.很久没有更新博客了 这次想要记录分享一下在这段学习过程中实现的进制转换器 c语言代码 里面运用到了一些数据结构和递归的相关知识 也算是一个简单的综合运用吧 2.核心思想是将一切进制转换为10进制 ...
- 万能进制转换器——栈应用
万能进制转换器--栈应用 前言 一.设计思路 1.字符串和数字通过ASCII码来转换 2.先将用户输入的数字转换为10进制,再由10进制转为其他进制 二.具体步骤的实现 1.准备步骤 1.执行步骤 ( ...
最新文章
- 社交软件这样赚钱:line表情包卖了2亿多美元
- 2019年秋计算机管理工作总结,年段工作总结(2019秋高一上)
- 用eclips连hadoop报Unknown protocol to job tracker: org.apache.hadoop.hdfs.protocol.ClientProtoco
- 臭名昭著的Java错误和陷阱
- 常州win8如何禁用应用商店_Win8系统当中Windows defnedder安全软件应该如何禁用?...
- Spring Cloud微服务之子模块的创建(二)
- 伪样式:hover ,:active,:focus
- 长庆企业信息化管理课件_会博通浅谈企业档案管理信息化的必要性和优势
- python获取键盘按键键值_python获取键盘输的值
- 苹果 macOS 11 Big Sur初体验, 升还是不升?
- 分类算法学习(四)——决策树算法的原理及简单实现
- XML语言以及DTD的详解(方立勋javaweb)
- 如何用pdfFactory新建打印机并设置属性
- 在线免费caj转Word,不用安装软件
- java pack unpack_解释一下pack和unpack
- 如何给CSDN博客添加微信公众号二维码或自定义栏目
- 实验六:熟悉Hive的基本操作
- 国内最火的内存数据库,110000 QPS 没有之一!
- Source Insight使用教程(一):导入工程
- 套接字属性函数getsockopt和setsockopt