Android开发06—菜单与对话框(下)

androidstudyroom 2011-05-16

Android开发06—菜单与对话框(下)

1.进度对话框

ProgressDialog可以显示进度轮和进度条,由于ProgressDialog继承自AlertDialog,所以在进度对话框中也可以添加按钮。

实例说明进度对话框的用法:

package qijia.si;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ContextMenuDemo extends Activity {
	//声明进度对话框id
	final int PROGRESS_DIALOG = 0;
	//Handler消息类型
	final int INCREASE = 0;
	//进度对话框对象引用
	ProgressDialog pd;
	//Handler对象引用
	Handler myHandler;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //获得Button对象
        Button bt = (Button)this.findViewById(R.id.button1);
        //设置onClickListener监听器
        bt.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				// TODO Auto-generated method stub
				showDialog(PROGRESS_DIALOG);
			}
        	
        });
        //创建handler对象
        myHandler = new Handler(){

			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				switch(msg.what){
				case INCREASE:
					pd.incrementProgressBy(1);
					//如果进度走完就关闭
					if(pd.getProgress()>=100){
						pd.dismiss();
					}
					break;
				}
				super.handleMessage(msg);
			}
        	
        };
    }  
    //重写onCreateDialog方法
    public Dialog onCreateDialog(int id){
    	switch(id){
    	//创建进度对话框
    	case PROGRESS_DIALOG:
    		pd = new ProgressDialog(this);
    		//设置进度对话框
    		pd.setMax(100);
    		pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    		pd.setTitle(R.string.title);
    		pd.setCancelable(false);
    		new Thread(){
    		public void run(){
				while(true){
					//发送Handler消息
					myHandler.sendEmptyMessage(INCREASE);
					if(pd.getProgress()>=100){
						break;
					}
					try{
						Thread.sleep(100);
					}catch(Exception e){
						e.printStackTrace();
					}
				}
			}
		}.start();
    		break;
    	}
		return pd;
    	
    }
    //每次弹出对话框时被动态回调以动态更新对话框
    public void onPrePareDialog(int id,Dialog dialog){
    	super.onPrepareDialog(id, dialog);
    	switch(id){
    	case PROGRESS_DIALOG:
    		//对话框进度清零
    		pd.incrementProgressBy(-pd.getProgress());
    		//匿名对象
    		new Thread()
    		{
    			public void run(){
    				while(true){
    					//发送Handler消息
    					myHandler.sendEmptyMessage(INCREASE);
    					if(pd.getProgress()>=100){
    						break;
    					}
    					try{
    						Thread.sleep(100);
    					}catch(Exception e){
    						e.printStackTrace();
    					}
    				}
    			}
    		}.start();
    		break;
    		}
    }
    
}

2.消息提示

1)Toast的使用

Toast向用户提供比较快速的即时消息,当Toast被显示时,虽然其悬浮于应用程序的最上方,但是Toast从不获得焦点。因为涉及Toast时就是为了让其在提示有用信息时尽量不显眼。Toast用于用户某项设置成功等。

Toast对象的创建通过Toast类的静态方法makeText来实现,该方法有两个重载实现,主要的不同时一个接受字符串,而另一个接收字符串的资源标识符作为参数。Toast对象创建好之后通过show方法即可将消息提示显示到屏幕上。

实例:

package qijia.si;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class ContextMenuDemo extends Activity {

	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button btn = (Button) this.findViewById(R.id.button1);
        btn.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				// TODO Auto-generated method stub
				//创建ImageView
				ImageView iv = new ImageView(ContextMenuDemo.this);
				iv.setImageResource(R.drawable.header);
				//创建一个线性布局
				LinearLayout ll = new LinearLayout(ContextMenuDemo.this);
				
				Toast toast = Toast.makeText(ContextMenuDemo.this, "Hello World", Toast.LENGTH_LONG);
				toast.setGravity(Gravity.CENTER, 0, 0);
				View toastView = toast.getView();
				ll.setOrientation(LinearLayout.HORIZONTAL);
				//将ImageView添加到线性布局
				ll.addView(iv);
				//将Toast的View添加到线性布局
				ll.addView(toastView);
				toast.setView(ll);
				//显示toast
				toast.show();
			}
        	
        });
    }
    
}
2) Notification

Notification是另一种消息提示的方式。Notification位于手机的状态栏,在Android手机中用手指按下状态栏并往下拉可以打开状态栏查看系统的提示信息。

实例:

main.xml:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent" android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:orientation="vertical">
        <Button android:layout_height="wrap_content" android:id="@+id/button1" android:text="@string/btn" android:layout_width="fill_parent"></Button>
    </LinearLayout>
   

notified.xml

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
        <EditText android:layout_height="wrap_content" android:id="@+id/editText1" android:text="@string/tv" android:layout_width="fill_parent" android:editable="false" android:layout_gravity="center_horizontal"></EditText>
    </LinearLayout>

1.Activity

package qijia.si;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ContextMenuDemo extends Activity {

	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button btn = (Button)findViewById(R.id.button1);
        //添加监听
        btn.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(ContextMenuDemo.this,NotifiedActivity.class);
				//建立一个新的Activity
				PendingIntent pi = PendingIntent.getActivity(ContextMenuDemo.this, 0, i, 0);
				//创建一个Notification对象
				Notification myNotification = new Notification();
				myNotification.icon = R.drawable.header;
				//设置文字和声音
				myNotification.tickerText = getResources().getString(R.string.Notification);
				myNotification.defaults = Notification.DEFAULT_SOUND;
				myNotification.setLatestEventInfo(ContextMenuDemo.this, "Example", "Hello World", pi);
				//建立NotificationManger并发送消息
				NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
				notificationManager.notify(0,myNotification);
			}
        	
        });
    }
    
}
2. Activity
package qijia.si;

import android.app.Activity;
import android.os.Bundle;

//当用户在状态栏点击Notification时会启动该Activity
public class NotifiedActivity extends Activity{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.notified);
	}
	

}

注意最后在AndroidManifest.xml中添加新的Acitivity,否则无法显示。

相关推荐