Android(Xamarin)之旅(三)

简介: 原文:Android(Xamarin)之旅(三)前面两篇说到了Xamarin的安装和一些简单的控件,今天来说说一些对话框和提示信息,以及简单的布局元素。 一、对话框和提示信息   一、对话框       我们首先从简单的对话框开始。
原文: Android(Xamarin)之旅(三)

前面两篇说到了Xamarin的安装和一些简单的控件,今天来说说一些对话框和提示信息,以及简单的布局元素。

一、对话框和提示信息

  一、对话框

      我们首先从简单的对话框开始。

      1、普通对话框

      在android里面,对话框用的是AlertDialog,这个呢,其实就和winform里面的MessageBox一样的。最简单的

AlertDialog.Builder ad_build = new AlertDialog.Builder(this)
                              .SetTitle(Resource.String.warming)//标题(警告)
                              .SetMessage(Resource.String.info)//获取本地string.xml定义的数据
                              .SetNegativeButton("确定", this)
                              .SetPositiveButton("取消", this)
                              .SetNeutralButton("中间按钮", this)
                              .SetIcon(Android.Resource.Drawable.StatSysWarning);
                    ad_build.Show();

       其中,SetNeutralButton这里是设置的一个中间按钮,这个东西,可以有,也可以没有,代码可以直接添加到程序里面运行即可。在代码中,我们可以看到提示信息里面的,  获取本地strings.xml定义的数据。我们可以看下本地的xml数据。

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="myapp">爺的APP</string>
  <string name="info">您确定结束本次任务?</string>
  <string name="yesorno">确定要退出吗?</string>
  <string name="warming">警告</string>
  <string name="jiazai">正在加载……</string>
  <string name="Img_Info">您有一个未接电话</string>
  <string name="MyToast">自定义提示信息</string>
</resources>

      在这里,不得不说一下,这里的SetIcon的问题,这里的Android.Resource.Drawable这个是系统自带的图片,可能我们有时候需要去访问本地自己的图片。

      在dialog显示gif图片 由于dialog不容易取到里面空间对象,推荐使用透明样式的activity,只需把该activity的样式设置为透明样式 即android:theme="@android:style/Theme.Translucent",同时在oncreat()方法的setcontenview()之前设置requestWindowFeature(Window.FEATURE_NO_TITLE);去掉标题. 播放gif图片 由于android控件不支持播放gif。推荐使用webview 里面放入html中含有img标签 src便是图片的地址 可以使网络地址 也可以是本地地址 然后webview加载该html即实现了播放

      2、单选对话框

      单选对话框,还用的是AlertDialog

 AlertDialog.Builder ad_build = new AlertDialog.Builder(this)
                              .SetTitle(Resource.String.warming)//标题(警告)
                              .SetSingleChoiceItems(new string[] { "中国", "日本", "韩国" }, 0, this)//自定义的单选数组
                              .SetNegativeButton("确定", this)
                              .SetPositiveButton("取消", this)
                              .SetIcon(Android.Resource.Drawable.StatSysWarning); ad_build.Show();

       在这里,任然可以添加中间按钮,直接在后面继续加点,加Set就可以,但是单选要实现 IDialogInterfaceOnClickListener 接口,通过这个接口,我们可以获取到现在选择的到底是哪一个的值。其实,单选就是多了一个SetSingleChoiceItems这个参数,然后传值就可以。

        /// <summary>
        /// 单选和普通对话框
        /// </summary>
        /// <param name="dialog"></param>
        /// <param name="which"></param>
        public void OnClick(IDialogInterface dialog, int which)
        {
            Toast.MakeText(this, which + "", ToastLength.Short).Show();
        }

       这里的which就是选择的是哪一个的值,获取到值一般来说才是最重要的,我们才可以继续其他的事情。

      3、多选对话框

      多选对话框,还用的是AlertDialog

  AlertDialog.Builder ad_build = new AlertDialog.Builder(this)
                             .SetTitle(Resource.String.warming)//标题(警告)
                             .SetMultiChoiceItems(new string[] { "中国", "日本", "韩国" }, new bool[] { false, true, true }, this)//多选自定义数组
                             .SetNegativeButton("确定", this)
                             .SetPositiveButton("取消", this)
                             .SetIcon(Android.Resource.Drawable.StatSysWarning);
                    ad_build.Show();

       其中,多选框实现的 IDialogInterfaceOnMultiChoiceClickListener 是这个接口。

     /// <summary>
        /// 多选接口的实现
        /// </summary>
        /// <param name="dialog"></param>
        /// <param name="which"></param>
        /// <param name="isChecked"></param>
        public void OnClick(IDialogInterface dialog, int which, bool isChecked)
        {
            Toast.MakeText(this, which.ToString() +"    "+ isChecked.ToString(), ToastLength.Short).Show();
        }

       同样的,这里的which是在这个多选框中的唯一ID,后面的isChecked,是否选择,通过这些,我们就可以获取到很多信息了。

      4、正在加载对话框

      正在加载用的是ProgressDialog 这个方法,这个方法同样可以 Builder,但是和SetIcon一样,如果想采取自定义的图片,同样需要前面的图片自定义的办法。

          ProgressDialog p_dialog = new ProgressDialog(this);
          p_dialog.SetMessage("正在加载……");
          p_dialog.Show();            

       这个效果就是登陆或者其他的那个,如果这里用 ProgressDialog.Builder 也是可以,但是要自定义显示信息,包括图片信息等等。

      5、自定义对话框

      这里自定义对话框用的还是AlertDialog,但是不同的是,自定义的对话框,要注意。自定义对话框,要完全自定义布局,也就是说,要完全定义所有的相关信息,这就相当于我们做web的时候,填出一个提示框一样,在Android里面,要完全弹出自定义对话框,哪就需要View,因为所有的界面都是View,直接右键添加一个Android Layout就可以,哇咔咔,继续开始设计。

      我的界面是这样定义的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="系统提示"
        android:background="#0094FF"
        android:textColor="#ffffff" />
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:background="#848484">
    <!--@android:drawable/stat_notify_missed_call 引用的是系统的图片-->
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/stat_notify_missed_call" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="一个未接电话" />
    </LinearLayout>
