<p>在调试代码的时候我们需要查看调试信息,那我们就需要用Android Log类。</p><p>android.util.Log常用的方法有以下5个:Log.v() Log.d() Log.i() Log.w() 以及 Log.e() 。根据首字母对应VERBOSE,DEBUG,INFO, WARN,ERROR。</p><p>1、Log.v 的调试颜色为黑色的,任何消息都会输出,这里的v代表verbose啰嗦的意思,平时使用就是Log.v(&quot;&quot;,&quot;&quot;);</p><p>2、Log.d的输出颜色是蓝色的,仅输出debug调试的意思,但他会输出上层的信息,过滤起来可以通过DDMS的Logcat标签来选择.</p><p>3、Log.i的输出为绿色,一般提示性的消息information,它不会输出Log.v和Log.d的信息,但会显示i、w和e的信息</p><p>4、Log.w的意思为橙色,可以看作为warning警告,一般需要我们注意优化Android代码,同时选择它后还会输出Log.e的信息。</p><p>5、Log.e为红色,可以想到error错误,这里仅显示红色的错误信息,这些错误就需要我们认真的分析,查看栈的信息了。</p><p>注意:不同的打印方法在使用时都是某个方法带上(String tag, String msg)参数,tag表示的是打印信息的标签,msg表示的是需要打印的信息。</p><p>下面是我做的一个简单的LogDemo(Step By Step):</p><p>Step 1:准备工作(打开LogCat视窗).</p><p>启动Eclipse,在Window-&gt;Show View会出来一个对话框,当我们点击Ok按钮时,会在控制台窗口出现LogCat视窗.如下图:</p><p><img src="/up_pic/201809/020951179692.gif" title="020951179692.gif" alt="1.gif"/></p><p><img src="/up_pic/201809/020951233133.gif" title="020951233133.gif" alt="2.gif"/></p><p>Step 2:新建一个Android工程,命名为LogDemo.</p><p>Step 3:设计UI界面,我们在这里就加了一个Button按钮(点击按钮出现Log日志信息).</p><p>Main.xml代码如下:</p><p></p><p>[xhtml] view plain copy</p><pre class="brush:html;toolbar:false">&lt;?xmlversion=&quot;1.0&quot;encoding=&quot;utf-8&quot;?&gt; &lt;LinearLayoutxmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; android:orientation=&quot;vertical&quot; android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;fill_parent&quot; &gt; &lt;TextView android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;wrap_content&quot; android:text=&quot;@string/hello&quot; /&gt; &lt;Button android:id=&quot;@+id/bt&quot; android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot; android:text=&quot;PresseMeLookLog&quot; /&gt; &lt;/LinearLayout&gt;</pre><p>Step 4:设计主类LogDemo.java,代码如下:</p><p>[java] view plain copy</p><pre class="brush:java;toolbar:false">publicclassLogDemoextendsActivity{ privatestaticfinalStringACTIVITY_TAG=&quot;LogDemo&quot;; privateButtonbt; publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); //通过findViewById找到Button资源 bt=(Button)findViewById(R.id.bt); //增加事件响应 bt.setOnClickListener(newButton.OnClickListener(){ @Override publicvoidonClick(Viewv){ Log.v(LogDemo.ACTIVITY_TAG,&quot;ThisisVerbose.&quot;); Log.d(LogDemo.ACTIVITY_TAG,&quot;ThisisDebug.&quot;); Log.i(LogDemo.ACTIVITY_TAG,&quot;ThisisInformation&quot;); Log.w(LogDemo.ACTIVITY_TAG,&quot;ThisisWarnning.&quot;); Log.e(LogDemo.ACTIVITY_TAG,&quot;ThisisError.&quot;); } }); } }</pre><p>Step 5:运行LogDemo工程,效果如下:</p><p><img src="/up_pic/201809/020951377102.gif" title="020951377102.gif" alt="3.gif"/></p><p>当我们点击按钮时,会触发事件,在Logcat视窗下有如下效果:</p><p><img src="/up_pic/201809/020951453658.gif" title="020951453658.gif" alt="4.gif"/></p>
T:0.006755s,M:248.02 KB
返回顶部 留言