android基础三之CheckBox

//activity_main.xml


    
strings.xml


定边网站制作公司哪家好,找成都创新互联!从网页设计、网站建设、微信开发、APP开发、成都响应式网站建设公司等网站项目制作,到程序开发,运营维护。成都创新互联从2013年开始到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选成都创新互联

    CheckBox
    Hello world!
    Settings
    篮球

MainActivity.java
package com.vincentlin.checkbox;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class MainActivity extends Activity {
 private CheckBox checkbox;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  //初始化checkbox
  checkbox = (CheckBox) findViewById(R.id.checkBox1);
  //通过设置checkbox的监听事件来对checkbox是不是被选中
  checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
   
   @Override
   public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    //通过onCheckedChanged来监听当前的checkBox是否被选中
    Log.i("tag", isChecked+"");
    if (isChecked) {
     //获得checkBox的文本内容
     String text = checkbox.getText().toString();
     Log.i("tag", text);
    } 
   }
  });
 }

}

分享文章:android基础三之CheckBox
网页链接:http://ybzwz.com/article/pephso.html