Xamarin.Android实现界面弹框
目录
- 1、使用系统自带的样式
- 1.1 具体实现
- 1.2 效果
- 2、自定义样式的实现
- 2.1 预期效果
- 2.2 具体实现
- 2.3 相关知识
- 3 代码下载
- 4、参考
在App的实际使用中,一定会出现弹框选择的情况。如图所示:
因此非常有必须学会及使用弹框的功能,因此本次学习Xamarin.Andorid下面的弹框。下面是代码说明
1、使用系统自带的样式
1.1 具体实现
这个是最简单的,其效果就是最开始展示的效果图片的,效果还是很不错,建议使用此方式。其代码如下,
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{private Button diaLogBtn, selfDefineBtn;//界面上的button,调用不用的dialogprivate Android.App.AlertDialog alertDialog = null;private Android.App.AlertDialog.Builder builder = null;private EditText name, inputtype;protected override void OnCreate(Bundle savedInstanceState){base.OnCreate(savedInstanceState);Xamarin.Essentials.Platform.Init(this, savedInstanceState);// Set our view from the "main" layout resourceSetContentView(Resource.Layout.activity_main);//界面中其他input元素name = FindViewById<EditText>(Resource.Id.editText1);inputtype = FindViewById<EditText>(Resource.Id.editText2);//界面中的按钮diaLogBtn = FindViewById<Button>(Resource.Id.button1); //使用系统自定义//按钮下的功能实现diaLogBtn.Click += DiaLogBtn_Click;}//使用系统默认的private void DiaLogBtn_Click(object sender, System.EventArgs e){string select_string = "";builder = new Android.App.AlertDialog.Builder(this);string[] testData = new string[] {"选项1","选项2","选项3" }; //要在界面上展示的数据alertDialog = builder.SetTitle("提示").SetSingleChoiceItems(testData,0, (s,e) => { //表示设置的是单选框。这儿可以设置为单选、多选以及只是显示选项select_string = testData[e.Which];}).SetPositiveButton("确定", (s, e) => { //确定按钮及内部方法addData(name.Text, inputtype.Text, select_string);}).SetNegativeButton("关闭", (s, e) => { //界面上的关闭按钮及方法alertDialog.Dismiss();}).Create();alertDialog.Show();}//具体的业务方法(例如,调用API,存储到数据库中)private void addData(string name,string type,string selected){Toast.MakeText(this, "所选择的内容分别为:"+name+";"+type+";"+selected, ToastLength.Long).Show();alertDialog.Dismiss();}
}
在对话框的实现中,还可以设置为选项、多选框,可通过设置SetItems()
、SetMultiChoiceItems
等方法实现。
通过以上代码,就可以实现。效果如下:
1.2 效果
在界面上的text框内,录入相关内容,并在弹出的框中,选择选项后,点击保存(图2),界面会弹出text及所选文本框的内容(图3)。
图2 界面上录入的内容,并选择相应选项
图3 具体的展示效果
2、自定义样式的实现
2.1 预期效果
预期效果如下所示:
因为需要自定义窗体,所以需要对窗体进行定义。
2.2 具体实现
窗体分为上中下三个部分。因此在Resources/layout
文件夹中添加布局文件view_dialog_custom.xml
.具体代码如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/layout_relative"android:layout_width="match_parent"android:layout_height="match_parent"><!--头部--><RelativeLayoutandroid:id="@+id/layout_title"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentTop="true"android:background="#53cc66"android:padding="5dp"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_centerVertical="true"android:text="提示文本"android:textSize="18sp"android:textStyle="bold"android:textColor="#ffffff"/><Buttonandroid:id="@+id/btn_cancel"android:layout_width="30dp"android:layout_height="30dp"android:layout_alignParentRight="true"android:background="@drawable/btn_selector_exit" /></RelativeLayout><!--中间内容--><LinearLayoutandroid:id="@+id/layout_detail"android:layout_height="wrap_content"android:layout_width="match_parent"android:layout_below="@+id/layout_title"android:layout_centerInParent="true"android:orientation="vertical"android:background="#f1f1f1"android:padding="20dp"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="自定义对话框"android:textColor="#04AEDA"android:textSize="18sp" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="作者:郑林"android:textColor="#04AEDA"android:textSize="18sp" /></LinearLayout><!--底部按钮--><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@+id/layout_detail"android:orientation="horizontal"android:background="#f1f1f1"android:padding="5dp"><Buttonandroid:id="@+id/btn_blog"android:layout_width="match_parent"android:layout_height="45dp"android:layout_weight="1"android:background="@drawable/btn_selector_choose"android:text="访问博客"android:textColor="#ffffff"android:textSize="20sp"android:layout_marginRight="5dp"/><Buttonandroid:id="@+id/btn_close"android:layout_width="match_parent"android:layout_height="45dp"android:layout_weight="1"android:background="@drawable/btn_selector_choose"android:text="关闭对话框"android:textColor="#ffffff"android:textSize="20sp" /></LinearLayout>
</RelativeLayout>
在布局文件中,头部中按钮的样式,引用了Resources/drawable
的btn_selector_exit.xml
文件。具体代码如下:
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_pressed="true" android:drawable="@drawable/exit_press"/><item android:drawable="@drawable/exit"/>
</selector>
在底部的按钮,同样引用了Resources/drawable
的btn_selector_choose.xml
文件,具体代码如下:
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_pressed="true" android:drawable="@drawable/pressed_color" /><item android:drawable="@drawable/default_color" />
</selector>
这两个样式,其最核心的功能就是,button的press_down后,颜色发生变化。
以上就定义了样式。
下面是核心的活动定义的实现部分。
在MainActivity中,具体定义如下:
protected override void OnCreate(Bundle savedInstanceState)
{base.OnCreate(savedInstanceState);Xamarin.Essentials.Platform.Init(this, savedInstanceState);// Set our view from the "main" layout resourceSetContentView(Resource.Layout.activity_main);//界面中其他inputname = FindViewById<EditText>(Resource.Id.editText1);inputtype = FindViewById<EditText>(Resource.Id.editText2);//界面中的按钮diaLogBtn = FindViewById<Button>(Resource.Id.button1); //使用系统自定义selfDefineBtn = FindViewById<Button>(Resource.Id.button2);//使用自定义的样式//按钮下的功能实现diaLogBtn.Click += DiaLogBtn_Click;selfDefineBtn.Click += SelefDefineBtn_Click;
}//自定义样式的具体实现
private void SelefDefineBtn_Click(object sender, System.EventArgs e)
{//加载自定的样式,使样式成为View。再将View赋给dialogLayoutInflater layoutInflater = LayoutInflater.From(this);var view_customer = layoutInflater.Inflate(Resource.Layout.view_dialog_custom, null, false);builder = new Android.App.AlertDialog.Builder(this);builder.SetView(view_customer);builder.SetCancelable(false);alertDialog = builder.Create();view_customer.FindViewById(Resource.Id.btn_cancel).Click += (s, e) =>{Toast.MakeText(this, "对话框已关闭", ToastLength.Short).Show();alertDialog.Dismiss();};view_customer.FindViewById(Resource.Id.btn_blog).Click += delegate{Toast.MakeText(this, "正在访问博客", ToastLength.Short).Show();Uri uri = Uri.Parse("https://blog.csdn.net/zlbcdn");Intent intent = new Intent(Intent.ActionView, uri);StartActivity(intent);alertDialog.Dismiss();};view_customer.FindViewById(Resource.Id.btn_close).Click += delegate{Toast.MakeText(this, "对话框已关闭", ToastLength.Short).Show();alertDialog.Dismiss();};alertDialog.Show();
}
2.3 相关知识
在整个过程中就是使用了AlertDialog
。一般创建对话框的逻辑:
构造显示Dialog的一般流程,构造AlertDialog.Builder,然后设置各种属性,最后调用AlertDialog.Builder.create方法获取AlertDialog对象,并且create方法中会执行,构造AlertDialog,设置dialog各种属性的操作。最后我们调用Dialog.show方法展示窗口,初始化Dialog的布局文件,Window对象等,然后执行mWindowManager.addView方法,开始执行绘制View的操作,并最终将Dialog显示出来;
3 代码下载
代码下载地址:代码下载,提取码:ZLNH
4、参考
1、xamarin android alertdialog详解(几种弹出窗口)
2、【推荐***推荐】AlertDialog的六种创建方式
3、AlertDialog 详解
Xamarin.Android实现界面弹框相关推荐
- Xamarin Android设置界面提示类型错误
Xamarin Android设置界面提示类型错误 错误信息:Integer types not allow (at 'padding' with value '10') Android界面属性的长度 ...
- android仿ios弹框_在“提示”框中:iOS外观(在Android上运行),Google Maps作为Time Machine,下载Wii游戏保存...
android仿ios弹框 Once a week we round up some great reader tips and share them with everyone. Read on t ...
- Android自定义Dialog弹框效果
这是弹框输入密码的,这里还有一个是我发现一个博友写的自定义Dialog弹框信息和背景阴影显示 ,两者结合一起看,最好了!!! 下面是弹框输入密码的!!! 首先需要自定义一个类,继承Dialog pac ...
- Android 几种弹框样式 自定义Dialog PopupWindow的使用
1.弹框的波浪线是动态的 和小度弹框样式相似 用到PopWindow 和自定义View . 2.这个弹框是动态的 用于网络加载时候 用到自定义Dialog 3.这就是一简单通用的弹框样式 第一种弹框 ...
- Android之底部弹框
文章链接: Android之底部弹窗 知识点: 1.dialog的简单使用: 2.自定义底部弹框: 3.新名词记录 { 5.0 转场动画 } 底部弹框:就是从手机屏幕底边位置弹出来,最常见的就是弹框里 ...
- android点击弹框,Android——js交互实现点击弹框
1.在main文件夹下创建assets文件夹导入html文件 Carson_Ho // JS代码 // Android需要调用的方法 function callJS(){ alert("An ...
- Android隐私弹框
Android使用隐私弹框 编写Android的APP进去点击的用户隐私弹框 效果图 java代码 xml布局界面 如何使用 git地址 本次弹框分享到此结束,喜欢的点个赞,谢谢大家啦 编写Andro ...
- android筛选功能代码,Android中 TeaScreenPopupWindow多类型筛选弹框功能的实例代码
Github地址 YangsBryant/TeaScreenPopupWindow (Github排版比较好,建议进入这里查看详情,如果觉得好,点个star吧!) 引入module allprojec ...
- java安卓弹框样式_Android 实现抖音头像底部弹框效果的实例代码
布局文件 activity_test.xml android:layout_width="match_parent" android:layout_height="mat ...
最新文章
- javascript元素跟随鼠标在指定区域运动
- hessian原理解析二(服务端分析)
- 根据屏幕分辨率获取css,根据判断浏览器类型屏幕分辨率自动调用不同CSS的代码...
- 用ASP.NET 重绘TabControl代码
- 2008 年 十大预测的总结 (只对了一半左右)
- 压缩pdf大小_PDF压缩到指定大小该怎么操作?分享完成PDF压缩超好用的方法
- JavaScript:语言精粹
- 软件工程师的技能知识图谱0.1(偏后台方向)
- 翻译: 3.2. 从零开始实现线性回归 深入神经网络 pytorch
- queryDsl初学
- 242种颜色样式、中英文名称及十六进制的值
- 基于KNN的离群点检测算法的Matlab版实现
- 软件研发的绩效考核漫谈一
- 上云一时爽,遇坑泪两行
- 961 计算机学科专业基础综合,2018年北京航空航天大学961计算机学科专业基础综合考研复习资料...
- 计蒜客 god of gambler
- 网络安全之防病毒网关
- Unity Hair 毛发系统 初体验
- 十三五智慧医疗与健康服务业发展趋势
- 限量级预告!劳动合同电子化,这样做最合法