Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android對話框(二)ProgressDialog

Android對話框(二)ProgressDialog

編輯:關於Android編程

main.xml



    
custom_dialog.xml




    

    


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();
			}
		});
	}
}
\\\

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved