Event Handling
문제
activity_main.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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"
android:gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#AEACAC"
app:backgroundTint="@null"
android:textColor="@color/black"
android:drawableLeft="@android:drawable/btn_star_big_on"
android:drawableRight="@android:drawable/btn_star_big_on"
android:text="회전하기" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageButton
android:id="@+id/imageButton"
android:layout_width="311dp"
android:layout_height="355dp"
app:srcCompat="@drawable/dog" />
</LinearLayout>
</LinearLayout>
MainActivity.java
package kr.ac.skuniv.a1005practice3;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
public class MainActivity extends AppCompatActivity {
Button btn;
ImageButton ibtn;
int angle=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button2);
ibtn = (ImageButton) findViewById(R.id.imageButton);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
angle+=10;
ibtn.setRotation(angle);
}
});
}
}