티스토리 뷰
메일/우편수신 화면 변경
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="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
android:padding="30dp">
<RadioGroup
android:id="@+id/r"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal" >
<RadioButton
android:id="@+id/m"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="메일 수신"
android:textSize="20sp"
android:padding="10dp" />
<RadioButton
android:id="@+id/p"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="우편 수신"
android:textSize="20sp"
android:padding="10dp" />
</RadioGroup>
<EditText
android:id="@+id/mail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:hint="메일 주소"
android:textSize="20sp"
android:background="#23673AB7" />
<RadioGroup
android:id="@+id/post"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:background="#23673AB7" >
<RadioButton
android:id="@+id/r1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="자택 주소"
android:textSize="20sp"
android:padding="10dp" />
<RadioButton
android:id="@+id/r2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="직장 주소"
android:textSize="20sp"
android:padding="10dp" />
</RadioGroup>
<Button
android:id="@+id/b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="제출"
android:textSize="20sp" />
</LinearLayout>
MainActivity.java
package com.example.test;
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 androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RadioGroup r = findViewById(R.id.r);
RadioButton m = findViewById(R.id.m);
RadioButton p = findViewById(R.id.p);
EditText e = findViewById(R.id.mail);
e.setVisibility(View.GONE);
RadioGroup r0 = findViewById(R.id.post);
r0.setVisibility(View.GONE);
RadioButton r1 = findViewById(R.id.r1);
RadioButton r2 = findViewById(R.id.r2);
r.setOnCheckedChangeListener((a, b) -> {
if (m.isChecked()) {
e.setVisibility(View.VISIBLE);
r0.setVisibility(View.GONE);
}
else if (p.isChecked()) {
e.setVisibility(View.GONE);
r0.setVisibility(View.VISIBLE);
}
});
Button b = findViewById(R.id.b);
b.setOnClickListener(v -> {
String s;
if (m.isChecked()) {
String email = e.getText()+"";
s = m.getText() + "\n" + email;
}
else if (p.isChecked()) {
String postOption = r1.isChecked() ? r1.getText()+"" : r2.getText()+"";
s = p.getText() + "\n" + postOption;
}
else {
s = "선택되지 않음";
}
Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
});
}
}
공지사항
링크