【android基础学习之七】——常用效果2

晨曦之光 发布于 2012/03/08 14:19
阅读 1K+
收藏 2

  声明:学习的书籍《Android应用开发揭秘》,这里记录学习该书籍的日志,引用的相关代码与总结描述,没有商业的用途,完全是自我学习的一个记录,刚刚学习不可避免会出现很多问题,若是有错误还请大家多多批评。

2011-10-30周日,继续《Android应用开发揭秘》的学习,接上一篇常用效果的学习;

一、    进度条(ProgressBar)

进度条作为后台程序处理过程中,反馈给使用者的一个很好的凭证,来显示当前程序处理的怎么样,进度如何等情况。Android中一共有两种样式进度条:长形进度条与圆形进度条。而且有的程序也可以在标题栏显示进度条。

 

在我们Eclipse开发android程序中,在编辑main.xml文件时,也提供了图形化界面的编辑,如下图:


实例分析:通过一个开始按钮的点击,显示圆形与长形进度条的进度。

 

关键源码:

main.xml布局文件:

<ProgressBar
    android:id="@+id/ProgressBar01"
	style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:visibility="gone"
  />
  <ProgressBar 
  		android:id="@+id/ProgressBar02"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        style="?android:attr/progressBarStyleLarge"
        android:max="100"
        android:progress="50"
        android:secondaryProgress="70"
		android:visibility="gone"
  />

【注意】该实例关键的是对ProgressBar的控制,之前例子中已经将过若是通过Handler实例的sendMessage()方法进而触发handleMessage(Message mesg)方法:

	//当按钮按下时开始执行,
	    mButton01.setOnClickListener(new Button.OnClickListener(){
	      public void onClick(View v){	  
	    	  m_ProgressBar.setVisibility(View.VISIBLE);//设置ProgressBar为可见状态
	    	  m_ProgressBar2.setVisibility(View.VISIBLE);
	    	  m_ProgressBar.setMax(100);		//设置ProgressBar的最大值
	    	  m_ProgressBar.setProgress(0); 		//设置ProgressBar当前值
	    	  m_ProgressBar2.setProgress(0);

	    	  //通过线程来改变ProgressBar的值
	    	  new Thread(new Runnable() {
			public void run(){
				for (int i = 0; i < 10; i++){
					try{
						intCounter = (i + 1) * 20;
						Thread.sleep(1000);
						if (i == 4){
							Message m = new Message();
							m.what = Activity01.GUI_STOP_NOTIFIER;
							Activity01.this.myMessageHandler.sendMessage(m);
							break;
						}else{
							Message m = new Message();
							m.what = Activity01.GUI_THREADING_NOTIFIER;
							Activity01.this.myMessageHandler.sendMessage(m);
						}
					}catch (Exception e){
						e.printStackTrace();
					}
				}
			}
		}).start();
	}
		});
	}

	  Handler myMessageHandler = new Handler(){
		  public void handleMessage(Message msg){
			  switch (msg.what){		  
			  case Activity01.GUI_STOP_NOTIFIER:	//ProgressBar已经是对大值
				  m_ProgressBar.setVisibility(View.GONE);
				  m_ProgressBar2.setVisibility(View.GONE);
				  Thread.currentThread().interrupt();
				  break;
			  case Activity01.GUI_THREADING_NOTIFIER:
				  if (!Thread.currentThread().isInterrupted()){	  
					m_ProgressBar.setProgress(intCounter);// 改变ProgressBar的当前值
					m_ProgressBar2.setProgress(intCounter);
					setProgress(intCounter*100);	// 设置标题栏中前景的一个进度条进度值		
					setSecondaryProgress(intCounter*100);//设置标题栏中后面的一个进度条进度值 
				  }
				  break;
			  }
			  super.handleMessage(msg);
		 }
	  }; 

实例效果:

  

二、    拖动条(SeekBar)

拖动条主要用于程序中,对一些属性的调节,如:音效大小。在Android中实现还是比较容易,SeekBar控件,而且只需要监听该控件的三个事件:

数值改变(onProgressChanged);

开始拖动(onStartTrackingTouch);

停止拖动(onStopTrackingTouch);

 

其控件配置也比较简单:

<SeekBar android:id="@+id/seek"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="50"
android:secondaryProgress="75" />

效果图:

三、状态栏提示(Notification,NotificationManager)

