Ex 11
Ex 11
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<CheckBox
android:id="@+id/ch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="chk_ios" />
<CheckBox
android:id="@+id/ch2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="chk_android"
android:checked="true" />
<CheckBox
android:id="@+id/ch3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="chk_windows" />
<Button
android:id="@+id/ch4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="btn_display" />
</LinearLayout>
package com.example.helloworld;
import android.os.Bundle;
import android.app.Activity;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ch1=(CheckBox)findViewById(R.id.ch1);
ch2=(CheckBox)findViewById(R.id.ch2);
ch3=(CheckBox)findViewById(R.id.ch3);
b=(Button)findViewById(R.id.ch4);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringBuffer result = new StringBuffer();
if(ch1.isChecked()) {
result.append(ch1.getText());
}
if(ch2.isChecked()) {
result.append("\n").append(ch2.getText());
}
if(ch3.isChecked()) {
result.append("\n").append(ch3.getText());
}
Toast.makeText(MainActivity.this, result.toString(),
Toast.LENGTH_LONG).show();
}
});
}
}