Demo
MainActivity.java
package com.example.recyclerview;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import static com.example.recyclerview.Data.IMAGE_DRAWABLES;
import static com.example.recyclerview.Data.PROFILE_TEXT;
public class MainActivity extends AppCompatActivity {
private CheckBox checkBox1, checkBox2, checkBox3, checkBox4;
private Button btnReset, btnSubmit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkBox1 = findViewById(R.id.check_box_01);
checkBox2 = findViewById(R.id.check_box_02);
checkBox3 = findViewById(R.id.check_box_03);
checkBox4 = findViewById(R.id.check_box_04);
btnReset = findViewById(R.id.btn_reset);
btnSubmit = findViewById(R.id.btn_submit);
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringBuffer result = new StringBuffer();
result.append("My choice : ");
if(checkBox1.isChecked()) {
result.append("\n" + checkBox1.getText().toString());
}
if(checkBox2.isChecked()) {
result.append("\n" + checkBox2.getText().toString());
}
if(checkBox3.isChecked()) {
result.append("\n" + checkBox3.getText().toString());
}
if(checkBox4.isChecked()) {
result.append("\n" + checkBox4.getText().toString());
}
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
}
});
btnReset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(checkBox1.isChecked()) {
checkBox1.setChecked(false);
}
if(checkBox2.isChecked()) {
checkBox2.setChecked(false);
}
if(checkBox3.isChecked()) {
checkBox3.setChecked(false);
}
if(checkBox4.isChecked()) {
checkBox4.setChecked(false);
}
}
});
checkBox1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean checked = ((CheckBox) v).isChecked();
/*if(checked)
Toast.makeText(getApplicationContext(), "great!!!", Toast.LENGTH_SHORT).show();*/
}
});
}
public void myCheckBokClickListener(View view) {
boolean checked = ((CheckBox) view).isChecked();
switch (view.getId()) {
case R.id.check_box_01:
if(checked)
Toast.makeText(getApplicationContext(), ((CheckBox) view).getText().toString(), Toast.LENGTH_SHORT).show();
break;
case R.id.check_box_02:
if(checked)
Toast.makeText(getApplicationContext(), ((CheckBox) view).getText().toString(), Toast.LENGTH_SHORT).show();
break;
case R.id.check_box_03:
if(checked)
Toast.makeText(getApplicationContext(), ((CheckBox) view).getText().toString(), Toast.LENGTH_SHORT).show();
break;
case R.id.check_box_04:
if(checked)
Toast.makeText(getApplicationContext(), ((CheckBox) view).getText().toString(), Toast.LENGTH_SHORT).show();
break;
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center" >
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chosse"
android:textSize="32dp"
android:layout_gravity="center"/>
<CheckBox
android:id="@+id/check_box_01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="myCheckBokClickListener"
android:text="C++" />
<CheckBox
android:id="@+id/check_box_02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="myCheckBokClickListener"
android:text="Java" />
<CheckBox
android:id="@+id/check_box_03"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="myCheckBokClickListener"
android:text="php" />
<CheckBox
android:id="@+id/check_box_04"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="myCheckBokClickListener"
android:text="Python" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="@+id/btn_reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="reset"
android:layout_margin="10dp"/>
<Button
android:id="@+id/btn_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="submit"
android:layout_margin="10dp"/>
</LinearLayout>
</LinearLayout>
'Application Programming > Android' 카테고리의 다른 글
[Android/Java] Media player(local storage) (0) | 2021.06.13 |
---|---|
[Android/Java] Video view (0) | 2021.06.13 |
[Android/Java] radio button (0) | 2021.06.13 |
[Android/Java] drawer menu (0) | 2021.06.12 |
[Android/Java] WebView (0) | 2021.06.12 |