コンピュータクワガタ

かっぱのかっぱによるコンピュータ関連のサイトです

Androidアプリ入門 No.16 CheckBoxのActivityからの操作

今風に書き直した記事がありますので、合わせて参照ください。
blog.webarata3.link

CheckBox

Activityからの操作

ボタンを組み合わせて書式を変更する例を以下に示す。まずは、main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff"
    >
    <CheckBox
        android:id="@+id/wideCheckBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="横長にする"
        android:textColor="#000"
        android:textSize="16sp"
        />
    <CheckBox
        android:id="@+id/redCheckBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="赤字にする"
        android:textColor="#000"
        android:textSize="16sp"
        />
    <Button
        android:id="@+id/changeButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="変更"
        android:textColor="#000"
        android:textSize="16sp"
        />
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ここの文字が変更されます。"
        android:textColor="#000"
        android:textSize="16sp"
        />
</LinearLayout>

次に、MainActivity.java

package sample.at;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

public class MainActivity extends Activity {
    private CheckBox wideCheckBox;
    private CheckBox redCheckBox;
    private Button changeButton;
    private TextView textView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        wideCheckBox = (CheckBox) findViewById(R.id.wideCheckBox);
        redCheckBox = (CheckBox) findViewById(R.id.redCheckBox);
        textView = (TextView) findViewById(R.id.textView);

        changeButton = (Button) findViewById(R.id.changeButton);
        changeButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                if (wideCheckBox.isChecked()) {
                    textView.setTextScaleX(2.0f);
                } else {
                    textView.setTextScaleX(1.0f);
                }
                if (redCheckBox.isChecked()) {
                    textView.setTextColor(Color.RED);
                } else {
                    textView.setTextColor(Color.BLACK);
                }
            }
        });
    }
}

実際の動作画面は以下。変更ボタンを押したときに、「横長にする」、「赤字にする」CheckBoxの状態を調べて、チェックされていれば文字通りの状態に変更する。

次に、ボタンがなくチェックボックスの状態を変更した場合にすぐにTextViewの状態を変更する。まずは、main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff"
    >
    <CheckBox
        android:id="@+id/wideCheckBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="横長にする"
        android:textColor="#000"
        android:textSize="16sp"
        />
    <CheckBox
        android:id="@+id/redCheckBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="赤字にする"
        android:textColor="#000"
        android:textSize="16sp"
        />
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ここの文字が変更されます。"
        android:textColor="#000"
        android:textSize="16sp"
        />
</LinearLayout>

次に、MainActivity.java

package sample.at;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class MainActivity extends Activity {
    private CheckBox wideCheckBox;
    private CheckBox redCheckBox;
    private TextView textView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        wideCheckBox = (CheckBox) findViewById(R.id.wideCheckBox);
        redCheckBox = (CheckBox) findViewById(R.id.redCheckBox);
        textView = (TextView) findViewById(R.id.textView);

        wideCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    textView.setTextScaleX(2.0f);
                } else {
                    textView.setTextScaleX(1.0f);
                }
            }
        });
        redCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    textView.setTextColor(Color.RED);
                } else {
                    textView.setTextColor(Color.BLACK);
                }
            }
        });
    }
}

実行結果は以下。OnCheckedChangeListenerでCheckBoxのCheckの状態が変わったイベントを受け取ることができる。チェックボックスを押すだけで文字列の状態が変わることが分かる。