</LinearLayout>

      OK,这里要注意一点 关于 ImageView 的 src 的问题 android:src="@drawable/myapk" 这么写,引用的本地定义的myapk的图片,最好能是 png、jpg等等此类的,要是gif的好像还是要重新加载一下,这里的引用也就是自己在 drawable 文件夹添加的图片的 名字。 @android:drawable/stat_notify_missed_call 这么写就是引用的 Android SDK文件夹下面的 drawable 的文件,这些文件你得先找到你自己的文件安装路径,也就是你定义的SDK的安装路径,找到安装路径之后,platformsandroid-15→data→res→drawable-ldpi 在这个文件夹下面,你就可以看到很多图片了,如果实在找不到,你还是用Android Studio安装的话,我告诉一个好消息,SDK Manager (sdk管理器)或者 AVD Manager (虚拟机管理器)的快捷方式,找到安装的根目录,这个可以做到吧。

      如图,找到对应的文件夹,依次按照platforms→android-15→data→res→drawable-ldpi 这个顺序往下点击就可以了,你就能看到你想要的了。

      后台代码:

                    View view = LayoutInflater.From(this).Inflate(Resource.Layout.MyDialog, null);
                    AlertDialog.Builder a_bulid = new AlertDialog.Builder(this);
                    a_bulid.SetMessage("自定义对话框").SetView(view);
                    a_bulid.Show();

      先用View来接收我刚定义的界面,然后给这个界面设置一个标题,然后直接用 AlertDialog 直接 Show 就可以,其中 LayoutInflater 这就是一点要注意的,这个和本身的 FindViewById就是一个相同的意思,一个找布局文件,一个找界面元素。

      6、列表对话框

      列表对话框用的还是AlertDialog

      

 AlertDialog.Builder ad_build = new AlertDialog.Builder(this)
                             .SetTitle(Resource.String.warming)//标题(警告)
                             .SetItems(new string[] { "中国", "日本", "韩国" }, this)
                             .SetNegativeButton("确定", this)
                             .SetPositiveButton("取消", this)
                             .SetIcon(Android.Resource.Drawable.StatSysWarning);
                    ad_build.Show();

       这里不同的就是列表对话框用的是 SetItems 这个属性

    二、提示信息

      1、普通提示信息

                    var item = Toast.MakeText(this, Resource.String.info, ToastLength.Short);
                    //设置垂直水平居中
                    item.SetGravity(GravityFlags.CenterHorizontal | GravityFlags.CenterVertical, 0, 0);
                    item.Show();

       这里的其实没有什么注意的,就是一个 SetGravity 设置显示的位置的属性。

      2、含图片提示信息

            var item = Toast.MakeText(this, Resource.String.Img_Info, ToastLength.Short);
                    //创建一个图片视图
                    ImageView iv = new ImageView(this);
                    iv.SetImageResource(Android.Resource.Drawable.StatNotifyMissedCall);
                    //得到Toast布局(强制改变为线型布局)
                    LinearLayout toastView = (LinearLayout)item.View;
                    //设置内容显示位置
                    toastView.SetGravity(GravityFlags.Center);
                    //设置布局的方向
                    //
                    //Orientation.Horizontal 居于屏幕下方
                    //Orientation.Horizontal | Orientation.Vertical
                    //
                    toastView.Orientation = Orientation.Horizontal;
                    //给布局添加一个视图,并且设置位置
                    toastView.AddView(iv, 0);
                    //显示Toast
                    item.Show();

      3、完全自定义提示信息

                    View view = LayoutInflater.From(this).Inflate(Resource.Layout.MyDialog, null);
                    Toast toast = new Toast(this);
                    toast.View = view;
                    toast.Show();

       看到这里,相信大家都有一个简单的了解了,我做了一个简单的反思,就是Android的这个东西,当你要呈现一个新的元素或者其他的任务之类的,都需要去单独接受,感觉和委托的意思一样,是这样吗?

 

