[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();
}
}
by: 发表于:2017-10-18 09:18:33 顶(0) | 踩(0) 回复
??
回复评论