package com.lesson1;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Toast;
public class CheckBoxDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final CheckBox check_box1 = (CheckBox) findViewById(R.id.cb1);
final CheckBox check_box2 = (CheckBox) findViewById(R.id.cb2);
check_box1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(check_box1.isChecked()){
System.out.println("Male is chosen");
Toast.makeText(CheckBoxDemoActivity.this, "Come guys! I am a mail",Toast.LENGTH_LONG).show();
}
else {
System.out.println("Female is chosen");
Toast.makeText(CheckBoxDemoActivity.this, "Alas, I am a woman",Toast.LENGTH_LONG).show();
}
}
});
}
}
and the layout.xml
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Toast;
public class CheckBoxDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final CheckBox check_box1 = (CheckBox) findViewById(R.id.cb1);
final CheckBox check_box2 = (CheckBox) findViewById(R.id.cb2);
check_box1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(check_box1.isChecked()){
System.out.println("Male is chosen");
Toast.makeText(CheckBoxDemoActivity.this, "Come guys! I am a mail",Toast.LENGTH_LONG).show();
}
else {
System.out.println("Female is chosen");
Toast.makeText(CheckBoxDemoActivity.this, "Alas, I am a woman",Toast.LENGTH_LONG).show();
}
}
});
}
}
and the layout.xml
<?
<
xml version="1.0" encoding="utf-8"?>LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/hello" /><CheckBox android:id="@+id/cb1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Male" /><CheckBox android:id="@+id/cb2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Female" /></LinearLayout>