二、布局

     在android里面,不同的像素密度,一般使用的是dip做单位的,文字使用的是sp

            AbsoluteLayout 绝对布局(所有的信息都是写死的)

            FramerLayout 帧布局 这个布局所有的东西都是从左上角开始的,就是会叠加显示,不会像div那样挤压

            LinearLayout  线程布局(默认)默认从上到下依次
            android:orientation 设置布局方向  
            horizontal 水平均分
            layout_weight 在相同的情况下,呈现的是正好是对立的状态,在同一个线型布局里面就可以看到,

            带有layout_的都指的的是父空间的样式
            如 layout_gravity 指的是自己在父控件里面对齐样式 gravity    就是本身自己的样式

            RelativeLayout 该布局是参照父控件或者是其他控件的位置进行布局。比如说我要把A控件 ,放到B控件的下面,并且A控件的右边与B的左边对齐。类似这样的布局就可以使用相对布局来完成比较容易

            fill_parent 填满当前视图,在2.2之后的android版本 match_parent 即可(相同的意思)
            wrap_content 设置一个视图的尺寸为wrap_content将强制性地使视图扩展以显示全部内容。

    TableLayout 布局页面

     单元格属性:
          android:layout_column:指定该单元格在第几列显示(从0开始)
          android:layout_span:跨列(意思就是当前的控件占据单元格多少列)

          列属性:
          android:stretchColumns    设置可伸展的列。该列可以向行方向伸展,最多可占据一整行。
          android:shrinkColumns    设置可收缩的列。当该列子控件的内容太多,已经挤满所在行,那么该子控件的内容将往列方向显示。
          android:collapseColumns    设置要隐藏的列。

    本人对于布局就是个菜,本来就很不擅长做布局,No,不能用这个词,用这个词瞬间拉低了擅长这个词的身价,这样吧,我自己摸索的几个简单的布局,可以大家参考,参考

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:id="@+id/absoluteLayout1">
<!--android:orientation 设置的线型布局的方向-->
<!--在android里面,不同的像素密度,一般使用的是dip做单位的,文字使用的是sp-->
<!--layout_weight 在相同的情况下,呈现的是正好是对立的状态-->
    <Button
        android:id="@+id/MyButton1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="按钮一"
        android:layout_weight="3" />
    <Button
        android:id="@+id/MyButton2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="按钮二"
        android:layout_weight="3" />
    <Button
        android:id="@+id/MyButton3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="按钮三"
        android:layout_weight="3" />
</LinearLayout>

 效果图:

 

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <EditText
        android:id="@+id/edit"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="下面有个按钮" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮"
        android:layout_below="@id/edit"
        android:layout_alignRight="@id/edit" />
</RelativeLayout>

 效果图

 

来个带后台代码的。  

  

<?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">
<!--wrap_content 包裹内容-->
<!--horizontal 水平-->
<!--android:layout_height="wrap_content" 横向包裹内容-->
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/home"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="首页" />
        <Button
            android:id="@+id/jiankang"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第二个页面" />
        <Button
            android:text="第三个"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/button1" />
    </LinearLayout>
    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <LinearLayout
            android:id="@+id/layout1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="第一个布局"
                android:background="#0094ff" />
        </LinearLayout>
        <LinearLayout
            android:id="@+id/layout2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="第二个布局"
                android:background="#0045ff" />
        </LinearLayout>
        <LinearLayout
            android:id="@+id/layout3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="第三个布局"
                android:textSize="24sp"
                android:id="@+id/tv1" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:textSize="36sp"
                android:id="@+id/tv2"
                android:gravity="center" />
        </LinearLayout>
    </FrameLayout>
