[연습문제]
4-6
4-7
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">
<CheckBox
android:id="@+id/enableCheckBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enable 속성" />
<CheckBox
android:id="@+id/clickableCheckBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Clickable 속성" />
<CheckBox
android:id="@+id/rotationCheckBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="45도 회전" />
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
android:enabled="false"
android:layout_marginTop="130dp"/>
</LinearLayout>
MainActivity.java
더보기
package com.cookandroid.a4_6;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
public class MainActivity extends AppCompatActivity {
CheckBox check1, check2, check3;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("연습문제 4-7");
check1 = (CheckBox) findViewById(R.id.enableCheckBox);
check2 = (CheckBox) findViewById(R.id.clickableCheckBox);
check3 = (CheckBox) findViewById(R.id.rotationCheckBox);
btn = (Button) findViewById(R.id.btn);
check1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
btn.setEnabled(check1.isChecked());
}
});
check2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
btn.setClickable(check2.isChecked());
}
});
check3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(check3.isChecked()){
btn.setRotation(45);
}else{
btn.setRotation(0);
}
}
});
}
}
4-8
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">
<EditText
android:id="@+id/et1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text"
android:text="입력하세요" />
</LinearLayout>
MainActivity.java
더보기
package com.cookandroid.a4_8;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText et1;
String wrote;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("연습문제 4-8");
et1 =(EditText) findViewById(R.id.et1);
et1.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
wrote=et1.getText().toString();
Toast.makeText(getApplicationContext(), wrote, Toast.LENGTH_SHORT).show();
return false;
}
});
}
}
4-9
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"
tools:context=".MainActivity"
android:orientation="vertical">
<Button
android:id="@+id/btnRotate"
android:layout_width="146dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="100dp"
android:drawableLeft="@drawable/drawable_btn_img"
android:drawableRight="@drawable/drawable_btn_img"
android:text="회전하기"/>
<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="@drawable/cat2" />
</LinearLayout>
drawable_btn_imag.xml(이미지 크기조절용. res/drawable에 뒀음)
더보기
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/cat"
android:width="15dp"
android:height="15dp"
/>
</layer-list>
MainActivity.java
더보기
package com.cookandroid.a4_9;
import androidx.appcompat.app.AppCompatActivity;
import android.media.Image;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
Button rotate;
ImageView img;
int degree=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rotate= (Button) findViewById(R.id.btnRotate);
img = (ImageView) findViewById(R.id.img);
rotate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
degree += 10;
img.setRotation(degree);
}
});
}
}
'DEVELOPMENT' 카테고리의 다른 글
[Android Studio] Chapter 5 연습문제 (0) | 2021.06.28 |
---|---|
[Android Studio] Chapter 5 직접 풀어보기 (0) | 2021.06.28 |
[Android Studio] 안드로이드 사진 보기 어플 (0) | 2021.05.07 |
[Android Studio] 초간단 계산기 앱 만들기(직접 풀어보기) (0) | 2021.05.06 |
[Android Studio] 뷰, 위젯, 레이아웃 (0) | 2021.05.06 |