Languages/Android
[Android] ๊ธฐ๋ณธ ์์ ฏ ์ตํ๊ธฐ
- -
๋ฐ์ํ
์กฐ๋ง๊ฐ ๋ ์ด์์์ ๋ํ ๊ธ๋ ๋ฐ๋ก ํฌ์คํ ํ๋๋ก ํ๊ฒ ์ต๋๋ค.
๊ฐ์ธ ๊ณต๋ถ ํ ์ ๋ฆฌ ๋ชฉ์ ์ผ๋ก ์ฌ๋ฆฌ๋ ๊ฒ์ด๋ฉฐ, ์๋ชป๋ ๊ฒ์ด ์๋ค๋ฉด ๋๊ธ๋ก ๋จ๊ฒจ์ฃผ์ธ์.
(์ด๋ฒ ์์ค๋ ์ค๋ช ์ด ์๊ณ , ์ฝ๋๋ง ์ฌ๋ ค๋์์ต๋๋ค.)
์๋ฐ์์ค
package org.techtown.a2a_2019202202_8;
import androidx.appcompat.app.AppCompatActivity;
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 org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
CheckBox chkStart;
TextView tvQuery;
RadioGroup rgAnimal;
RadioButton rgDog, rgCat,rgRabbit;
Button btnOk;
ImageView ivAnimal;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chkStart = findViewById(R.id.chkStart);
tvQuery = findViewById(R.id.tvQuery);
rgAnimal = findViewById(R.id.rgAnimal);
rgDog = findViewById(R.id.rgDog);
rgCat = findViewById(R.id.rgCat);
rgRabbit = findViewById(R.id.rgRabbit);
btnOk = findViewById(R.id.btnOk);
ivAnimal = findViewById(R.id.ivAnimal);
btnOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
switch(rgAnimal.getCheckedRadioButtonId()){
case R.id.rgDog : ivAnimal.setImageResource(R.drawable.dog);
break;
case R.id.rgCat : ivAnimal.setImageResource(R.drawable.cat);
break;
case R.id.rgRabbit : ivAnimal.setImageResource(R.drawable.rabbit);
break;
}
}
});
/*๊ฐ ๋ฒํผ์ ๋๋ ์ ๋ ์ฌ์ง์ด ๋ฐ๋๋๋ก ํ๋ ์ด๋ฒคํธ ์ฒ๋ฆฌ ๊ธฐ๋ฅ
rgAnimal.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (i){
case R.id.rgDog : ivAnimal.setImageResource(R.drawable.dog);
break;
case R.id.rgCat : ivAnimal.setImageResource(R.drawable.cat);
break;
case R.id.rgRabbit : ivAnimal.setImageResource(R.drawable.rabbit);
break;
}
}
});
*/
//์์ ๋ฒํผ์ ํด๋ฆญํ๋ฉด ์๋ ๊ธ์ด ๋ณด์ด๊ณ , ์๋๋ฉด ์๋ณด์ด๊ฒ ํ๋ ๊ธฐ๋ฅ.(๋ฐฉ๋ฒ1)
/*chkStart.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(b==true){
tvQuery.setVisibility(View.VISIBLE);
rgAnimal.setVisibility(View.VISIBLE);
btnOk.setVisibility(View.VISIBLE);
ivAnimal.setVisibility(View.VISIBLE);
}else{
tvQuery.setVisibility(View.INVISIBLE);
rgAnimal.setVisibility(View.INVISIBLE);
btnOk.setVisibility(View.INVISIBLE);
ivAnimal.setVisibility(View.INVISIBLE);
}
}
});*/
//์์ ๋ฒํผ์ ํด๋ฆญํ๋ฉด ์๋ ๊ธ์ด ๋ณด์ด๊ณ , ์๋๋ฉด ์๋ณด์ด๊ฒ ํ๋ ๊ธฐ๋ฅ.(๋ฐฉ๋ฒ2)
chkStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(chkStart.isChecked()==true){
tvQuery.setVisibility(View.VISIBLE);
rgAnimal.setVisibility(View.VISIBLE);
btnOk.setVisibility(View.VISIBLE);
ivAnimal.setVisibility(View.VISIBLE);
}else{
tvQuery.setVisibility(View.INVISIBLE);
rgAnimal.setVisibility(View.INVISIBLE);
btnOk.setVisibility(View.INVISIBLE);
ivAnimal.setVisibility(View.INVISIBLE);
}
}
});
}
}
xml ์์ค
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="์ ํ์ ์์ํ๊ฒ ์ต๋๊น?"
android:textSize="24sp" />
<CheckBox
android:id="@+id/chkStart"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="์์ํจ"
android:textSize="24sp" />
<TextView
android:id="@+id/tvQuery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="์ข์ํ๋ ์ ์๋๋ฌผ์?"
android:textSize="24sp"
android:visibility="invisible" />
<RadioGroup
android:id="@+id/rgAnimal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="invisible">
<RadioButton
android:id="@+id/rgDog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
android:text="๊ฐ์์ง"
android:textSize="24sp" />
<RadioButton
android:id="@+id/rgCat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="๊ณ ์์ด"
android:textSize="24sp" />
<RadioButton
android:id="@+id/rgRabbit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ํ ๋ผ"
android:textSize="24sp" />
</RadioGroup>
<Button
android:id="@+id/btnOk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="์ ํ์๋ฃ"
android:textSize="24sp"
android:visibility="invisible" />
<ImageView
android:id="@+id/ivAnimal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
app:srcCompat="@drawable/dog" />
</LinearLayout>
๊ฒฐ๊ณผ
๊ฒฐ๊ณผ
์ด๋ฒคํธ ๋ฆฌ์ค๋๋ฑ์ ๊ด๋ จ๋์ ๋ค์ ๊ธ์ ํฌ์คํ ํ๊ฒ ์ต๋๋ค.
๋ฐ์ํ
'Languages > Android' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Android] imageView | imageButton (0) | 2021.03.04 |
---|---|
[Android] View | Widget (0) | 2021.03.04 |
์๋๋ก์ด๋ ์คํ๋์ค ์ค์นํ๊ธฐ (0) | 2020.09.04 |
์๋๋ก์ด๋๋? (0) | 2020.09.04 |
Contents
์์คํ ๊ณต๊ฐ ๊ฐ์ฌํฉ๋๋ค