CheckBox的使用(一):onCheckedChanged事件

  • Post author:
  • Post category:其他


重写接口

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)

package com.example.androidtest;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.CompoundButton;
import android.widget.CheckBox;
import android.util.Log;

public class MainActivity extends Activity implements CompoundButton.OnCheckedChangeListener {
	
	CheckBox cb;
	
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		cb = (CheckBox)findViewById(R.id.check);
		cb.setOnCheckedChangeListener(this);
	}	
	
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
	{
		if (isChecked) {
			cb.setText("This checkbox is: checked");
		} else {
			cb.setText("This checkbox is: unchecked");
		}
	}
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/check"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="This checkbox is: unchecked" />



版权声明:本文为wuli2496原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。