Android中级教程之----Log图文详解

android.util.Log常用的方法有以下5个:Log.v() Log.d() Log.i() Log.w() 以及 Log.e() 。根据首字母对应VERBOSE,DEBUG,INFO, WARN,ERROR。

1、Log.v 的调试颜色为黑色的,任何消息都会输出,这里的v代表verbose啰嗦的意思,平时使用就是Log.v("","");

2、Log.d的输出颜色是蓝色的,仅输出debug调试的意思,但他会输出上层的信息,过滤起来可以通过DDMS的Logcat标签来选择.

3、Log.i的输出为绿色,一般提示性的消息information,它不会输出Log.v和Log.d的信息,但会显示i、w和e的信息

4、Log.w的意思为橙色,可以看作为warning警告,一般需要我们注意优化Android代码,同时选择它后还会输出Log.e的信息。

5、Log.e为红色,可以想到error错误,这里仅显示红色的错误信息,这些错误就需要我们认真的分析,查看栈的信息了。

下面是我做的一个简单的LogDemo(Step By Step):

Step 1:准备工作(打开LogCat视窗).

 启动Eclipse,在Window->Show View会出来一个对话框,当我们点击Ok按钮时,会在控制台窗口出现LogCat视窗.如下图:

 

 

Step 2:新建一个Android工程,命名为LogDemo.

Step 3:设计UI界面,我们在这里就加了一个Button按钮(点击按钮出现Log日志信息).

Main.xml代码如下:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" 
  6.     > 
  7. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="@string/hello" 
  11.     /> 
  12. <Button 
  13.  android:id="@+id/bt" 
  14.  android:layout_width="wrap_content" 
  15.  android:layout_height="wrap_content" 
  16.  android:text="Presse Me Look Log" 
  17. /> 
  18. </LinearLayout> 
  19.  

Step 4:设计主类LogDemo.java,代码如下:

  1. package com.android.test;  
  2.  
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.util.Log;  
  6. import android.view.View;  
  7. import android.widget.Button;  
  8.  
  9. public class LogDemo extends Activity {  
  10.     
  11.  private static final String ACTIVITY_TAG="LogDemo";  
  12.  private Button bt;  
  13.     public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.main);  
  16.         //通过findViewById找到Button资源  
  17.         bt = (Button)findViewById(R.id.bt);  
  18.         //增加事件响应  
  19.         bt.setOnClickListener(new Button.OnClickListener(){  
  20.     @Override 
  21.    public void onClick(View v) {  
  22.     Log.v(LogDemo.ACTIVITY_TAG, "This is Verbose.");  
  23.     Log.d(LogDemo.ACTIVITY_TAG, "This is Debug.");  
  24.     Log.i(LogDemo.ACTIVITY_TAG, "This is Information");  
  25.     Log.w(LogDemo.ACTIVITY_TAG, "This is Warnning.");  
  26.     Log.e(LogDemo.ACTIVITY_TAG, "This is Error.");  
  27.    }  
  28.            
  29.         });  
  30.     }  
  31.           
  32. }  
  33.  

Step 5:运行LogDemo工程,效果如下:

 

当我们点击按钮时,会触发事件,在Logcat视窗下有如下效果:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章