【Android学习笔记】自定义AlertDialog对话框
来源:程序员人生 发布时间:2015-02-06 09:23:12 阅读次数:2828次
(1)布局文件:
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="70dp"
android:layout_marginTop="62dp"
android:text="正告对话框" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="64dp"
android:text="多选对话框" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button2"
android:layout_below="@+id/button2"
android:layout_marginTop="60dp"
android:text="单选对话框" />
</RelativeLayout>
(2)类的文件:
package com.example.alertdialog;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
/**
* 正告对话框的使用
*
* @author piaodangdehun
*
*/
public class MainActivity extends Activity {
private Button button1;// 正告对话框
private Button button2;// 多选对话框
private Button button3;// 单选对话框
private AlertDialog.Builder builder; // 为button1设定的对话框,当有1个对话框的时候可使用全局的定义
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) this.findViewById(R.id.button1);
button2 = (Button) this.findViewById(R.id.button2);
button3 = (Button) this.findViewById(R.id.button3);
/*
* 1.正告对话框的设定
*/
builder = new AlertDialog.Builder(this);
builder.setTitle("提示框");
builder.setMessage("你肯定要删除吗?");
builder.setIcon(R.drawable.ic_launcher); // 设置图片
/*
* 肯定对话框
*/
builder.setPositiveButton("肯定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "确认删除", 1).show();
}
});
/*
* 取消对话框
*/
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "取消删除", 1).show();
}
});
/*
* 疏忽对话框
*/
builder.setNeutralButton("疏忽", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "疏忽删除", 1).show();
}
});
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
builder.show();
}
});
/*
* 2.多选对话框
* 当有多个对话框的时候在具体的时间内不进行声明定义
*/
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
/*
* 对话框的创建
*/
AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this);
builder.setTitle("请选择爱好");
final String[] hobby = { "上网", "打游戏", "聊天" };
/*
* 选择的列表
*/
builder.setMultiChoiceItems(hobby, new boolean[] { true, false,
false },
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which, boolean isChecked) {
if (isChecked) {
Toast.makeText(MainActivity.this,
"-->>" + hobby[which], 1).show();
}
}
});
/*
* 取消按钮
*/
builder.setPositiveButton("肯定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
}
});
builder.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
}
});
builder.show();
}
});
/*
* 3.单选对话框的设定
*/
button3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("XX");
builder.setSingleChoiceItems(new String[]{"上网", "打游戏", "聊天"}, ⑴, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setPositiveButton("肯定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
}
});
}
/*
* private final View.OnClickListener mylistenerClickListener = new
* OnClickListener() {
*
* @Override public void onClick(View v) { // TODO Auto-generated method
* stub
*
* } };
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
(3)结果展现
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