Untitled

 avatar
unknown
java
9 months ago
3.1 kB
58
Indexable
package sumago.androidipt.day08b1;

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);
        setContentView(R.layout.activity_main);

        try {
            radioGroup=findViewById(R.id.radioGroup);
            radioButtonMale=findViewById(R.id.radioButtonMale);
            radioButtonFeMale=findViewById(R.id.radioButtonFeMale);
            checkBoxMaybe=findViewById(R.id.checkBoxMaybe);
            checkBoxNo=findViewById(R.id.checkBoxNo);
            checkBoxYes=findViewById(R.id.checkBoxYes);
            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) {


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

                }else if(checkedId==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, ""+isChecked, Toast.LENGTH_SHORT).show();
            }
        });



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

        }

    }
}
Editor is loading...
Leave a Comment