본문 바로가기
Android/그림으로 쉽게 설명하는 안드로이드 프로그래밍 개정6판

[Android/그림으로 쉽게 설명하는 안드로이드 프로그래밍 개정6판]그림으로 쉽게 설명하는 안드로이드 프로그래밍(개정6판) 연습문제 7장

by starfish22 2022. 10. 31.
728x90

정답이 아닐 수 있습니다. 오류가 있으면 알려주세요ㅎ

 

1.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/refresh"
        android:title="Refresh" />
    <item
        android:id="@+id/settings"
        android:title="Settings" />
</menu>

 

2.

@Override
public boolean onCreateOptionsMenu(Menu menu){
    getMenuInflater().inflate(R.menu.mymenu, menu);
    return true;
}

 

3.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="미사일 버튼" />

</LinearLayout>

custom_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#DDDDDD"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="1dp"
        android:layout_weight="0.5"
        android:background="#EEEEEE"
        android:gravity="center_vertical"
        android:paddingLeft="10dp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:text="Fire missles?"
        android:textColor="#000000"
        android:textSize="20dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/cancel"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_margin="1dp"
            android:layout_weight="0.5"
            android:background="#EEEEEE"
            android:gravity="center"
            android:paddingTop="13dp"
            android:paddingBottom="13dp"
            android:text="Cancel"
            android:textColor="#000000"
            android:textSize="17dp" />

        <TextView
            android:id="@+id/fire"
            android:layout_width="50dp"
            android:layout_height="match_parent"
            android:layout_margin="1dp"
            android:layout_weight="0.5"
            android:background="#EEEEEE"
            android:gravity="center"
            android:paddingTop="13dp"
            android:paddingBottom="13dp"
            android:text="Fire"
            android:textColor="#000000"
            android:textSize="17dp" />
    </LinearLayout>

</LinearLayout>

MainActivity.java

package kr.co.company.ch7;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements View.OnClickListener {
    View layout;
    TextView cancel, fire;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public void onClick(View view) {
        layout = getLayoutInflater().inflate(R.layout.custom_dialog, null);
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(layout);
        AlertDialog alertDialog = builder.create();
        alertDialog.show();

        cancel = (TextView) alertDialog.findViewById(R.id.cancel);
        fire = (TextView) alertDialog.findViewById(R.id.fire);

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "미사일 취소", Toast.LENGTH_LONG).show();
                alertDialog.dismiss();
            }
        });
        fire.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "미사일 발사", Toast.LENGTH_LONG).show();
                alertDialog.dismiss();
            }
        });
    }
}

결과

728x90

댓글