mojo's Blog
안드로이드 프로그래밍 제 4장 연습문제 (7, 8, 9번) 본문
7번 체크박스를 선택할 때마다 버튼의 속성이 설정되도록 프로젝트 작성하기
XML Code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="30dp"
android:orientation = "vertical">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Enabled 속성"
android:id="@+id/chk1"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Clickable 속성"
android:id="@+id/chk2"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="45도 회전"
android:id="@+id/chk3"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:text="Button"
android:enabled="false"
android:clickable="false"
android:rotation="0"
android:id="@+id/btn1"/>
</LinearLayout>
Java Code
package com.example.helloandroid;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
CheckBox chk1,chk2,chk3;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chk1 = (CheckBox)findViewById(R.id.chk1);
chk2 = (CheckBox)findViewById(R.id.chk2);
chk3 = (CheckBox)findViewById(R.id.chk3);
btn = (Button)findViewById(R.id.btn1);
chk1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(chk1.isChecked() == true){
btn.setEnabled(true);
}
else{
btn.setEnabled(false);
}
}
});
chk2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(chk2.isChecked() == true){
btn.setClickable(true);
}
else{
btn.setClickable(false);
}
}
});
chk3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(chk3.isChecked() == true){
btn.setRotation(45);
}
else{
btn.setRotation(0);
}
}
});
}
}
8번 EditText 키가 눌릴 때마다 바뀐 글자가 토스트 메시지로 나오도록 프로젝트 작성하기
XML Code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="30dp"
android:orientation = "vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="입력하세요."
android:id="@+id/edit1"/>
</LinearLayout>
Java Code
package com.example.helloandroid;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
EditText edit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit = (EditText)findViewById(R.id.edit1);
edit.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
String s = edit.getText().toString();
Toast.makeText(getApplicationContext(),s,Toast.LENGTH_SHORT).show();
return true;
}
});
}
}
9번 버튼을 클릭하면 이미지가 10도씩 회전하도록 프로젝트를 작성하기
XML Code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="30dp"
android:orientation = "vertical">
<ImageButton
android:layout_width="300dp"
android:layout_height="100dp"
android:src="@drawable/rabbit"
android:id="@+id/imgBtn1"
android:layout_margin="30dp"
android:scaleType="fitCenter"/>
<ImageView
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_margin="30dp"
android:rotation="0"
android:src="@drawable/oreo"
android:id="@+id/img1"
android:scaleType="fitCenter"/>
</LinearLayout>
Java Code
package com.example.helloandroid;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
ImageButton imgBtn;
ImageView imgView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgBtn = (ImageButton)findViewById(R.id.imgBtn1);
imgView = (ImageView)findViewById(R.id.img1);
imgBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int rotate = (int)imgView.getRotation();
imgView.setRotation((rotate+10) % 360);
}
});
}
}
'Android' 카테고리의 다른 글
Java 코드로 화면 만들기 (0) | 2021.08.28 |
---|---|
Layout 종류 및 LinearLayout (0) | 2021.08.28 |
기본 위젯 활용하기 (0) | 2021.08.26 |
기본 위젯 다뤄보기 (0) | 2021.08.26 |
View 그리고 View 클래스의 XML 속성 (0) | 2021.08.26 |
Comments