android初级--Dialog示例

鼓励自己,每学一个技术就写一篇博客。

这是自己在csdn的第一篇博客。记录下。

Dialog是自己接触android学习的第一个控件,当初还是看《雨松MOMO安卓博客课程》学习的。转眼4个月已经过去了。最近看了好多大神的文章都鼓励要写博客,所以就来参与了。虽然之前在ACM打酱油的时候也写过。

毕竟是4个月前的代码了,所以风格跟现在的也有很大差距了,再加上代码页差不多忘了,最近又公司任务多,而且这个例子也没有什么难度了,就不多做介绍了。

该例子可能对初学者有用,各种牛人就把这给忽略掉吧。

以下是效果图:


首先当然是先写一个layout了,以下是layout的部分代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:id="@+id/wenzi"
        android:text="@string/hello_world" />
    <Button 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/wenzi"
        android:text="@string/san_btn"
        android:id="@+id/btn1"
        />

接下去是自定义对话框的layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dialog"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <LinearLayout
        android:id="@+id/dialogname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/tvUserName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@android:color/white"
            android:text="姓名:" />

        <EditText
            android:id="@+id/etUserName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:minWidth="200dip" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/dialognum"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/dialogname"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/tvPassWord"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@android:color/white"
            android:text="密码:" />

        <EditText
            android:id="@+id/etPassWord"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:minWidth="200dip" />
    </LinearLayout>

</RelativeLayout>

然后是activity.java。这里紧截取部分由意义的代码。

public void CreateDialog(int id){
		AlertDialog.Builder builder= new AlertDialog.Builder(MainActivity.this);
		switch(id){
		case DIALOG_1:
			builder.setIcon(R.drawable.ic_launcher);
			builder.setTitle("你是男的女的?");
			builder.setPositiveButton("男", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					showDialog("您的性别是男性");
				}
			});
			builder.setNegativeButton("女", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					showDialog("您的性别是女性");
				}
			});
			break;
		case DIALOG_2:
			builder.setIcon(R.drawable.ic_launcher);
			builder.setTitle("你是男的女的?");
			builder.setPositiveButton("男", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					showDialog("您的性别是男性");
				}
			});
			builder.setNeutralButton("你猜", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					showDialog("您的性别是你猜");
				}
			});
            builder.setNegativeButton("女", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					showDialog("您的性别是女性");
				}
			});
			break;
		case DIALOG_3:
			mChickNum=0;
			builder.setIcon(R.drawable.ic_launcher);
			builder.setTitle("单选框,选择一个你喜欢的");
			builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					mChickNum=which;
					showDialog("您喜欢的是:"+items[mChickNum]+"   他的id是:"+mChickNum);
				}
			});
			builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					showDialog("您点击了确定,您喜欢的是:"+items[mChickNum]+"   他的id是:"+mChickNum);
				}
			});
			builder.setNegativeButton("离开", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					
				}
			});
			break;
		case DIALOG_4:
			mCheckNum.clear();
			builder.setIcon(R.drawable.ic_launcher);
			builder.setTitle("多选框,选择一些你喜欢的");
			builder.setMultiChoiceItems(items,
					new boolean[] {false,false,false,false,false,false,false},
					new DialogInterface.OnMultiChoiceClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which, boolean isChecked) {
							// TODO Auto-generated method stub
							if(isChecked){
								mCheckNum.add(which);
								showDialog("您喜欢的是:"+items[which]+"   他的id是:"+which);
							}else{
								mCheckNum.remove(which);
							}
						}
					});
			builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					String strCheck="";
					int mSize=mCheckNum.size();
					for(int i=0;i<mSize;i++){
						strCheck+=items[mCheckNum.get(i)];
					}
					showDialog("您点击了确定,您喜欢的是:"+strCheck);
				}
			});
            builder.setNegativeButton("离开", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					
				}
			});
			break;
		case DIALOG_5:
			builder.setTitle("类表框显示");
			builder.setIcon(R.drawable.ic_launcher);
			builder.setItems(items, new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					showDialog("您选择的是"+items[which]);
				}
			});
			break;
		case DIALOG_6:
			mprogreedialog=new ProgressDialog(MainActivity.this);
			mprogreedialog.setIcon(R.drawable.ic_launcher);
			mprogreedialog.setTitle("进度条演示");
			mprogreedialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
			mprogreedialog.setMax(MAX_DIALOG);
			mprogreedialog.setButton("确定", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					showDialog("当前进度为:"+mprogreedialog.getProgress());
				}
			});
			mprogreedialog.show();
			new Thread(this).start();
			return;
		case DIALOG_7:
			mprogreedialog=new ProgressDialog(MainActivity.this);
			mprogreedialog.setIcon(R.drawable.ic_launcher);
			mprogreedialog.setTitle("读取进度条演示");
			mprogreedialog.setMessage("正在读取中。。。");
			mprogreedialog.setIndeterminate(true);
			mprogreedialog.setCancelable(true);
			mprogreedialog.show();
			return;
		case DIALOG_8:
			LayoutInflater factory=LayoutInflater.from(this);
			final View textEntryView=factory.inflate(R.layout.test, null);
			builder.setIcon(R.drawable.ic_launcher);
			builder.setTitle("自定义对话框");
			builder.setView(textEntryView);
			builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					EditText userName= (EditText) textEntryView.findViewById(R.id.etUserName);
					EditText userPsw=(EditText) textEntryView.findViewById(R.id.etPassWord);
					
					showDialog("用户名为:"+userName.getText().toString()+"     密码为:"+userPsw.getText().toString());
				}
			});
			break;
		}
		builder.create().show();
	}
	public void showDialog(String str){
		new AlertDialog.Builder(MainActivity.this).setMessage(str).show();
	}


由于代码较为简单,这里就不多介绍了。大家看着就懂了。

有兴趣可以去下载我的源码。http://download.csdn.net/detail/han123456o/6022051

个人觉得初学者用《雨松MOMO安卓博客课程》还是很有用的,这里推荐下了。



Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值