DialogAct.java文件
package com.sxy.Dialog;
/**
* Android Dialog的7种形式
*/
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.example.s_demo.R;
public class DialogAct extends Activity
{
private LinearLayout mMainLy = null;
private LinearLayout.LayoutParams linearParams = null;
private AlertDialog.Builder builder = null;
//1.该效果是当按返回按钮时弹出一个提示,来确保无误操作,采用常见的对话框样式。
private Button common_Btn = null;
//2.改变了对话框的图表
private Button changeIcon_Btn = null;
//3.信息内容是一个简单的View类型
private Button input_Btn = null;
//4.信息内容是一组单选框
private Button singleChoice_Btn = null;
//5.信息内容是一组多选框
private Button multichoice_Btn = null;
//6.信息内容是一组简单列表项
private Button list_Btn = null;
//7.信息内容是一个自定义的布局
private Button custom_Btn = null;
private final int COMMON_BTNID = 0;
private final int CHANGEICON_BTNID = 1;
private final int INPUT_BTNID = 2;
private final int SINGLECHOICE_BTNID = 3;
private final int MULTICHOICE_BTNID = 4;
private final int LIST_BTNID = 5;
private final int CUSTOM_BTNID = 6;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.act_entry);
mMainLy = (LinearLayout)findViewById(R.id.act_entryly);
addBtn();
setBtnListener();
}
private void addBtn(){
linearParams = new LinearLayout.LayoutParams(-2, -2);
linearParams.topMargin = 20;
common_Btn = new Button(this);
common_Btn.setId(COMMON_BTNID);
common_Btn.setText("1、常见对话框");
if(mMainLy != null)
{
mMainLy.addView(common_Btn, linearParams);
}
changeIcon_Btn = new Button(this);
changeIcon_Btn.setId(CHANGEICON_BTNID);
changeIcon_Btn.setText("2、改变图表的对话框");
if(mMainLy != null)
{
mMainLy.addView(changeIcon_Btn, linearParams);
}
input_Btn = new Button(this);
input_Btn.setId(INPUT_BTNID);
input_Btn.setText("3、可输入的对话框");
if(mMainLy != null)
{
mMainLy.addView(input_Btn, linearParams);
}
singleChoice_Btn = new Button(this);
singleChoice_Btn.setId(SINGLECHOICE_BTNID);
singleChoice_Btn.setText("4、单选框的对话框");
if(mMainLy != null)
{
mMainLy.addView(singleChoice_Btn, linearParams);
}
multichoice_Btn = new Button(this);
multichoice_Btn.setId(MULTICHOICE_BTNID);
multichoice_Btn.setText("5、多选框的对话框");
if(mMainLy != null)
{
mMainLy.addView(multichoice_Btn, linearParams);
}
list_Btn = new Button(this);
list_Btn.setId(LIST_BTNID);
list_Btn.setText("6、列表项的对话框");
if(mMainLy != null)
{
mMainLy.addView(list_Btn, linearParams);
}
custom_Btn = new Button(this);
custom_Btn.setId(CUSTOM_BTNID);
custom_Btn.setText("7、自定义的对话框");
if(mMainLy != null)
{
mMainLy.addView(custom_Btn, linearParams);
}
}
private void setBtnListener(){
if(myOnClickListener != null){
if(common_Btn != null)
{
common_Btn.setOnClickListener(myOnClickListener);
}
if(changeIcon_Btn != null)
{
changeIcon_Btn.setOnClickListener(myOnClickListener);
}
if(input_Btn != null)
{
input_Btn.setOnClickListener(myOnClickListener);
}
if(singleChoice_Btn != null)
{
singleChoice_Btn.setOnClickListener(myOnClickListener);
}
if(multichoice_Btn != null)
{
multichoice_Btn.setOnClickListener(myOnClickListener);
}
if(list_Btn != null)
{
list_Btn.setOnClickListener(myOnClickListener);
}
if(custom_Btn != null)
{
custom_Btn.setOnClickListener(myOnClickListener);
}
}
}
OnClickListener myOnClickListener = new OnClickListener()
{
@Override
public void onClick(View v)
{
switch (v.getId())
{
case COMMON_BTNID:
//1、常见对话框
showCommonDialog();
break;
case CHANGEICON_BTNID:
//2、改变图表的对话框
showChangepicDialog();
break;
case INPUT_BTNID:
//3、可输入的对话框
showInputDialog();
break;
case SINGLECHOICE_BTNID:
//4、单选框的对话框
showSingleChoiceDialog();
break;
case MULTICHOICE_BTNID:
//5、多选框的对话框
showMultiChoiceDialog();
break;
case LIST_BTNID:
//6、列表项的对话框
showListDialog();
break;
case CUSTOM_BTNID:
//7、自定义的对话框
showCustomDialog();
break;
default:
break;
}
}
};
//1、常见对话框:该效果是当按返回按钮时弹出一个提示,来确保无误操作,采用常见的对话框样式。
private void showCommonDialog(){
builder = new AlertDialog.Builder(DialogAct.this);
builder.setMessage("确认下下");
builder.setTitle("温馨提示");
builder.setNeutralButton("中立", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
Toast.makeText(DialogAct.this, "1、常见对话框 click 中立", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
Toast.makeText(DialogAct.this, "1、常见对话框 click 取消", Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("确认", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
Toast.makeText(DialogAct.this, "1、常见对话框 click 确认", Toast.LENGTH_SHORT).show();
}
});
builder.show();
}
//2、改变图表的对话框:改变了对话框的图表
private void showChangepicDialog(){
builder = new AlertDialog.Builder(DialogAct.this);
builder.setIcon(R.drawable.anim_xph_1)
.setTitle("喜好调查")
.setMessage("你喜欢李连杰的电影吗?")
.setPositiveButton("很喜欢", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
Toast.makeText(DialogAct.this, "2、改变图表的对话框 click 很喜欢", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("不喜欢", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
Toast.makeText(DialogAct.this, "2、改变图表的对话框 click 不喜欢", Toast.LENGTH_SHORT).show();
}
})
.setNeutralButton("一般", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
Toast.makeText(DialogAct.this, "2、改变图表的对话框 click 一般", Toast.LENGTH_SHORT).show();
}
}).create().show();
}
//3.信息内容是一个简单的View类型
private void showInputDialog(){
builder = new AlertDialog.Builder(DialogAct.this);
builder.setIcon(R.drawable.anim_xph_1)
.setTitle("请输入")
.setView(new EditText(this))
.setPositiveButton("确定", null)
.setNegativeButton("取消", null)
.show();
}
//4、单选框的对话框
private void showSingleChoiceDialog(){
builder = new AlertDialog.Builder(DialogAct.this);
builder.setIcon(R.drawable.anim_xph_1)
.setTitle("单选框")
.setSingleChoiceItems(new String[]{"item1", "item2"}, 0, null)
.setPositiveButton("确定", null)
.setNegativeButton("取消", null)
.show();
}
//5、多选框的对话框
private void showMultiChoiceDialog(){
builder = new AlertDialog.Builder(DialogAct.this);
builder.setIcon(R.drawable.anim_xph_1)
.setTitle("多选框")
.setMultiChoiceItems(new String[]{"item1", "item2"}, null, null)
.setPositiveButton("确定", null)
.setNegativeButton("取消", null)
.show();
}
//6、列表项的对话框
private void showListDialog(){
builder = new AlertDialog.Builder(DialogAct.this);
builder.setIcon(R.drawable.anim_xph_1)
.setTitle("列表框")
.setItems(new String[]{"item1", "item2"}, null)
.setPositiveButton("确定", null)
.setNegativeButton("取消", null)
.show();
}
//7、自定义的对话框
private void showCustomDialog(){
LinearLayout relaLy = new LinearLayout(this);
relaLy.setBackgroundResource(R.drawable.dog);
Button btn = new Button(this);
btn.setBackgroundResource(R.drawable.pp);
btn.setGravity(Gravity.CENTER);
relaLy.addView(btn, -2, -2);
builder = new AlertDialog.Builder(DialogAct.this);
builder.setTitle("自定义布局")
.setView(relaLy)
.setPositiveButton("确定", null)
.show();
}
}
act_entry.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/act_entryly"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dip" >
</LinearLayout>