package org.com.examples;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity
{
Button simple = null;
Button list = null;
Button single = null;
Button progress = null;
Button custom = null;
private final static int SIMPLEALERTDIALOG = 1;
private final static int LISTALERTDIALOG = 2;
private final static int SINGLEALERTDIALOG = 3;
private final static int PROGRESSALERTDIALOG = 4;
private final static int CUSTOMALERTDIALOG = 5;
private static final int MAX = 100;
String[] items;
private AlertDialog alert;
private ProgressDialog progressdialog;
private Handler handler;
private int progs = 0;
EditText et1 = null;
EditText et2 = null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
simple = (Button) findViewById(R.id.btnSimpleDialog);
list = (Button) findViewById(R.id.btnListDialog);
single = (Button) findViewById(R.id.btnSingleDialog);
progress = (Button) findViewById(R.id.btnProgressDialog);
custom = (Button) findViewById(R.id.btnCustomDialog);
simple.setOnClickListener(listener);
list.setOnClickListener(listener);
single.setOnClickListener(listener);
custom.setOnClickListener(listener);
progress.setOnClickListener(listener);
handler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
super.handleMessage(msg);
if (progs >= MAX)
{
progressdialog.dismiss();
} else
{
progs++;
progressdialog.setProgress(progs);
handler.sendEmptyMessageDelayed(0, 100);
// handler.sendEmptyMessageAtTime(0, 100);
Log.i("ss", "" + progs);
}
}
};
}
OnClickListener listener = new OnClickListener()
{
@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.btnSimpleDialog:
// Toast.makeText(MainActivity.this, "点击了Button",
// Toast.LENGTH_SHORT).show();
Log.i("ss", "OnClickListener");
showDialog(SIMPLEALERTDIALOG);
break;
case R.id.btnListDialog:
showDialog(LISTALERTDIALOG);
break;
case R.id.btnSingleDialog:
showDialog(SINGLEALERTDIALOG);
break;
case R.id.btnProgressDialog:
showDialog(PROGRESSALERTDIALOG);
progs = 0;
progressdialog.setProgress(progs);
handler.sendEmptyMessage(0);
break;
case R.id.btnCustomDialog:
showDialog(CUSTOMALERTDIALOG);
break;
}
}
};
@Override
protected Dialog onCreateDialog(int id)
{
items = new String[]
{ "红色", "蓝色", "绿色", "青色", "橙色", "紫色" };
Builder builder = new AlertDialog.Builder(this);
switch (id)
{
case SIMPLEALERTDIALOG://是否退出本软件
Log.i("ss", "onCreateDialog");
builder.setMessage(" 您要退出本软件么?");
builder.setCancelable(false);
builder.setPositiveButton("是", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
MainActivity.this.finish();
}
});
builder.setNegativeButton("否", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
dialog.cancel();
}
});
return builder.create();
case LISTALERTDIALOG://一个ListView的dialog,选择一项 dialog自动消失
builder.setTitle("请选择一种颜色:");
builder.setItems(items, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
Toast.makeText(MainActivity.this, items[which], Toast.LENGTH_SHORT).show();
}
});
return builder.create();
case SINGLEALERTDIALOG://单选RadioButton的dialog
builder.setTitle("请选择一种颜色:");
builder.setSingleChoiceItems(items, 1, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
Toast.makeText(MainActivity.this, items[which], Toast.LENGTH_SHORT).show();
if (null != alert)
{
alert.dismiss();
}
}
});
alert = builder.create();
return alert;
case PROGRESSALERTDIALOG:
//复选框CheckButton的dialog
// builder.setTitle("请选择一种颜色:");
// builder.setMultiChoiceItems(items, null, new
// DialogInterface.OnMultiChoiceClickListener()
// {
//
// @Override
// public void onClick(DialogInterface dialog, int which, boolean
// isChecked)
// {
// if (isChecked)
// {
// Toast.makeText(MainActivity.this, items[which],
// Toast.LENGTH_SHORT).show();
// }
// }
// });
//进度条dialog
progressdialog = new ProgressDialog(this);
progressdialog.setTitle("进度条!");
progressdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressdialog.setMax(MAX);
// progressdialog.setProgress(50);
progressdialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
Toast.makeText(MainActivity.this, "" + progs, Toast.LENGTH_SHORT).show();
}
});
progressdialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
progressdialog.dismiss();
}
});
return progressdialog;
case CUSTOMALERTDIALOG://自定义dialog,外加取得dialog中的EditText
LayoutInflater inflater = LayoutInflater.from(this);
final View view = inflater.inflate(R.layout.dialogs, null);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("自定义对话框");
builder.setView(view);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
// TODO Auto-generated method stub
et1 = (EditText) (view.findViewById(R.id.Name));
et2 = (EditText) (view.findViewById(R.id.Password));
Toast.makeText(MainActivity.this, "姓名:" + et1.getText().toString() + "\n密码:" + et2.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
// TODO Auto-generated method stub
}
});
return builder.create();
}
return null;
}
@Override
protected void onPrepareDialog(int id, Dialog dialog)
{
// TODO Auto-generated method stub
super.onPrepareDialog(id, dialog);
}
}
android:progressBarStyle:默认进度条样式
android:progressBarStyleHorizontal:水平样式 |