1. ホーム
  2. Android development

Android 開発 キーボード イベント モバイル ミッキーマウス ケース

2022-02-17 21:37:25

MainAcitity.javaファイル



package com.example;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.appcompat.app;
public class MainActivity extends AppCompatActivity {
    protected static final int STEP = 10;
    private ImageView ivMickey;
    private LinearLayout root;
    private LinearLayout.LayoutParams layoutParams;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set up the user interface using the layout resource file
        setContentView(R.layout.activity_main);
        //get the control instance by resource index
        ivMickey = findViewById(R.id.ivMickey);
        root = findViewById(R.id.root);
        // set the root layout to be focusable
        root.setFocusable(true);
        //get the root layout to get the focus
        root.requestFocus();
        //get the layout parameters of the image control
        layoutParams = (LinearLayout.LayoutParams) ivMickey.getLayoutParams();
        //register the listener to the root layout
        root.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                switch (keyCode) {
                    //modify the layout parameters of the image control according to the key
                    KEYCODE_DPAD_UP:// Press the upper arrow key
                        //Upper border collision detection
                        if (ivMickey.getTop() >= 10) {
                            layoutParams.topMargin = layoutParams.topMargin - STEP;
                        } else {
                            Toast.makeText(MainActivity.this, "Hit the top border", Toast.LENGTH_SHORT).show();
                        }
                        break;
                    case KeyEvent.KEYCODE_DPAD_DOWN:// Press the arrow key
                        if (ivMickey.getBottom() <=root.getHeight()) {
                        layoutParams.topMargin = layoutParams.topMargin + STEP;
                        } else {
                            Toast.makeText(MainActivity.this,"Hit the lower border",Toast.LENGTH_SHORT).show();
                        }
                        break;
                    KEYCODE_DPAD_LEFT:// Press the left arrow key
                        if (ivMickey.getLeft() >= 9) {
                            layoutParams.leftMargin = layoutParams.leftMargin - STEP;
                        }else {
                            Toast.makeText(MainActivity.this,"Hit the left border",Toast.LENGTH_SHORT).show();
                        }
                        break;
                    case KeyEvent.KEYCODE_DPAD_RIGHT:// Press the right arrow key
                        if(ivMickey.getRight() <= root.getWidth()) {
                            layoutParams.leftMargin = layoutParams.leftMargin + STEP;
                        } else {
                            Toast.makeText(MainActivity.this,"Hit the right margin",Toast.LENGTH_SHORT).show();
                        }
                        break;
                }
                //reset the layout parameters of the image control
                ivMickey.setLayoutParams(layoutParams);
                return false;
            }
        });


    }
}

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:id="@+id/root"
    android:background="@drawable/background"
    android:orientation="vertical"
    tools:context=".MainActivity">
<ImageView
    android:id="@+id/ivMickey"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:gravity="center"
    android:src="@drawable/mickey"/>

</LinearLayout>