安卓 log 的用法

今天我就把从网上以及SDK里东拼西凑过来,让大家先一睹为快,希望对大家入门AndroidLog有一定的帮助.

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

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

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

3Log.i的输出为绿色,一般提示性的消息information,它不会输出Log.vLog.d的信息,但会显示iwe的信息

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

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

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

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

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


 

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

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

Main.xml代码如下:

<?xmlversion="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/hello"

/ >

<Button

android:id="@+id/bt"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="PresseMe Look Log"

/>

</LinearLayout >

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

packagecom.android.test;

importandroid.app.Activity;

importandroid.os.Bundle;

importandroid.util.Log;

importandroid.view.View;

importandroid.widget.Button;

publicclass LogDemo extends Activity {

privatestatic final String ACTIVITY_TAG="LogDemo";

privateButton bt;

publicvoid onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

//通过findViewById找到Button资源

bt= (Button)findViewById(R.id.bt);

//增加事件响应

bt.setOnClickListener(newButton.OnClickListener(){

@Override

publicvoid onClick(View v) {

Log.v(LogDemo.ACTIVITY_TAG,"This is Verbose.");

Log.d(LogDemo.ACTIVITY_TAG,"This is Debug.");

Log.i(LogDemo.ACTIVITY_TAG,"This is Information");

Log.w(LogDemo.ACTIVITY_TAG,"This is Warnning.");

Log.e(LogDemo.ACTIVITY_TAG,"This is Error.");

}

});

}

}

Step5:运行LogDemo工程,效果如下:

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值