ITGenerations
안드로이드 - 버튼 클릭 + 결과 클릭 = 사진보여주기 본문
<Youtube>
< 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="com.example.ryan.myapplication.MainActivity">
<RadioGroup
android:id="@+id/radiogroup1"
android:layout_width="match_parent"
android:layout_height="66dp">
<RadioButton
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cat1" />
<RadioButton
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Dog1" />
<RadioButton
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Dog2" />
</RadioGroup>
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:text="Display Image"
android:textAlignment="center" />
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="203dp"
app:srcCompat="@drawable/cat" />
</LinearLayout>
<java>
package com.example.ryan.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;
public class MainActivity extends AppCompatActivity {
ImageView image;
RadioGroup group;
//
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image=(ImageView)findViewById(R.id.imageView);
group=(RadioGroup)findViewById(R.id.radiogroup1);
}
//
//
public void onRadioButtonClicked(View view) {
//
switch (group.getCheckedRadioButtonId()) {
case R.id.button1:
image.setImageResource(R.drawable.cat);
break;
case R.id.button2:
image.setImageResource(R.drawable.dog1);
break;
case R.id.button3:
image.setImageResource(R.drawable.dog2);
break;
}///
}
///
}
<picture>
'프로그래밍 > 안드로이드특강' 카테고리의 다른 글
버튼 yes, no (0) | 2018.01.23 |
---|---|
계산기 소스 (0) | 2018.01.22 |
안드로이드 - 토글 버튼 on, off (0) | 2018.01.19 |
안드로이드 소스 - 버튼 클릭, 이미지 (0) | 2018.01.19 |
라디오 버튼 만들기 안드로이드 (0) | 2018.01.19 |