Untitled

mail@pastecode.io avatar
unknown
java
a month ago
3.6 kB
49
Indexable
Never
package sumago.androidipt.day08b2checkboxradiobutton;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {


    RadioGroup radioGroup;
    RadioButton radioButtonMale;
    RadioButton radioButtonFemale;
    CheckBox checkBoxYes;
    CheckBox checkBoxNo;
    CheckBox checkBoxMaybe;
    Button btnSubmit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
       // radioGroup=findViewById(R.id.radioGroup);
        try {
            radioButtonMale=findViewById(R.id.radioButtonMale);
            radioButtonFemale=findViewById(R.id.radioButtonFemale);

            checkBoxYes=findViewById(R.id.checkBoxYes);
            checkBoxNo=findViewById(R.id.checkBoxNo);
            checkBoxMaybe=findViewById(R.id.checkBoxMaybe);
            btnSubmit=findViewById(R.id.btnSubmit);

            radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {

                    RadioButton checkedButton=findViewById(checkedId);
                    Toast.makeText(MainActivity.this, ""+checkedButton.getText()+" => "+checkedButton.isChecked(), Toast.LENGTH_SHORT).show();

                }
            });

            btnSubmit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {



                    if(radioGroup.getCheckedRadioButtonId()==R.id.radioButtonMale)
                    {
                        Log.d("mytag","radioButtonMale=>"+radioButtonMale.isChecked());

                    }else if(radioGroup.getCheckedRadioButtonId()==R.id.radioButtonFemale)
                    {
                        Log.d("mytag","radioButtonFeMale=>"+radioButtonFemale.isChecked());
                    }

                    Log.d("mytag","CheckBoxYes=>"+checkBoxYes.isChecked());
                    Log.d("mytag","CheckBoxNo=>"+checkBoxNo.isChecked());
                    Log.d("mytag","CheckBoxMaybe=>"+checkBoxMaybe.isChecked());
                }
            });

            checkBoxYes.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                    Toast.makeText(MainActivity.this, ""+checkBoxYes.getText()+" => "+isChecked, Toast.LENGTH_SHORT).show();

                }
            });
        } catch (Exception e) {
            Log.d("myatg",e.getMessage(),e);
            e.printStackTrace();
        }


    }
}
Leave a Comment