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.EditText;
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 RadioGroup radioGroup;
private Button btnReset, btnSubmit;
private RadioButton radioButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup = findViewById(R.id.my_radio_group);
btnReset = findViewById(R.id.btn_reset);
btnSubmit = findViewById(R.id.btn_submit);
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int checkedId = radioGroup.getCheckedRadioButtonId();
if(checkedId == -1) {
Toast.makeText(getApplicationContext(), "Please select", Toast.LENGTH_SHORT).show();
}else {
findRadioButton(checkedId);
}
}
});
btnReset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
radioGroup.clearCheck();
}
});
//체크값이 바뀔때 이벤트
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
radioButton = findViewById(checkedId);
Toast.makeText(getApplicationContext(), radioButton.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
}
private void findRadioButton(int checkedId) {
radioButton = findViewById(checkedId);
switch (checkedId) {
case R.id.radio_button_01:
Toast.makeText(getApplicationContext(), radioButton.getText().toString(), Toast.LENGTH_SHORT).show();
break;
case R.id.radio_button_02:
Toast.makeText(getApplicationContext(), radioButton.getText().toString(), Toast.LENGTH_SHORT).show();
break;
case R.id.radio_button_03:
Toast.makeText(getApplicationContext(), radioButton.getText().toString(), Toast.LENGTH_SHORT).show();
break;
case R.id.radio_button_04:
Toast.makeText(getApplicationContext(), radioButton.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"/>
<RadioGroup
android:id="@+id/my_radio_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<RadioButton
android:id="@+id/radio_button_01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="C++"
android:checked="true"/>
<RadioButton
android:id="@+id/radio_button_02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Java" />
<RadioButton
android:id="@+id/radio_button_03"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="php" />
<RadioButton
android:id="@+id/radio_button_04"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Python" />
</RadioGroup>
<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>
'System Programming > Android' 카테고리의 다른 글
[Android/Java] Video view (0) | 2021.06.13 |
---|---|
[Android/Java] 체크박스 (0) | 2021.06.13 |
[Android/Java] drawer menu (0) | 2021.06.12 |
[Android/Java] WebView (0) | 2021.06.12 |
[Android/Java] Sharedpreferences로 임시저장(알림같은 설정값 저장) (0) | 2021.06.12 |