Android Basic Learning Day10 | Event Processing

Words written in the front

1. Mainly refer to related videos from station B.
2. If there is something wrong with the content, I hope to point out or add it.
3. Consolidate content.

I. Overview

Event: It is the user's operation on the graphical interface. On Android phones and tablets, there are mainly 键盘事件和触摸事件two categories. Keyboard events include press down, pop up, etc., touch events include press down, pop up, slide, double click, etc.

1. Event processing based on the callback mechanism (generalization): methods starting with on.
2. Event processing based on the listener interface mechanism (summary): ends with Listener.

Two, handle keyboard events

(I. Overview

1. Click event

The event listener is View.OnClickListener, and the event processing method is onClick().

2. Key event

It is generated when the user presses or releases a key on the phone keyboard, the listener is View.OnKeyListener, and the event processing method is onKey().

3. Focus events

This event is generated when the component gets or loses focus. The listener is View.OnFocusChangeListener, and the event processing method is onFocusChange().

(2) Test-Comprehensive

① Layout
Insert picture description here
② MainActivity.java

package com.example.test;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener,
View.OnKeyListener,View.OnFocusChangeListener{
    
    

    private TextView t_title,t_text;
    //声明一个长度为2的数组
    ImageButton[] bt_cats = new ImageButton[2];

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

        //获取到控件
        t_title = findViewById(R.id.test_title);
        t_text = findViewById(R.id.test_text);
        bt_cats[0] = findViewById(R.id.test_cat1);//赋给数组中的第一个元素
        bt_cats[1] = findViewById(R.id.test_cat2);

        //设置标题
        t_title.setText("请通过键盘中的1、2或点击\n来选择你喜欢的猫咪");
        //取出bt_cats里面的每一个元素并为它添加监听器
        for(ImageButton bt_cat:bt_cats){
    
    
            //给bt_cat添加单击事件监听器
            bt_cat.setOnClickListener(this);
            //给bt_cat添加键盘按键监听器
            bt_cat.setOnKeyListener(this);
            //给bt_cat添加焦点监听器
            bt_cat.setOnFocusChangeListener(this);
        }
    }
    @Override
    //实现OnClickListener接口中的方法  单击事件
    public void onClick(View v){
    
    
        switch (v.getId()){
    
    
            case R.id.test_cat1:
                //设置提示文本
                t_text.setText("这是一只布偶猫");
                break;
            case R.id.test_cat2:
                t_text.setText("这是一只橘猫");
                break;
        }
    }

    @Override
    //实现OnKeyListener接口中的方法  按键事件
    public boolean onKey(View v, int keyCode, KeyEvent event) {
    
    
        //判断键盘码
        switch(keyCode){
    
    
            case KeyEvent.KEYCODE_1:
                //调用点击事件并请求到焦点事件
                bt_cats[0].performClick();
                bt_cats[0].requestFocus();
                break;
            case KeyEvent.KEYCODE_2:
                bt_cats[1].performClick();
                bt_cats[1].requestFocus();
                break;
        }
        return false;
    }

    @Override
    //实现OnFocusChangeListener接口中的方法  焦点事件
    public void onFocusChange(View v, boolean hasFocus) {
    
    
        if (hasFocus) {
    
    
            //吐司提示
            Toast toast = Toast.makeText(this, "获得焦点", Toast.LENGTH_SHORT);
            TextView tv = (TextView) toast.getView().findViewById(android.R.id.message);
            tv.setTextColor(Color.YELLOW); //设置吐司字体颜色
            toast.show();
            
            //得到焦点时文本变成红色
            t_text.setTextColor(Color.RED);
        }else{
    
    
            t_text.setTextColor(Color.BLACK);
        }
    }
}

③ Test effect
Insert picture description here

Three, handle touch events

When the mobile phone has a touch screen function, the event is generated by touching the screen, the listener is View.OnTouchListener, and the event processing method is onTouch().

(1) Test-move location

① Layout
Insert picture description here
② TestActivity.java

package com.example.test;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.appcompat.app.AppCompatActivity;

public class TestActivity extends AppCompatActivity {
    
    
    private ImageView t_img;
    private LinearLayout t_layout;
    private LinearLayout.LayoutParams LayoutParams;

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test2);

        //获取到控件
        t_img = (ImageView) findViewById(R.id.test_img);
        t_layout = (LinearLayout) findViewById(R.id.test_layout);
        // 获取焦点
        t_layout.setFocusable(true);
        t_layout.requestFocus();
        // 获取图像控件的布局参数
        LayoutParams = (LinearLayout.LayoutParams) t_img.getLayoutParams();

        // 触摸监听事件
        t_layout.setOnTouchListener(new View.OnTouchListener() {
    
    
            @Override
            public boolean onTouch(View v, MotionEvent event) {
    
    
                // 根据变化的触点坐标来更新图像控件的布局参数
                LayoutParams.leftMargin = (int) event.getX();
                LayoutParams.topMargin = (int) event.getY();
                // 重新设置图像控件的布局参数
                t_img.setLayoutParams(LayoutParams);

                return true;
            }
        });
    }
}

③ Test effect

Since it is a new active window to test, you need to change the manifest file first, as follows.
Insert picture description here
Insert picture description here

Four, supplement

1. Basic use of Toast

2. GestureOverlayView properties

Guess you like

Origin blog.csdn.net/luck_ch09/article/details/112782069