码迷,mamicode.com
首页 > 移动开发 > 详细

Android对话框(二)ProgressDialog

时间:2015-01-26 21:07:58      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:

main.xml

<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="com.example.progressdialog.MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="显示进度对话框一" />

    <Button
        android:id="@+id/button2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/button1"
        android:text="显示进度对话框二" />

    <Button
        android:id="@+id/button3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button2"
        android:text="自定义对话框的使用" />

</RelativeLayout>
custom_dialog.xml

<?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:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>
package com.example.progressdialog;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomDialog {

	private Context context;
	Dialog dialog;
	public CustomDialog(Context context){
		this.context = context;
		dialog = new Dialog(context);
	}
	public void show(){
		View view = LayoutInflater.from(context).inflate(R.layout.custom_dialog, null);
		//inflate的第二个参数为null表示当前对话框的布局没根布局
		dialog.setContentView(view);
		/*
		 * setContentView(R.layout.custom_dialog);
		 * 也可以以这种方式加载
		 */
		dialog.setTitle("自定义的对话框");
		ImageView imageView = (ImageView) view.findViewById(R.id.imageView1);
		imageView.setImageResource(R.drawable.ic_launcher);
		TextView textView = (TextView) view.findViewById(R.id.textView1);
		textView.setText("这是自定义对话框");
		dialog.show();
	}
}
package com.example.progressdialog;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

	private Button button1;
	private Button button2;
	private Button button3;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		button1 = (Button) findViewById(R.id.button1);
		button2 = (Button) findViewById(R.id.button2);
		button3 = (Button) findViewById(R.id.button3);
		button1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				/*
				 * ProgressDialog.show(MainActivity.this, "提示", "正在加载,请稍后...");
				 * 这样点击回退按钮没反应
				 */
				ProgressDialog dialog = new ProgressDialog(MainActivity.this);
				dialog.setTitle("提示");
				dialog.setMessage("正在加载,请稍后...");
				dialog.show();//显示进度条,这样写点击回退按钮可退出进度对话框
			}
		});
		button2.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				/*
				 * 第一个参数:上下文
				 * 第二个参数:主题样式 int theme
				 * 第一种写法
				 * ProgressDialog dialog = new ProgressDialog(MainActivity.this, ProgressDialog.STYLE_HORIZONTAL);
				 */
				ProgressDialog dialog = new ProgressDialog(MainActivity.this);
				dialog.setTitle("下载提示");
				dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
				dialog.setProgress(50);//要显示进度,加线程
				//dialog.setCancelable(false);
				dialog.show();
			}
		});
		button3.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				CustomDialog dialog = new CustomDialog(MainActivity.this);
				dialog.show();
			}
		});
	}
}
技术分享技术分享技术分享技术分享

Android对话框(二)ProgressDialog

标签:

原文地址:http://blog.csdn.net/lindonglian/article/details/43158929

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!