mercredi 22 juillet 2015

checkbox filter in android

I'm trying to learn more about the checkbox functions in android. I want to display the values of the checked Checkboxes in TextView. In my code, it shows all the checkboxes either true(if checked) or false (if not checked) but I only want to print those checkboxes which are checked and exclude those are unchecked. I tried using "if, else if" but its not working. Any help will be really appreciated.

MainActivity:

public class Demo extends Activity{
        
         private CheckBox linux, macos, windows;
          private Button button;
          private EditText ed1, ed2;
          private TextView text;
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                // TODO Auto-generated method stub
                super.onCreate(savedInstanceState);
                setContentView(R.layout.demo);
                addListenerOnButton();
        }
        
         public void addListenerOnButton() {
                 
                        linux = (CheckBox) findViewById(R.id.checkBox1);
                        macos = (CheckBox) findViewById(R.id.checkBox2);
                        windows = (CheckBox) findViewById(R.id.checkBox3);
                        ed1 = (EditText) findViewById(R.id.editText1);
                        ed2 = (EditText) findViewById(R.id.editText2);
                        button = (Button) findViewById(R.id.go);
                        text = (TextView) findViewById(R.id.textView1);
                 
                        button.setOnClickListener(new OnClickListener() {
                 
                          @Override
                          public void onClick(View v) {
                 
                                StringBuffer result = new StringBuffer();
                                result.append("Android check : ").append(linux.isChecked());
                                result.append("\nWindows OS check : ").append(macos.isChecked());
                                result.append("\niOS check :").append(windows.isChecked());
                                
                 
                                //Toast.makeText(Demo.this, result.toString(), Toast.LENGTH_LONG).show();
                        text.setText(result);
                          }
                        });
                 

}
}



Aucun commentaire:

Enregistrer un commentaire