</LinearLayout>

 后台代码

    public class MainActivity : Activity, View.IOnClickListener
    {
        public void OnClick(View v)
        {
            LinearLayout layout3 = FindViewById<LinearLayout>(Resource.Id.layout3);
            LinearLayout layout2 = FindViewById<LinearLayout>(Resource.Id.layout2);
            LinearLayout layout1 = FindViewById<LinearLayout>(Resource.Id.layout1);
            if (v.Id == Resource.Id.home)
            {
                layout1.Visibility = ViewStates.Visible;
                layout2.Visibility = ViewStates.Invisible;
                layout3.Visibility = ViewStates.Invisible;
            }
            if (v.Id == Resource.Id.jiankang)
            {
                layout2.Visibility = ViewStates.Visible;
                layout1.Visibility = ViewStates.Invisible;
                layout3.Visibility = ViewStates.Invisible;
            }

            if (v.Id == Resource.Id.button1)
            {
                layout3.Visibility = ViewStates.Visible;
                layout1.Visibility = ViewStates.Invisible;
                layout2.Visibility = ViewStates.Invisible;
            }
        }

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            // Set our view from the "main" layout resource


            //  RelativeLayout
            //该布局是参照父控件或者是其他控件的位置进行布局。比如说我要把A控件
            //放到B控件的下面,并且A控件的右边与B的左边对齐。类似这样的布局就可
            //以使用相对布局来完成比较容易
            SetContentView(Resource.Layout.line);

            /*
            // 相对于给定ID控件
                android:layout_above 将该控件的底部置于给定ID的控件之上;
                android:layout_below 将该控件的底部置于给定ID的控件之下;
                android:layout_toLeftOf    将该控件的右边缘与给定ID的控件左边缘对齐;
                android:layout_toRightOf  将该控件的左边缘与给定ID的控件右边缘对齐;
 
                android:layout_alignBaseline  将该控件的baseline与给定ID的baseline对齐;
                android:layout_alignTop        将该控件的顶部边缘与给定ID的顶部边缘对齐;
                android:layout_alignBottom   将该控件的底部边缘与给定ID的底部边缘对齐;
                android:layout_alignLeft        将该控件的左边缘与给定ID的左边缘对齐;
                android:layout_alignRight      将该控件的右边缘与给定ID的右边缘对齐;
                // 相对于父组件
                android:layout_alignParentTop      如果为true,将该控件的顶部与其父控件的顶部对齐;
                android:layout_alignParentBottom 如果为true,将该控件的底部与其父控件的底部对齐;
                android:layout_alignParentLeft      如果为true,将该控件的左部与其父控件的左部对齐;
                android:layout_alignParentRight    如果为true,将该控件的右部与其父控件的右部对齐;
                // 居中
                android:layout_centerHorizontal 如果为true,将该控件的置于水平居中;
                android:layout_centerVertical     如果为true,将该控件的置于垂直居中;
                android:layout_centerInParent   如果为true,将该控件的置于父控件的中央;
                // 指定移动像素
                android:layout_marginTop      上偏移的值;
                android:layout_marginBottom 下偏移的值;
                android:layout_marginLeft   左偏移的值;
                android:layout_marginRight   右偏移的值;

            */

            /*
            设置line简单布局
            Introduce/Test007 布局说明.txt
            */
            //SetContentView(Resource.Layout.line);
            //layoutExample();

        }
        /// <summary>
        /// Line布局
        /// </summary>
        private void layoutExample()
        {
            Button btn = FindViewById<Button>(Resource.Id.home);
            btn.SetOnClickListener(this);
            Button btn1 = FindViewById<Button>(Resource.Id.jiankang);
            btn1.SetOnClickListener(this);
            Button bt1 = FindViewById<Button>(Resource.Id.button1);
            bt1.SetOnClickListener(this);
        }
    }

 

    原谅我,神呀,就这样了,对了,给大家留下点源码,解压之后,可以看到有一个test007和test011,其中011包含了前面所说到的所有的提示信息和消息框。007里面就是一些让正常人看着头疼的布局,额,相信我,好吧,不过你可以运行一下007的源码,点击一下那几个按钮,额,还是可以的么。

