[Android] Change Images with button click (버튼클릭시 이미지 변경)
필그램
·2017. 7. 17. 04:26
When you click the button, a image will be changed with this code.
버튼클릭시 이미지가 바뀌는 프로그램
[Screen Shot]
[activity_main.xml]
이미지 3장으 drawable 폴더에 복사해 넣고, imageView 3개를 각각의 파일과 연결합니다.
(Copy 3 image files to the drawable folder.
Link imageView to the each file)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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"
tools:context="com.example.eric.ch12mylayout.MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:onClick="onButton1Clicked"
android:text="Change"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<FrameLayout
android:layout_width="296dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/button"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srcCompat="@drawable/landscape01"/>
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srcCompat="@drawable/landscape02"/>
<ImageView
android:id="@+id/imageView3"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srcCompat="@drawable/landscape03"/>
</FrameLayout>
</android.support.constraint.ConstraintLayout>
[mainActivity.java]
public class MainActivity extends AppCompatActivity {
ImageView imageView;
ImageView imageView2;
ImageView imageView3;
int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView);
imageView2 = (ImageView) findViewById(R.id.imageView2);
imageView3 = (ImageView) findViewById(R.id.imageView3);
}
public void onButton1Clicked (View v) {
int index = count % 3;
if (index == 0) {
imageView.setVisibility(View.VISIBLE);
imageView2.setVisibility(View.GONE);
imageView3.setVisibility(View.GONE);
}
if (index == 1) {
imageView.setVisibility(View.GONE);
imageView2.setVisibility(View.VISIBLE);
imageView3.setVisibility(View.GONE);
}
if (index == 2) {
imageView.setVisibility(View.GONE);
imageView2.setVisibility(View.GONE);
imageView3.setVisibility(View.VISIBLE);
}
count++;
}
}
'프로그래밍 > 모바일: iOS, Java, Android, Swift' 카테고리의 다른 글
안드로이드 레이아웃 : 테이블 레이아웃(TableLayout)과 stretchColumn (0) | 2017.07.25 |
---|---|
[JAVA] 추상클래스 (abstract) (0) | 2017.07.19 |
[JAVA] 스택과 큐(Stacks and Queues) 알아보기(1) (0) | 2017.07.14 |
안드로이드 프로그래밍 : 버튼만드는법 2가지 (0) | 2017.07.13 |
안드로이드 프로그램 기본 정리 (0) | 2017.07.11 |