Blog chia sẻ kinh nghiệm lập trình Android

Chào các bạn hôm nay mình sẽ hướng dẫn các bạn làm thế nào đề xuất ra một thông báo ẩn dạng logcat cho người lập trình biết đoạn code nó hoạt động như thế nào. giống như System.out.printf(" thông báo");
ảnh minh họa :

   


sau khi bấn nút tính thì sẽ hiện lên thông báo trên phần output. phần này thông báo cho người lập trình biết , người dùng ứng dụng không hề biết.




vậy trong android làm thế nào để bạn có thể làm được điều đó, hôm nay mình sẽ hướng dẫn cho các bạn biết.
 Đầu tiên tạo 1 project sau đó thêm cho mình một button "OK" vào để hiển thị thông báo khi click.



Bạn sang main.class thực hiện như sau : 



import log từ import android.util.Log;


Bây giờ bạn chạy ứng dụng và click nút OK.



Phần thông báo sẽ xuất hiện.
  


Đây là cách kiểm tra các thông số mà chúng ta không muốn hiển thị lên màn hình hoặc bất kì điều gì bạn muốn , cũng khá đơn giản.
Chúc các bạn thành công.

full code xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="90dp"
        android:layout_toRightOf="@+id/textView1"
        android:text="ok" />

</RelativeLayout>

fullcode java


package com.example.show_logv;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// khai báo button 
Button btok = (Button)findViewById(R.id.button1);
//event button
btok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.v("check lỗi", "bạn đã biết cách sử dụng Log.v để kiểm tra code");
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}


Mọi thắc mắc các bạn liên hệ mình qua địa chỉ Gmail : svk10acntt@gmail.com hoặc Fb: John Ly Phạm







0 comments:

Post a Comment

http://knlaptrinhandroid.blogspot.com/

 
Top