Defining custom-checkbox in android

后端 未结 4 1892
悲哀的现实
悲哀的现实 2020-12-09 06:38

How to make a custom check-Box in android

my current XML::



        
4条回答
  •  失恋的感觉
    2020-12-09 07:16

    For your requirement I prefer you to use CheckedTextView instead of CheckBox.Here is the code what you wanted.

         
    

    create 3 xml ( chk_indicator.xml , chk_bg.xml , chk_pressed_bg.xml )in drawable folder

    chk_indicator.xml

    
    
    
    
    
    
    
    

    chk_bg.xml

    
    
    
        
    
             
    
             
    
            
        
        
    
    

    chk_pressed_bg.xml

     
     
        
        
    
             
    
             
    
            
        
        
     
    

    output:

    set onClick event on CheckedTextView

     ((CheckedTextView)findViewById(R.id.ctv)).setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View arg0) {
    
                boolean isChecked = ((CheckedTextView)findViewById(R.id.ctv)).isChecked();
    
                if(isChecked)
                  ((CheckedTextView)findViewById(R.id.ctv)).setChecked(false);
                else
                  ((CheckedTextView)findViewById(R.id.ctv)).setChecked(true);
    
    
    
    
            }
        });
    

提交回复
热议问题