用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - android代码库

AlertDialog的使用

2016-12-16 作者: cwm1996举报

[android]代码库

package com.example.android_day13alertdialog;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener{

	private Button bt1,bt2,bt3,bt4,bt5;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initviews();
	}
	//初始化控件
	public void initviews(){

		bt1=(Button) findViewById(R.id.bt1);
		bt2=(Button) findViewById(R.id.bt2);
		bt3=(Button) findViewById(R.id.bt3);
		bt4=(Button) findViewById(R.id.bt4);
		bt5=(Button) findViewById(R.id.bt5);
		bt1.setOnClickListener(this);
		bt2.setOnClickListener(this);
		bt3.setOnClickListener(this);
		bt4.setOnClickListener(this);
		bt5.setOnClickListener(this);
	}
	//设置点击事件监听
	public void onClick(View v) {

		switch(v.getId()){
		case R.id.bt1:
			common();
			break;
		case R.id.bt2:
			item();
			break;
		case R.id.bt3:
			radiobutton();
			break;
		case R.id.bt4:
			checkbox();
			break;
		case R.id.bt5:
			custom();
			break;
		}		
	}
	/**
	 * 普通文本框
	 */
	public void common(){
		//创建对象
		Builder builder=new AlertDialog.Builder(this);
		//设置标题
		builder.setTitle("QQ");
		//设置图标
		builder.setIcon(R.drawable.qq);
		//设置内容
		builder.setMessage("是否确定退出QQ?");
		//设置确定按钮
		builder.setPositiveButton("是", new DialogInterface.OnClickListener() {			
			@Override
			public void onClick(DialogInterface dialog, int which) {				
				Toast.makeText(MainActivity.this, "欢迎下次进入QQ", Toast.LENGTH_SHORT).show();							    
			}
		})
		//设置取消按钮
		.setNegativeButton("否",new DialogInterface.OnClickListener() {			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(MainActivity.this, "继续运行QQ", Toast.LENGTH_SHORT).show();
			}
			//创建对话框create(),展示show(),关闭dismiss()。
		});
		AlertDialog dialog=builder.create();
				dialog.show();
	}
	/**
	 * 列表选项卡
	 */
	public void item(){
		final String[] str=new String[]{"选项一","选项二","选项三","选项四"};
		new AlertDialog.Builder(this)
		.setTitle("列表对话框")
		.setIcon(R.drawable.qq)
		.setItems(str, new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {				
				Toast.makeText(MainActivity.this, str[which], Toast.LENGTH_SHORT).show();							    				
			}
		})
		.setPositiveButton("是", new DialogInterface.OnClickListener() {			
			@Override
			public void onClick(DialogInterface dialog, int which) {				
				Toast.makeText(MainActivity.this, "欢迎下次进入QQ", Toast.LENGTH_SHORT).show();							    
			}
		})
		.setNegativeButton("否",new DialogInterface.OnClickListener() {			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(MainActivity.this, "继续运行QQ", Toast.LENGTH_SHORT).show();
			}
		}).create().show();
	}
	/**
	 * 单选文本框
	 */
	public void radiobutton(){
		final String[] str=new String[]{"选项一","选项二","选项三","选项四"};
		new AlertDialog.Builder(this)
		.setTitle("列表对话框")
		.setIcon(R.drawable.qq)
		.setSingleChoiceItems(str, 2, new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(MainActivity.this, "您选了"+str[which],Toast.LENGTH_SHORT).show();

			}
		})
		.setPositiveButton("是", new DialogInterface.OnClickListener() {			
			@Override
			public void onClick(DialogInterface dialog, int which) {				
				Toast.makeText(MainActivity.this, "欢迎下次进入QQ", Toast.LENGTH_SHORT).show();							    
			}
		})
		.setNegativeButton("否",new DialogInterface.OnClickListener() {			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(MainActivity.this, "继续运行QQ", Toast.LENGTH_SHORT).show();
			}
		}).create().show();
	}
	/**
	 * 多选文本框
	 */
	public void checkbox(){
		final String[] str=new String[]{"选项一","选项二","选项三","选项四"}; 
		new AlertDialog.Builder(this)
		.setTitle("列表对话框")
		.setIcon(R.drawable.qq)
		.setMultiChoiceItems(str,null , new OnMultiChoiceClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which, boolean isChecked) {
				Toast.makeText(MainActivity.this, str[which]+"的状态为"+isChecked, Toast.LENGTH_SHORT).show();			
			}
		})
		.setPositiveButton("是", new DialogInterface.OnClickListener() {			
			@Override
			public void onClick(DialogInterface dialog, int which) {				
				Toast.makeText(MainActivity.this, "欢迎下次进入QQ", Toast.LENGTH_SHORT).show();							    
			}
		})
		.setNegativeButton("否",new DialogInterface.OnClickListener() {			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(MainActivity.this, "继续运行QQ", Toast.LENGTH_SHORT).show();
			}
		}).create().show();
	}
	/**
	 * 自定义文本框
	 */
	public void custom(){
		//		ImageView iv=new ImageView(MainActivity.this);
		//		iv.setImageResource(R.drawable.ic_launcher);
		View v=getLayoutInflater().inflate(R.layout.login, null);
		new AlertDialog.Builder(this)
		.setTitle("列表对话框")
		.setIcon(R.drawable.qq)
		.setView(v)
		.setPositiveButton("是", new DialogInterface.OnClickListener() {			
			@Override
			public void onClick(DialogInterface dialog, int which) {				
				Toast.makeText(MainActivity.this, "欢迎下次进入QQ", Toast.LENGTH_SHORT).show();							    
			}
		})
		.setNegativeButton("否",new DialogInterface.OnClickListener() {			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(MainActivity.this, "继续运行QQ", Toast.LENGTH_SHORT).show();
			}
		}).setCancelable(false).create().show();
	}



}


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...