android CheckBox控件的简单使用方法

 研究了一下CheckBox选择按钮,下面就以喜欢颜色为素材,做个记录,使用TableRow实现的;

 

 

1.布局activity_layout.xml:


     
        
        
            
            
            
        
    
    



     
        
        
            
            
            
        
    
    

colors.xml


#D2691E
#D2691E

 

下面我们来看Activity的代码实现;

public class MainActivity extends Activity{
    private TextView show;
    private CheckBox box1,box2,box3;
    private String love= "我喜欢色是";
    private String not= "不是我喜欢的";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout);
        show = (TextView) findViewById(R.id.show);
        box1 = (CheckBox) findViewById(R.id.box1);
        box2 = (CheckBox) findViewById(R.id.box2);
        box3 = (CheckBox) findViewById(R.id.box3);
        box1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    if(show.getVisibility()==View.VISIBLE){
                        show.setText(love+box1.getText());
                        textColor();
                    }else {
                        show.setVisibility(View.VISIBLE);
                        show.setText(love+box1.getText());
                        textColor();
                    }
                }else{
                    show.setText(not);
                    setTextBlack();
                }
            }
        });
        
        //box2 and box3 和box1的写法一样,在这就不重复写了;
    
    
    private void textColor() {
        box3.setTextColor(getApplicationContext().getColor(R.color.chocolate));
    }
    private void setTextBlack() {
        box3.setTextColor(getApplicationContext().getColor(R.color.black));
    }
}
}

大功告成,看一下效果图:

 

android CheckBox控件的简单使用方法_第1张图片

 

比较简单,没事了随便写写,希望有帮助?

 

谢谢!

你可能感兴趣的:(android,研发)