ToggleButton có thể nói là dạng đặc biệt của button,nó có 2
trạng thái cơ bản là check và not check.Khi ở trạng thái check chúng ta click
nó sẽ chuyển sang trạng thái not check và ngược lại
Tùy theo phiên bản android mà hình ảnh
ToggleButton khác nhau(check và not check).
Ví dụ ngày hôm nay chỉ đơn giản khi ta click ToggleButton sẽ thay đổi hình ảnh trên ImageView thay đổi theo trạng thái ToggleButton thay đổi.
-activity_main.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" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/toggleButton1"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher"
android:contentDescription="@string/app_name"
/>
<ToggleButton
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="180dp"
android:text="ToggleButton" />
</RelativeLayout>
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" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/toggleButton1"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher"
android:contentDescription="@string/app_name"
/>
<ToggleButton
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="180dp"
android:text="ToggleButton" />
</RelativeLayout>
-MainActivity.java:
package com.example.togglebutton;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ToggleButton;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView imageView=(ImageView)
findViewById(R.id.imageView1);
final ToggleButton toggleButton=(ToggleButton)
findViewById(R.id.toggleButton1);
//Xử lí sự kiện khi click ToggleButton
toggleButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(toggleButton.isChecked()){
imageView.setImageDrawable(getResources().getDrawable(R.drawable.check));
}
else{
imageView.setImageDrawable(getResources().getDrawable(R.drawable.not_check));
}
}
});
toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), toggleButton.isChecked()?"Check":"Not Check", Toast.LENGTH_SHORT).show();//Thông báo lên toast khi trạng thái của
//ToggleButton thay đổi
}
});
}
@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;
}
}
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ToggleButton;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView imageView=(ImageView)
findViewById(R.id.imageView1);
final ToggleButton toggleButton=(ToggleButton)
findViewById(R.id.toggleButton1);
//Xử lí sự kiện khi click ToggleButton
toggleButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(toggleButton.isChecked()){
imageView.setImageDrawable(getResources().getDrawable(R.drawable.check));
}
else{
imageView.setImageDrawable(getResources().getDrawable(R.drawable.not_check));
}
}
});
toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), toggleButton.isChecked()?"Check":"Not Check", Toast.LENGTH_SHORT).show();//Thông báo lên toast khi trạng thái của
//ToggleButton thay đổi
}
});
}
@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;
}
}
-setOnCheckedChangeListener() để nhận sự kiện check thay đổi
-Thay đổi text hiển thị:
android:textOn text trên ToggleButton khi ở trạng thái
check.
android:textOn text trên ToggleButton khi ở trạng thái notcheck.
Download source code:ToggleButton
android:textOn="Check"
android:textOff="Not Check"
android:textOff="Not Check"
Download source code:ToggleButton