Getting started with Android. Multi-select button CheckBox

CheckBox


1. In the XML file, use the <CheckBox/> tag
<CheckBox
        android:id="@+id/eat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="eat"
        />
    <CheckBox
        android:id="@+id/sleep"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="sleep"
        />
    <CheckBox
        android:id="@+id/dota"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="dota"
        />


2. In the code, use the object of CheckBox to represent
private CheckBox eatBox;
	private CheckBox sleepBox;
	private CheckBox dotaBox;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate (savedInstanceState);
		setContentView(R.layout.activity_main);
		
		eatBox=(CheckBox)findViewById(R.id.eat);
		sleepBox=(CheckBox)findViewById(R.id.sleep);
		dotaBox=(CheckBox)findViewById(R.id.dota);
	}


3. Common listeners OnClickListener interface and OnCheckedChangeListener interface
Use classes to implement these interfaces



//OnClickListener
class CheckBoxClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {//view is the parent class of CheckBox, so CheckBox can be upcast
			if (v.getId() == R.id.eat) {
				System.out.println("eat");
			} else if (v.getId() == R.id.sleep) {
				System.out.println("sleep");
			} else if (v.getId() == R.id.dota) {
				System.out.println("dota");
			}
		}

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate (savedInstanceState);
		setContentView(R.layout.activity_main);

		eatBox = (CheckBox) findViewById(R.id.eat);
		sleepBox = (CheckBox) findViewById(R.id.sleep);
		dotaBox = (CheckBox) findViewById(R.id.dota);

		CheckBoxClickListener checkBoxClickListener = new CheckBoxClickListener();
		eatBox.setOnClickListener(checkBoxClickListener);
		sleepBox.setOnClickListener(checkBoxClickListener);//Listener binding controls
		dotaBox.setOnClickListener(checkBoxClickListener);
	}	

================


//implement OnCheckedChangeListener interface
	// radio button, isChecked (checked) or UnCheck
	class CheckBoxChange implements OnCheckedChangeListener {

		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			if (buttonView.getId() == R.id.dota) {
				System.out.println("dotaBox");
			} else if (buttonView.getId() == R.id.eat) {
				System.out.println("eatBox");
			} else if (buttonView.getId() == R.id.sleep) {
				System.out.println("sleepBox");
			}

			if (isChecked) {
				System.out.println("checked");
			} else {
				System.out.println("Unchecked");
			}
		}

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate (savedInstanceState);
		setContentView(R.layout.activity_main);

		eatBox = (CheckBox) findViewById(R.id.eat);
		sleepBox = (CheckBox) findViewById(R.id.sleep);
		dotaBox = (CheckBox) findViewById(R.id.dota);

		// CheckBoxClickListener checkBoxClickListener = new
		// CheckBoxClickListener();
		// eatBox.setOnClickListener(checkBoxClickListener);//Listener binding controls
		// sleepBox.setOnClickListener(checkBoxClickListener);//Listener binding controls
		// dotaBox.setOnClickListener(checkBoxClickListener);//Listener binding controls

		CheckBoxChange checkBoxChange = new CheckBoxChange();

		eatBox.setOnCheckedChangeListener(checkBoxChange);//Listener binding controls
		sleepBox.setOnCheckedChangeListener(checkBoxChange);//Listener binding controls
		dotaBox.setOnCheckedChangeListener(checkBoxChange);//Listener binding controls
	}	

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327011570&siteId=291194637