Screenshots :
activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="com.chantisandroid.checkboxexample.MainActivity">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Java"
android:id="@+id/checkBox"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="50dp"
android:layout_marginLeft="50dp" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android"
android:id="@+id/checkBox2"
android:layout_below="@+id/checkBox"
android:layout_alignLeft="@+id/checkBox"
android:layout_alignStart="@+id/checkBox"
android:layout_marginTop="10dp" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Php"
android:id="@+id/checkBox3"
android:layout_below="@+id/checkBox2"
android:layout_alignLeft="@+id/checkBox2"
android:layout_alignStart="@+id/checkBox2"
android:layout_marginTop="10dp" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Oracle"
android:id="@+id/checkBox4"
android:layout_below="@+id/checkBox3"
android:layout_alignLeft="@+id/checkBox3"
android:layout_alignStart="@+id/checkBox3"
android:layout_marginTop="10dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select"
android:id="@+id/button"
android:layout_below="@+id/checkBox4"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp" />
</RelativeLayout>
MainActivity.java :
package com.chantisandroid.checkboxexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
CheckBox checkBox,checkBox2,checkBox3,checkBox4;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkBox = (CheckBox)findViewById(R.id.checkBox);
checkBox2 = (CheckBox)findViewById(R.id.checkBox2);
checkBox3 = (CheckBox)findViewById(R.id.checkBox3);
checkBox4 = (CheckBox)findViewById(R.id.checkBox4);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(this);
}
@Override
public void onClick(View view) {
StringBuffer results = new StringBuffer();
results.append("Java : ").append(checkBox.isChecked());
results.append("\nAndroid : ").append(checkBox2.isChecked());
results.append("\nPhp :").append(checkBox3.isChecked());
results.append("\nOracle :").append(checkBox4.isChecked());
Toast.makeText(this,results.toString(),Toast.LENGTH_LONG).show();
}
}
No comments:
Post a Comment