android sdk---checkBox多選實例

應用顯示效果:點擊哪個checkBox會處理字符串的顯示和隱藏 

public CheckBox myCheckBox1;
 public CheckBox myCheckBox2;
 public CheckBox myCheckBox3;
 public TextView myTextView1;
 public TextView myTextView2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        myTextView2 = (TextView)findViewById(R.id.myTextView2);
        myCheckBox1 = (CheckBox)findViewById(R.id.checkBox1);
        myCheckBox2 = (CheckBox)findViewById(R.id.checkBox2);
        myCheckBox3 = (CheckBox)findViewById(R.id.checkBox3);
       
        myCheckBox1.setOnCheckedChangeListener(mCheckBoxChanged);
        myCheckBox2.setOnCheckedChangeListener(mCheckBoxChanged);
        myCheckBox3.setOnCheckedChangeListener(mCheckBoxChanged);
    }
    private CheckBox.OnCheckedChangeListener mCheckBoxChanged =
      new CheckBox.OnCheckedChangeListener()
    {

  @Override
  public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
   // TODO Auto-generated method stub
   String str0 = "one";
      String str1 = "two";
      String str2 = "three";
      String str = "";
      if(myCheckBox1.isChecked() == true)
      {
       str += str0;
      }
      if(myCheckBox2.isChecked() == true)
      {
       str += str1;
      }
      if(myCheckBox3.isChecked() == true)
      {
       str += str2;
      }
      myTextView2.setText(str);
  }
    };

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章