当手机有未接电话或者短信息时,手机顶部状态栏就会显示一个小图标,用来显示用户有没有处理的快讯。NotificationManager用来管理状态栏的信息,而Notification用来处理这些快讯信息。

NotificationManager对象的获取通过gerSystenService方法,Notification对象可以设置其内容,图标,标题等属性。然后通过notify方法来执行一个Notification快讯。

实例分析:当用户点击一个按钮,就发出一个Notification快讯,这是手机顶部状态栏显示相应提示信息。展开状态栏,点击快讯信息,跳转到处理界面。

关键源码:

//初始化NotificationManager对象
        m_NotificationManager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
        
        //获取四个按钮对象
        mButton1 = (Button) this.findViewById(R.id.Button01);
        mButton2 = (Button) this.findViewById(R.id.Button02);
        mButton3 = (Button) this.findViewById(R.id.Button03);
        mButton4 = (Button) this.findViewById(R.id.Button04);
        
        //点击通知时转移内容
        m_Intent = new Intent(Activity01.this, Activity02.class);
        //主要是设置点击通知时显示内容的类
        m_PendingIntent = PendingIntent.getActivity(Activity01.this, 0, m_Intent, 0);
        //构造Notification对象
        m_Notification = new Notification();
        mButton1.setOnClickListener(new Button.OnClickListener() {
		public void onClick(View v) {
			//设置通知在状态栏显示的图标
			m_Notification.icon = R.drawable.img1;	
			//当我们点击通知时显示的内容
			m_Notification.tickerText="Button1通知内容..........";
			//通知时发出默认的声音
			m_Notification.defaults = Notification.DEFAULT_SOUND;
			//设置通知显示的参数
			m_Notification.setLatestEventInfo(Activity01.this, "Button1", "Button1通知", m_PendingIntent);
			//可以理解为执行这个通知
			m_NotificationManager.notify(0,m_Notification);
		}
	});

其中,notify()方法:

public void notify (int id, Notification notification)

Post a notification to be shown in the status bar. If a notification with the same id has already been posted by your application and has not yet been canceled, it will be replaced by the updated information.

Parameters

id

An identifier for this notification unique within your application.

notification

A Notification object describing what to show the user. Must not be null.

 

 实例效果图:

  

四、对话框中的进度条(ProgressDialog)

对话框中的进度条,可以设置图标,内容等属性。

 

实例分析:通过点击两个按钮,显示对话框中得两种进度条。

关键源码:

//设置mButton01的事件的监听
mButton01.setOnClickListener(new Button.OnClickListener() {
	public void onClick(View v) {
		m_pDialog = new ProgressDialog(Examples_04_24Activity.this);//创建ProgressDialog对象
		//设置进度条风格,风格为圆形,旋转的
		m_pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		m_pDialog.setTitle("提示");				//设置ProgressDialog标题
		m_pDialog.setMessage("这是一个圆形进度条对话框");		//设置ProgressDialog提示信息
		m_pDialog.setIcon(R.drawable.img1);			//设置ProgressDialog标题图标
		m_pDialog.setIndeterminate(false);			//设置ProgressDialog的进度条是否不明确
		m_pDialog.setCancelable(true);			//设置PrgoressDialog是否可以按退回键取消
		m_pDialog.setButton("确定", new DialogInterface.OnClickListener() {//设置ProgressDialog的一个Button
			public void onClick(DialogInterface dialog, int which){
				dialog.cancel();
			}
		});
				
		//让ProgressDialog显示
		m_pDialog.show();
	}
});
        
//设置mButton02的事件监听
mButton02.setOnClickListener(new Button.OnClickListener() {
	public void onClick(View v) {
		m_count = 0;
		m_pDialog = new ProgressDialog(Examples_04_24Activity.this);//创建ProgressDialog对象
		m_pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//设置进度条风格,风格为长形
		......
		m_pDialog.show();// 让ProgressDialog显示
		new Thread() {
			public void run(){
				try{
					while (m_count <= 100){ 
						// 由线程来控制进度。
						m_pDialog.setProgress(m_count++);
						Thread.sleep(100); 
					}
					m_pDialog.cancel();
				}catch (InterruptedException e){
					m_pDialog.cancel();
				}
			}
		}.start();
	}
});

实例效果:

 

今天就实例效果学习结束了,关于android的学习,下面把布局学习结束后,基础的学习就结束了。加油!

 


原文链接: http://blog.csdn.net/yinyuan1987/article/details/6921670
加载中
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部