5种android对话框
1 弹出普通对话框 --- 系统更新
2 自定义对话框-- 用户登录
3 时间选择对话框 -- 时间对话框
4 进度条对话框 -- 信息加载..
5 popuWindow对话框
1 弹出普通对话框 --- 系统更新
[csharp] view plain
copy
class="dp-c" start="1">
- public void showNormalDialog(View v) {
-
- AlertDialog.Builder builder = new Builder(this);
-
- builder.setIcon(R.drawable.ic_launcher);
-
- builder.setTitle("更新");
-
- builder.setMessage("发现新版本是否更新?");
-
- builder.setPositiveButton("确定",new OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog, int which) {
-
- Toast.makeText(MainActivity.this, "开始下载新版本", Toast.LENGTH_SHORT).show();
- }
- });
- builder.setNegativeButton("取消", new OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog, int which) {
-
- Toast.makeText(MainActivity.this, "不需要更新", Toast.LENGTH_SHORT).show();
-
- }
- });
- builder.setNeutralButton("下一次", new OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog, int which) {
-
- Toast.makeText(MainActivity.this, "下一次吧", Toast.LENGTH_SHORT).show();
-
- }
- });
-
- Dialog dialog = builder.create();
-
- dialog.show();
-
- }
2 自定义对话框-- 用户登录
布局文件:
user_name_dialog.xml
[csharp] view plain
copy
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:padding="10dip"
- android:orientation="vertical" >
-
- <TextView
- android:id="@+id/tv_title"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="登录信息"
- android:gravity="center"
- android:textAppearance="?android:attr/textAppearanceLarge" />
-
-
- <TextView
- android:id="@+id/tv_name"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="用户名:" />
-
- <EditText android:id="@+id/et_name"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="请输入用户名"/>
-
- <TextView
- android:id="@+id/tv_pwd"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="密 码:" />
-
- <EditText android:id="@+id/et_pwd"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:inputType="textPassword"
- android:hint="请输入密码"/>
-
- <requestFocus />
-
- <Button
- android:id="@+id/btn_confirm"
- android:layout_width="150dip"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:text="登录" />
-
- <Button
- android:id="@+id/btn_cancel"
- android:layout_width="150dip"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:text="取消" />
-
-
- </LinearLayout>
java代码:
[csharp] view plain
copy
- Dialog cus_dialog ;
- public void showCustomDialog(View v){
- AlertDialog.Builder builder = new Builder(this);
-
- LayoutInflater inflater = LayoutInflater.from(this);
- View view = inflater.inflate(R.layout.user_name_dialog, null);
-
-
- builder.setView(view);
-
-
- final EditText name = (EditText) view.findViewById(R.id.et_name);
- final EditText pwd = (EditText) view.findViewById(R.id.et_pwd);
- Button btn_confirm = (Button) view.findViewById(R.id.btn_confirm);
-
- btn_confirm.setOnClickListener(new View.OnClickListener() {
-
- @Override
- public void onClick(View v) {
-
- if(name.getText().toString().trim().equals("abc")){
- showToastMsg("用户名正确");
-
- cus_dialog.dismiss();
- }
- else{
- showToastMsg("用户名错误");
- }
- }
- });
-
- Button btnCancel = (Button) view.findViewById(R.id.btn_cancel);
-
- btnCancel.setOnClickListener(new View.OnClickListener() {
-
- @Override
- public void onClick(View v) {
-
- cus_dialog.dismiss();
- }
- });
- cus_dialog = builder.create();
- cus_dialog.show();
-
-
- }
下载地址:http://www.jinhusns.com/Products/Download/?type=yhq
3 时间选择对话框 -- 时间对话框
[csharp] view plain
copy
- public void showTimePickerDialog(View v){
-
- Calendar sysDate = Calendar.getInstance();
-
- sysDate.setTimeInMillis(System.currentTimeMillis());
- int hour = sysDate.get(Calendar.HOUR_OF_DAY);
- int minute = sysDate.get(Calendar.MINUTE);
-
-
- TimePickerDialog time = new TimePickerDialog(this,
- new OnTimeSetListener() {
-
- @Override
- public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
-
- showToastMsg(" hourOfDay:" + hourOfDay + " minute:" + minute);
- }
- },
- hour,
- minute,
- true);
-
- time.show();
- }
4 进度条对话框 -- 信息加载..
[csharp] view plain
copy
- public void showProgressDialog(View v){
-
- final ProgressDialog progress = new ProgressDialog(this);
- progress.setProgress(R.drawable.img2);
- progress.setTitle("标题");
-
- progress.setMessage("加载中...");
-
- progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
-
-
-
-
- progress.setMax(100);
-
- progress.setProgress(50);
- progress.setButton("确定", new OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog, int which) {
-
- showToastMsg("确定按钮");
- }
- });
-
-
- progress.setButton2("取消", new OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog, int which) {
-
- showToastMsg("取消按钮");
- }
- });
-
- progress.show();
-
- new Thread(new Runnable() {
-
- @Override
- public void run() {
-
- int i = 0;
- while (i < 100) {
- try {
- Thread.sleep(200);
-
- progress.incrementProgressBy(5);
-
- i += 5;
-
- } catch (Exception e) {
-
- }
- }
-
- progress.dismiss();
-
- }
- }).start();
-
- }
5 popuWindow对话框
[csharp] view plain
copy
- Button btn_popu;
-
- public void showPopuWindow(View v){
-
- btn_popu = (Button) v;
-
- View view = LayoutInflater.from(this).inflate(R.layout.pop_window, null);
-
- PopupWindow window = new PopupWindow(this);
- window.setContentView(view);
- window.setWidth(360);
- window.setHeight(200);
-
- int[] location = new int[2];
-
- btn_popu.getLocationInWindow(location);
- window.setFocusable(true);
- window.setBackgroundDrawable(getResources().getDrawable(R.drawable.back_null));
- window.showAtLocation(btn_popu, Gravity.LEFT |Gravity.TOP , location[0]+ btn_popu.getWidth(), location[1] + 0 );
-
-
-
- ImageView img_start = (ImageView) view.findViewById(R.id.img_start);
- img_start.setOnClickListener(new View.OnClickListener() {
-
- @Override
- public void onClick(View v) {
-
- showToastMsg("点击了启动");
- }
- });
-
- }