• Mono的简单例子


    一直对移动端开发有些兴趣,但苦于不会Java,好在终于找到了个好玩的。

    安装方法略了,先建立一个玩玩

    不多说,贴代码了,需要注意的只有些JAVA和C#写法不太一样的地方,不细介绍了,因为没什么经验,乱试的,所以也没什么规范,随便看看就好

    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;
    using Java.Util;
    
    namespace AndroidHotelServiceTest
    {
        [Activity(Label = "My Activity")]
        public class ActivityCalendar : Activity
        {
            protected override Dialog OnCreateDialog(int id)
            {
                if (id == 1)
                {
                    return new DatePickerDialog(this, new DDialogLisetener(this), 2013, 11, 11);
                }
                return base.OnCreateDialog(0);
            }
    
            protected override void OnCreate(Bundle bundle)
            {
                base.OnCreate(bundle);
                // Create your application here
                SetContentView(Resource.Layout.CalendarView);
                Button button = FindViewById<Button>(Resource.Id.btClose);
                button.Click += delegate
                {
                    Intent intent = new Intent();
                    intent.SetClass(this, typeof(ActivityHotel));
                    StartActivity(intent);
                };
    
                ShowDialog(1);
    
            }
        }
    
        public class DDialogLisetener : DatePickerDialog.IOnDateSetListener
        {
            private Context _context;
            public DDialogLisetener(Context context)
            {
                _context = context;
            }
    
            public void OnDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
            {
                String sDayOfWeek = getDayOfWeek(year, monthOfYear, dayOfMonth);
                //Toast.makeText(CreateParty.this, "sdf", Toast.LENGTH_LONG).show();
                int m_nYear = year;
                int m_nMonth = monthOfYear + 1;
                int m_nDay = dayOfMonth;
                Toast.MakeText(_context, "ddd", ToastLength.Long).Show();
                Toast.MakeText(_context, m_nYear + "年" + m_nMonth + "月" + m_nDay + "日 ", ToastLength.Long).Show();
            }
    
            private string getDayOfWeek(int tmpYear, int tmpMonth, int tmpDay)
            {
                String myWeek = null;
                String sYear = tmpYear.ToString();
                // 取年的后两位
                String sYearTwo = sYear.Substring(sYear.Length - 2);
    
                int y = tmpYear;
    
                int m = tmpMonth + 1;
                int c = 20;
                int d = tmpDay;
                int w = (y + (y / 4) + (c / 4) - 2 * c
                        + (26 * (m + 1) / 10) + d - 1) % 7;
    
    
                switch (w)
                {
                    case 0:
                        myWeek = "日";
                        break;
                    case 1:
                        myWeek = "一";
                        break;
                    case 2:
                        myWeek = "二";
                        break;
                    case 3:
                        myWeek = "三";
                        break;
                    case 4:
                        myWeek = "四";
                        break;
                    case 5:
                        myWeek = "五";
                        break;
                    case 6:
                        myWeek = "六";
                        break;
                    default:
                        break;
                }
                return myWeek;
    
            }
    
            public void Dispose()
            {
                this.Dispose();
            }
    
            public IntPtr Handle
            {
                get { return IntPtr.Zero; }
            }
        }
    }
    

      参考了不少网上的代码

    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;
    using Android.Media;
    using Android.Content.Res;
    using Java.IO;
    
    namespace AndroidHotelServiceTest
    {
        public class BeepManager
        {
            private static Activity m_CurrentActivity;
            private static MediaPlayer m_MediaPlayer = null;
    
            private static MediaPlayer BuildMediaPlayer(Context context)
            {
                MediaPlayer player = new MediaPlayer();
                player.SetAudioStreamType(Android.Media.Stream.Music);
                player.Completion += new EventHandler(mediaplayer_Completion);
    
                AssetFileDescriptor assetfiledescriptor = context.Resources.OpenRawResourceFd(0x7f040000);
                try
                {
                    player.SetDataSource(assetfiledescriptor.FileDescriptor, assetfiledescriptor.StartOffset, assetfiledescriptor.Length);
                    assetfiledescriptor.Close();
                    player.SetVolume(0.1F, 0.1F);
                    player.Prepare();
                    m_MediaPlayer = player;
                }
                catch (IOException ioexception)
                {
                    player = null;
                }
    
                return player;
            }
    
            static void mediaplayer_Completion(object sender, EventArgs e)
            {
                m_MediaPlayer.SeekTo(0);
            }
    
            public static void PlayBeepSound(Activity activity)
            {
                m_CurrentActivity = activity;
                if (m_MediaPlayer == null)
                {
                    m_MediaPlayer = BuildMediaPlayer(m_CurrentActivity);
                }
                m_MediaPlayer.Start();
            }
    
            public static void Stop()
            {
                if (m_MediaPlayer != null)
                {
                    m_MediaPlayer.Stop();
                    m_MediaPlayer = null;
                }
            }
    
            //private void prepareToPlay()
            //{
            //    try
            //    {
            //        //获取当前音频流的路径后我们需要通过MediaPlayer的setDataSource来设置,然后调用prepareAsync()来完成缓存加载  
            //        String path = pathList.get(currPosition);
            //        player.setDataSource(path);
            //        //之所以使用prepareAsync是因为该方法是异步的,因为访问音频流是网络操作,在缓冲和准备播放时需要花费  
            //        //较长的时间,这样用户界面就可能出现卡死的现象  
            //        //该方法执行完成后,会执行onPreparedListener的onPrepared()方法。  
            //        player.prepareAsync();
    
            //    }
            //    catch (IllegalArgumentException e)
            //    {
            //        // TODO Auto-generated catch block  
            //        e.printStackTrace();
            //    }
            //    catch (IllegalStateException e)
            //    {
            //        // TODO Auto-generated catch block  
            //        e.printStackTrace();
            //    }
            //    catch (IOException e)
            //    {
            //        // TODO Auto-generated catch block  
            //        e.printStackTrace();
            //    }
            //}  
        }
    }

    调用WCF验证登陆

    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;
    using System.ServiceModel;
    using System.Threading;
    
    namespace AndroidHotelServiceTest
    {
        [Activity(Label = "登陆", MainLauncher = true)]
        public class ActivityLogin : Activity
        {
            protected override void OnCreate(Bundle bundle)
            {
                base.OnCreate(bundle);
                SetContentView(Resource.Layout.Login);
                // Create your application here
    
                Button button = FindViewById<Button>(Resource.Id.login);
                EditText txtUser = FindViewById<EditText>(Resource.Id.edtuser);
                EditText txtPassword = FindViewById<EditText>(Resource.Id.edtpsd);
    
                button.Click += delegate
                {
                    Login(txtUser.Text, txtPassword.Text);
                };
            }
    
            public void Login(string user, string password)
            {
                BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
                var timeout = new TimeSpan(0, 1, 0);
                binding.SendTimeout = timeout;
                binding.OpenTimeout = timeout;
                binding.ReceiveTimeout = timeout;
    
                IHotelService service = ChannelFactory<IHotelService>.CreateChannel(binding,
                    new EndpointAddress("http://172.25.16.50:90/HotelService.svc"));
                string loginUser = service.Login(user, password);
                if (!string.IsNullOrEmpty(loginUser))
                {
                    Intent intent = new Intent();
                    intent.SetClass(this, typeof(ActivityHotel));
                    intent.PutExtra("User", user);
                    StartActivity(intent);
                }
            }
        }
    }

    总之,大概就这样了,页面布局什么的也没什么特别的。

    <?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:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="账号:" />
            <EditText
                android:layout_width="240dip"
                android:layout_height="wrap_content"
                android:text="aaa"
                android:id="@+id/edtuser" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="密码:" />
            <EditText
                android:layout_width="240dip"
                android:layout_height="wrap_content"
                android:password="true"
                android:id="@+id/edtpsd" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal">
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:orientation="vertical">
                <CheckBox
                    android:id="@+id/checkbox"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="记住密码" />
            </LinearLayout>
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical">
                <Button
                    android:id="@+id/login"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:text="登录" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

    至于服务端,就是VS自动生成的代码加了一个登陆判断而已

    [ServiceContract]
        public interface IHotelService
        {
            [OperationContract]
            string Login(string user, string password);
        }
  • 相关阅读:
    实验8 统计和输出一次考试中的最高分、最低分、平均分,以及在90~100、 80~89、 70~79、 60~69和60分以下各分数段的人数。程序代码如下(可复制):
    实验7(3)输入x后,根据下式计算并输出y值
    实验7
    实验7(4)编写程序,输入一批学生的成绩,遇0或负数则输入结束,要求统计并输出优秀(大于85)、通过(60~84)和不及格(小于60)的学生人数。
    输入n个字符
    实验6-1 输入n个整数,将它们存入数组a中。输出最大值和它所对应的下标
    作业3-5 输入五级制成绩(A-E),输出相应的百分制成绩(0-100)区间,要求使用 switch语句。例如,输入 A,输出 90-100。五级制成绩对应的百分制成绩区间为:A(90-100)、B(80-89)、C(70-79)、D(60-69)和 E(0-59)。
    作业3-6 查询水果的单价。有 4 种水果,苹果(apples)、梨(pears)、桔子(oranges)和葡萄(grapes),单价分别是 3.00 元/公斤,2.50 元/公斤,4.10 元/公斤和 10.20 元/公斤。在屏幕上显示以下菜单(编号和选项),用户可以连续查询水果的单价,当查询次数
    作业3-4 循环输入多个年份 year,判断该年是否为闰年。判断闰年的条件是:能被 4 整除但不能被100 整除,或者能被 400 整除。输入-1退出程序执行
    作业3-3 输入 15 个字符,统计其中英文字母、空格或回车、数字字符和其他字符的个数
  • 原文地址:https://www.cnblogs.com/saaav/p/3585896.html
Copyright © 2020-2023  润新知