Pages

Thứ Ba, 18 tháng 6, 2019

Listener Class

Bắt sự kiện thông qua lớp lắng nghe (Listener Class)

Thay vì ở cách 3 bạn implement trực tiếp cho class MainActivity thì ở đây bạn tạo một class inner bên trong MainActivity và implement interface OnClickListener là xong, giao diện mình sử dụng lại ở ví dụ 3 nhé.

Ví dụ: Chúng ta có project tên là ListenerClass trong có 2 button, chúng ta sẽ tạo sự kiện cho 2 button này, dưới đây là tập tin 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="hiepsiit.com.MainActivity" >           <Button          android:id="@+id/btnLogin"          android:layout_width="wrap_content"          android:layout_height="wrap_content"                  android:layout_marginLeft="14dp"          android:layout_marginTop="57dp"          android:text="Login" />        <Button          android:id="@+id/btnLogout"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_alignBaseline="@+id/btnLogin"          android:layout_alignBottom="@+id/btnLogin"          android:layout_marginLeft="33dp"          android:layout_toRightOf="@+id/btnLogin"          android:text="Logout" />    </RelativeLayout>

Trong MainActivity.java chúng ta tạo một class con EventClass implements OnClickListener:

  class EventClass implements OnClickListener{  		           @Override          public void onClick(View v) {          	switch(v.getId()){      		case R.id.btnLogin:      			// Hiện thị thông báo trong vòng vài giây      			Toast.makeText(getApplication(), "Hiệp Sĩ IT: Bạn đang Click vào Button Login", Toast.LENGTH_LONG).show();      			break;      		case R.id.btnLogout:      			Toast.makeText(getApplication(), "Hiệp Sĩ IT: Bạn đang Click vào Button Logout", Toast.LENGTH_LONG).show();      			break;      		}          }

Xử lý sự kiện cho từng Button :

  btnLogin.setOnClickListener(new EventClass());  btnLogout.setOnClickListener(new EventClass());

Toàn bộ code trong tập tin MainActivity.java

  package hiepsiit.com;    import android.app.Activity;  import android.os.Bundle;  import android.view.Menu;  import android.view.MenuItem;  import android.view.View;  import android.view.View.OnClickListener;  import android.widget.Button;  import android.widget.Toast;    public class MainActivity extends Activity {  	Button btnLogin, btnLogout;  	@Override  	protected void onCreate(Bundle savedInstanceState) {  		super.onCreate(savedInstanceState);  		setContentView(R.layout.activity_main);  		btnLogin	= (Button)findViewById(R.id.btnLogin);  		btnLogout	= (Button)findViewById(R.id.btnLogout);  		btnLogin.setOnClickListener(new EventClass());  		btnLogout.setOnClickListener(new EventClass());  	}  	class EventClass implements OnClickListener{  		           @Override          public void onClick(View v) {          	switch(v.getId()){      		case R.id.btnLogin:      			// Hiện thị thông báo trong vòng vài giây      			Toast.makeText(getApplication(), "Hiệp Sĩ IT: Bạn đang Click vào Button Login", Toast.LENGTH_LONG).show();      			break;      		case R.id.btnLogout:      			Toast.makeText(getApplication(), "Hiệp Sĩ IT: Bạn đang Click vào Button Logout", Toast.LENGTH_LONG).show();      			break;      		}          }      }  	  }  

Download ví dụ

Ứng dụng này được phát triển bởi adt bundleandroid 4.2 sử dụng minimum sdk 11  and target sdk 21.


Kết quả khi click vào button Login:

Kết quả khi click vào button Logout:

 



Cập nhật công nghệ từ Youtube tại link: https://www.youtube.com/channel/UCOxeYcvZPGf-mGLYSl_1LuA/videos
Để tham gia khóa học công nghệ truy cập link: http://thuvien.hocviendaotao.com
Mọi hỗ trợ về công nghệ email: dinhanhtuan68@gmail.com