본문 바로가기

동방프로젝트

안드3일차 -1

package com.example.helloandroid;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
TextView text1, text2;
CheckBox chkAgree;
RadioGroup group1;
RadioButton btn1, btn2, btn3;
Button btnOk;
ImageView imacon;

@Override
protected void onCreate(Bundle savedInstanceState) {
//가장 먼저 시작하는 메소드 onCreate
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//setContentView: activity_main.xml을 화면에 표시하라는 의미
setTitle("이모티콘 보자"); //이름의 set

text1=(TextView) findViewById(R.id.Text1);
chkAgree=(CheckBox) findViewById(R.id.Chkagree1);

text2=(TextView) findViewById(R.id.Text2);
group1=(RadioGroup)findViewById(R.id.Rdg1);
btn1=(RadioButton)findViewById(R.id.Rbu1);
btn2=(RadioButton)findViewById(R.id.Rbu2);
btn3=(RadioButton)findViewById(R.id.Rbu3);

btnOk=(Button)findViewById(R.id.butOk);
imacon=(ImageView)findViewById(R.id.ImgCon);

chkAgree.setOnCheckedChangeListener
(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

if(chkAgree.isChecked()==true)
{
text2.setVisibility(android.view.View.VISIBLE);
group1.setVisibility(View.VISIBLE);
btnOk.setVisibility(View.VISIBLE);
imacon.setVisibility(View.VISIBLE);
}
else
{
text2.setVisibility(android.view.View.INVISIBLE);
group1.setVisibility(View.INVISIBLE);
btnOk.setVisibility(View.INVISIBLE);
imacon.setVisibility(View.INVISIBLE);
}
}
});

btnOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
switch(group1.getCheckedRadioButtonId())
{
case R.id.Rbu1:
imacon.setImageResource(R.drawable.chinhan);
break;
case R.id.Rbu2:
imacon.setImageResource(R.drawable.chiwawa);
break;
case R.id.Rbu3:
imacon.setImageResource(R.drawable.mamang);
break;
default:
Toast.makeText(getApplicationContext(),
"하나를 선택하세요", Toast.LENGTH_SHORT).show();
}
}
});
}
}

/////////////////////////////////////

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/Text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="선택을 시작하겠습니까?"/>

<CheckBox
android:id="@+id/Chkagree1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="시작함"/>

<TextView
android:id="@+id/Text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="좋아하는 동물은?"
android:visibility="invisible"/>

<RadioGroup
android:id="@+id/Rdg1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="invisible">

<RadioButton
android:id="@+id/Rbu1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="chinhan"/>

<RadioButton
android:id="@+id/Rbu2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/chiwawa"/>

<RadioButton
android:id="@+id/Rbu3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/mamang"/>

</RadioGroup>

<Button
android:id="@+id/butOk"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="선택완료"
android:visibility="invisible"/>

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ImgCon"
android:visibility="invisible"/>

</LinearLayout>

'동방프로젝트' 카테고리의 다른 글

안드3일차 3  (0) 2020.08.11
안드  (0) 2020.08.11
안드2일차  (0) 2020.08.10
안드1일차  (2) 2020.08.08
남방요척몽) 특별한 일 없으면 아직 바꿀 일 없을 player.cpp  (2) 2020.08.01