百度网盘链接:http://pan.baidu.com/s/1jHpbEoI 密码:afqf

前面两篇随笔的地址:

Android(Xamarin)之旅(一)

目录
相关文章
|
7月前
|
开发工具 Android开发 iOS开发
使用xamarin开发Android、iOS报错failed to open directory: 系统找不到指定的文件
使用vs2019学习xamarin时,创建新程序。使用模拟器真机等测试都报错如下图错误: ![请在此添加图片描述](https://developer-private-1258344699.cos.ap-guangzhou.myqcloud.com/column/article/5877188/20231030-de8ce5fd.png?x-cos-security-token=r4KyZDEowPT0kGTL0LqE8EnwfN1Nzexadb05dcffed3939ff8d7591c528c01706nvpGSE93QwHpZM8NwhJNTZctNRQa0l3KDhEnqj8P7d8t
70 0
使用xamarin开发Android、iOS报错failed to open directory: 系统找不到指定的文件
|
Java C# Android开发
.NET(WinCE、WM)开发转Android开发 ——Xamarin和Smobiler对比
WinCE从1995年诞生至今,已有20多年的发展历史,行业成熟方案覆盖范围广,从车载、工控、手持机都有涉及,且方案成熟。 近些年,Android以后来居上的态势,逐渐渗透至各行业领域,硬件手持大厂也把产品线重心向Android手持迁移,基于Android的行业解决方案越来越成熟,WinCE的开发人才流失,在WinCE解决方案上吃老本的企业寻求转型。
|
Web App开发 测试技术 Android开发
xamarin开发android收集的一些工具
原文:xamarin开发android收集的一些工具 xamarin开发android收集的一些工具 工欲善其事,必先利其器,从16年下半年开始做xamarin相关的开发,平时使用的一些工具和google插件给大家分享一下,都有下载地址,持续更新。
1494 0
|
Web App开发 JSON API
Xamarin android 调用Web Api(ListView使用远程数据)
xamarin android如何调用sqlserver 数据库呢(或者其他的),很多新手都会有这个疑问。xamarin android调用远程数据主要有两种方式: 在Android中保存数据或调用数据库可以利用SQLite,android中提供了几个类来管理SQLite数据库,对数据进行增删改查 直接调用Asp.net Web API对数据进行增删改查 这两种方式到底选择哪一种方式好一点呢?哪一种方式好不好我不敢确定,市场上大部分app都是调用api来clud的。
1308 0
|
Android开发 数据格式 XML
Xamarin Android Gestures详解
通过Gesture的监听我们将实现一个,手指的快速滑动显示坐标的变化,我们先来看一看效果图: 1.Android中手势交互的执行顺序 1.手指触碰屏幕时,触发MotionEvent事件! 2.
1007 0
|
Android开发 Java 数据格式
Xamarin Android自定义文本框
xamarin android 自定义文本框简单的用法 关键点在于,监听EditText的内容变化,不同于java中文本内容变化去调用EditText.addTextChangedListener(mTextWatcher);为EditText设置内容变化监听! 简单来说就是添加一个AfterTextChanged 事件就OK了,这是最简单的一种做法,当然你要想java那样去监听也可以的。
1145 0
|
缓存 Android开发 数据格式
xamarin android viewpager的用法
1.什么是ViewPager 通过手势滑动可以完成view的切换,一般是用来app的引导页或则实现图片轮播,类似网页上的banner轮播. Adnroid 3.0后引入的一个UI控件,在xamarin android 的开发中必须引入v4兼容包(ps这里有坑,菜鸟需要注意) 和ListVie...
1187 0
|
Android开发
xamarin android listview的用法
listview也许是用的非常频繁的一个控件之一,下面我写一个xamarin的listview栗子,大家尝一尝xamarin android开发的乐趣。原谅我的大小写吧. listview绑定自定义的BaseAdapter 先来看一下最终实现的效果图: News.
999 0
|
Android开发 Java
xamarin android如何监听单击事件
在xamarin android单击事件是最基础的事情,看过菜鸟上的android教程时,java写的都是监听事件,为一个按钮,单选按钮、多选按钮的单击事件有三种,前面两种用的非常普遍,也很简易,我这里主要就是写一下xamarin android中的监听事件。
1115 0
|
Android开发 数据格式 XML
xamarin android checkbox自定义样式
xamarin android checkbox自定义样式 在drawable文件在新建checkbox_bg.xml文件 //状态为选中 //状态为没选中 //默认的必须的有一个 在values文件夹中新建一个Style.
1268 0