mojo's Blog
안드로이드 프로그래밍 제 7장 연습문제 (4, 5, 6번) 본문
4번 ) 옵션 메뉴를 선택하면 동물의 사진이 바뀌는 프로젝트 만들기
activity_main.xml Code
<?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"
android:padding="10dp"
android:gravity="center_horizontal">
<ImageView
android:layout_width="300dp"
android:layout_height="300dp"
android:scaleType="fitCenter"
android:layout_margin="30dp"
android:id="@+id/img1"/>
</LinearLayout>
Java Code
public class MainActivity extends AppCompatActivity {
ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("연습하기");
img = (ImageView)findViewById(R.id.img1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0,1,0,"고양이");
menu.add(0,2,0,"라이언");
menu.add(0,3,0,"토끼");
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch(item.getItemId()){
case 1:
img.setImageResource(R.drawable.cat);
return true;
case 2:
img.setImageResource(R.drawable.lion);
return true;
case 3:
img.setImageResource(R.drawable.rabbit);
return true;
}
return false;
}
}
5번 ) 버튼을 클릭하면 임의의 위치에 이미지가 들어간 토스트가 나오는 프로젝트 만들기
activity_main.xml Code
<?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"
android:padding="10dp"
android:gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="토스트 보이기"
android:id="@+id/btn1"/>
</LinearLayout>
Java Code
public class MainActivity extends AppCompatActivity {
Button btn;
View toastView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("연습하기");
btn = (Button)findViewById(R.id.btn1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast toast = new Toast(MainActivity.this);
toastView = (View)View.inflate(MainActivity.this,R.layout.toast1, null);
toast.setView(toastView);
int offsetX = (int)(Math.random()*800) - 400;
int offsetY = (int)(Math.random()*800) - 400;
toast.setGravity(Gravity.NO_GRAVITY, offsetX, offsetY);
toast.show();
}
});
}
}
6번 ) 초기화면에 라디오버튼으로 네 가지 동물 이름을 선택할 수 있고, <그림보기> 를 클릭하면 그림이 대화상자로 나오는 프로젝트 만들기
activity_main.xml Code
<?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"
android:padding="10dp">
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:id="@+id/rGroup1">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="고양이"
android:id="@+id/rbtn1"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="라이언"
android:id="@+id/rbtn2"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="토끼"
android:id="@+id/rbtn3"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="강아지"
android:id="@+id/rbtn4"/>
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="그림보기"
android:layout_margin="10dp"
android:id="@+id/btn1"/>
</LinearLayout>
Dialog1.xml Code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:scaleType="fitCenter"
android:id="@+id/img1"/>
</LinearLayout>
Java Code
public class MainActivity extends AppCompatActivity {
RadioGroup rGroup;
RadioButton rbtn1, rbtn2, rbtn3, rbtn4;
Button btn, closeBtn;
View dialogView;
ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("연습하기");
rGroup = (RadioGroup)findViewById(R.id.rGroup1);
rbtn1 = (RadioButton)findViewById(R.id.rbtn1);
rbtn2 = (RadioButton)findViewById(R.id.rbtn2);
rbtn3 = (RadioButton)findViewById(R.id.rbtn3);
rbtn4 = (RadioButton)findViewById(R.id.rbtn4);
btn = (Button)findViewById(R.id.btn1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialogView = (View)View.inflate(MainActivity.this, R.layout.dialog1, null);
AlertDialog.Builder dlg = new AlertDialog.Builder(MainActivity.this);
String title = "";
int src = 0;
switch(rGroup.getCheckedRadioButtonId()){
case R.id.rbtn1:
title = "고양이";
src = R.drawable.cat;
break;
case R.id.rbtn2:
title = "라이언";
src = R.drawable.lion;
break;
case R.id.rbtn3:
title = "토끼";
src = R.drawable.rabbit;
break;
case R.id.rbtn4:
title = "강아지";
src = R.drawable.dog;
break;
}
dlg.setTitle(title);
dlg.setView(dialogView);
img = (ImageView)dialogView.findViewById(R.id.img1);
img.setImageResource(src);
dlg.setPositiveButton("닫기", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getApplicationContext(),"화면을 닫습니다.",Toast.LENGTH_SHORT).show();
}
});
dlg.show();
}
});
}
}
'Android' 카테고리의 다른 글
안드로이드 프로그래밍 제 8장 연습문제 6번 (0) | 2021.08.31 |
---|---|
파일 처리 및 응용 (0) | 2021.08.31 |
대화상자 (0) | 2021.08.30 |
메뉴 (0) | 2021.08.30 |
안드로이드 프로그래밍 제 6장 연습문제 6번 (0) | 2021.08.29 |
Comments