AndroidマルチセレクトボタンCheckBoxの使用
6063 ワード
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.sadhu.s01_e09_checkbox.MainActivity$PlaceholderFragment" >
<CheckBox
android:id="@+id/eatId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" " />
<CheckBox
android:id="@+id/sleepId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" " />
<CheckBox
android:id="@+id/dotaId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="dota" />
<CheckBox
android:id="@+id/allCheckedBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" "></CheckBox>
</LinearLayout>
package com.sadhu.s01_e09_checkbox;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
//
private CheckBox eatBox;
private CheckBox sleepBox;
private CheckBox dotaBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
//
eatBox = (CheckBox)findViewById(R.id.eatId);
sleepBox = (CheckBox)findViewById(R.id.sleepId);
dotaBox = (CheckBox)findViewById(R.id.dotaId);
// , , , 。CompoundButton CheckBox
CheckBoxListener cbListener = new CheckBoxListener();
eatBox.setOnCheckedChangeListener(cbListener);
sleepBox.setOnCheckedChangeListener(cbListener);
dotaBox.setOnCheckedChangeListener(cbListener);
//
((CheckBox)findViewById(R.id.allCheckedBox)).setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton checkBox, boolean isChecked) {
eatBox.setChecked(isChecked);
sleepBox.setChecked(isChecked);
dotaBox.setChecked(isChecked);
}
});;
/*
//
OnBoxClickListener onBoxClick = new OnBoxClickListener();
//
eatBox.setOnClickListener(onBoxClick);
sleepBox.setOnClickListener(onBoxClick);
dotaBox.setOnClickListener(onBoxClick);*/
}
//
class CheckBoxListener implements OnCheckedChangeListener
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(buttonView.getId()==R.id.eatId){
System.out.println("eatBox");
}
else if(buttonView.getId()==R.id.sleepId){
System.out.println("sleepBox");
}
else if(buttonView.getId()==R.id.dotaId)
{
System.out.println("dotaBox");
}
if(isChecked)
{
System.out.println("checked");
}
else
{
System.out.println("unchecked");
}
}
}
//
/*
class OnBoxClickListener implements OnClickListener {
@Override
public void onClick(View view)
{
CheckBox box = (CheckBox)view;
if(box.getId()==R.id.eatId)
{
System.out.println("eatBox");
}
else if(box.getId()==R.id.sleepId)
{
System.out.println("sleepBox");
}
else if(box.getId()==R.id.dotaId)
{
System.out.println("dotaBox");
}
if(box.isChecked())
{
System.out.println("checked");
}
else
{
System.out.println("unchecked");
}
}
}*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
マルチ選択ボタンCheckBoxは簡単で、ユーザーにマルチ選択機能を提供することである.彼は2つのリスナーを持っている.1つは(OnClickListener)コントロールをクリックしてトリガーされたリスナーであり、1つはボタン選択状態が変化した後に実行されるリスナー(OnCheckedChangeListener)である.