android中控件Dialog对话框的使用

	public void click(View view){
		AlertDialog.Builder ad = new AlertDialog.Builder(this) ;
		ad.setTitle("普通对话框")
		.setIcon(R.drawable.ic_launcher)
		.setMessage("哈哈哈,我弹出来了")
		.setNegativeButton("取消", new DialogInterface.OnClickListener(){

			@Override
			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(MainActivity.this, "我是对话框", 0).show() ;
			}
		}).show() ;
	}
	
	public void click1(View view){
		AlertDialog.Builder ad = new AlertDialog.Builder(this) ;
		ad.setTitle("确认对话框")
		.setIcon(R.drawable.ic_launcher)
		.setMessage("哈哈哈,我弹出来了")
		.setNegativeButton("取消", new DialogInterface.OnClickListener(){

			@Override
			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(MainActivity.this, "你点击了取消按钮", 0).show() ;
			}
		})
		.setPositiveButton("确认", new DialogInterface.OnClickListener(){

			@Override
			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(MainActivity.this, "你点击了确认按钮", 0).show() ;
			}
		})
		.show() ;
	}
	
	public void click2(View view){
		AlertDialog.Builder ad = new AlertDialog.Builder(this) ;
		ad.setTitle("确认对话框")
		.setIcon(R.drawable.ic_launcher)
		.setMessage("哈哈哈,我弹出来了")
		.setPositiveButton("确认", new DialogInterface.OnClickListener(){

			@Override
			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(MainActivity.this, "你点击了确认按钮", 0).show() ;
			}
		})
		.setNegativeButton("取消", new DialogInterface.OnClickListener(){

			@Override
			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(MainActivity.this, "你点击了取消按钮", 0).show() ;
			}
		})
		.setNeutralButton("忽略", new DialogInterface.OnClickListener(){

			@Override
			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(MainActivity.this, "你点击了忽略按钮", 0).show() ;
			}
		})
		.show() ;
	}
	
	public void click3(View view){
		AlertDialog.Builder ad = new AlertDialog.Builder(this) ;
		ad.setTitle("确认对话框")
		.setIcon(R.drawable.ic_launcher)
		.setItems(country, new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(MainActivity.this, "你选择了" + country[which], 0).show() ;
			}
		})
		.show() ;
	}
	
	public void click4(View view){
		AlertDialog.Builder ad = new AlertDialog.Builder(this) ;
		ad.setTitle("确认对话框")
		.setIcon(R.drawable.ic_launcher)
		.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.select_dialog_item, country),  new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(MainActivity.this, "你选择了" + country[which], 0).show() ;
			}
		})
		.show() ;
	}
	
	public void click5(View view){
		AlertDialog.Builder ad = new AlertDialog.Builder(this) ;
		ad.setTitle("单选对话框")
		.setIcon(R.drawable.ic_launcher)
		.setSingleChoiceItems(country, android.R.layout.select_dialog_singlechoice, new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(MainActivity.this, "你选择的是" + country[which], 0).show() ;
			}
		})
		.show() ;
	}
	
	public void click6(View view){
		AlertDialog.Builder ad = new AlertDialog.Builder(this) ;
		ad.setTitle("多选对话框")
		.setIcon(R.drawable.ic_launcher)
		.setMultiChoiceItems(country, new boolean[]{true,true,true}, new DialogInterface.OnMultiChoiceClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which, boolean isChecked) {
				if(isChecked)
					Toast.makeText(MainActivity.this, "你选择的是" + country[which], 0).show() ;
				else
					Toast.makeText(MainActivity.this, "你取消了" + country[which] + "的选择", 0).show() ;
			}
		})
		.show() ;
	}
	
	public void click7(View view){
		
		/**
		 * 先将xml文件转换为一个view控件
		 * LayoutInflater:布局填充器: 作用相当于findViewById方法
		 *    区别:
		 *    	   findViewById是用来寻找布局文件中的具体控件
		 *         LayoutInflater 是用来寻找布局文件,并把布局文件转换为view对象
		 *    
		 * 
		 */
		//创建布局填充器对象
		LayoutInflater li = LayoutInflater.from(this) ;
		//加载xml布局文件,并转换为view对象
		final View loginview = li.inflate(R.layout.login, null) ;
		
		AlertDialog.Builder ad = new AlertDialog.Builder(this) ;
		ad.setTitle("登录对话框")
		.setIcon(R.drawable.ic_launcher)
		.setView(loginview) 
		.setNegativeButton("取消", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel() ;  //关闭对话框 
			}
		})
		.setPositiveButton("登录", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				try {
					//点击不关闭对话框
					final Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
					field.setAccessible(true);
					field.set(dialog,false);//true表示要关闭
					
					//拿到对话框上的确认按钮
					Button btn = ((AlertDialog)dialog).getButton(DialogInterface.BUTTON_POSITIVE) ;
					
					final DialogInterface di = dialog ;
					//注册单击事件
					btn.setOnClickListener(new View.OnClickListener(){

						@Override
						public void onClick(View v) {
							login() ;
						}

						private void login() {
							//拿到登录的信息
							//注意:findViewById前一定要指定在那个布局文件中寻找控件资源
							EditText etName = (EditText) loginview.findViewById(R.id.name) ;
							EditText etPass = (EditText) loginview.findViewById(R.id.pass) ;
							
							String name = etName.getText().toString().trim() ;
							String pass = etPass.getText().toString() ;
							
							//判断信息是否输入
							if(TextUtils.isEmpty(name)){
								AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this) ;
								alert.setTitle("警告")
								.setIcon(android.R.drawable.alert_light_frame)
								.setMessage("姓名必须填写")
								.setPositiveButton("确认", null)
								.show() ;
								
								return ;
							}
							if(TextUtils.isEmpty(pass)){
								Toast.makeText(MainActivity.this, "密码必须填写", 0).show() ;
								return ;
							}
							
							//判断是否合法
							if(!"abc".equals(name) || !"111".equals(pass)){
								Toast.makeText(MainActivity.this, "用户名或者密码错误", 0).show() ;
								return ;
							}
							
							//合法则转向新的界面
							Intent intent = new Intent(MainActivity.this,SecondActivity.class) ;
							String s = name + "-" + pass ;
							intent.putExtra("info", s) ;
							
							startActivity(intent) ;
							
							try {
								field.set(di, true) ;  //关闭对话框
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					}) ;
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		})  
		.show();
		
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值