Android event processing based on monitoring

1. Common event monitoring interface and processing method

event interface Approach Explanation
Click event OnClickListener onClick() Fired when the component is clicked
Click event OnLongClickListener onLongClick() Triggered when Changan component
Keyboard events OnkeyListener onKey() Handle keyboard events
Focus event OnFocusChangeListener onFocusChange Fires when the focus changes
Touch event OnTouchListener onTouch Fires when the component is touched on the screen
Edit event time OnEditorActionListener onEditorAction Triggered when editing

2.View class common event listener registration method

method Explanation
setOnClickListener Register click event listener
setOnLongClickListener Register long press event listener
setOnkeyListener Register keyboard event listener
setOnFocusChangeListener Register focus event listener
setOnTouchListener Register touch event listener
setOnEditorActionListener Registered Editing Activity Time Listener

Case:
Layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="测试文字"
        android:layout_margin="20dp"
        android:layout_gravity="center_horizontal"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="颜色:"/>
        <Button
            android:id="@+id/red"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="红色"/>
        <Button
            android:id="@+id/green"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="绿色"/>
        <Button
            android:id="@+id/blue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="蓝色"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_margin="20dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="字体"/>
        <Button
            android:id="@+id/bold"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="粗体"/>
        <Button
            android:id="@+id/italic"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="斜体" />
        <Button
            android:id="@+id/normal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="默认"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_margin="20dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="字体大小:"/>
        <Button
            android:id="@+id/bigger"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="增大"/>
        <Button
            android:id="@+id/smaller"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="减小"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_margin="20dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="内容:"/>
        <EditText
            android:id="@+id/edit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>
</LinearLayout>

Insert picture description here
Mainactivity:

package com.example.a4_com;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView text;
    private Button red,green,blue,bigger,smaller,bold,italic,naomal;
    private EditText edit;
    private int flot=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //颜色事件获取组件
     text=(TextView) findViewById(R.id.text);
     red=(Button) findViewById(R.id.red);
     green=(Button)findViewById(R.id.green);
     blue=(Button) findViewById(R.id.blue);
     //实现事件监听器
     InterColour interColour=new InterColour();
     //注册事件监听器
     red.setOnClickListener(interColour);
     green.setOnClickListener(interColour);
     blue.setOnClickListener(interColour);
      //文字大小事件
     bigger=(Button) findViewById(R.id.bigger);
     smaller=(Button) findViewById(R.id.smaller);
     OuterTypeface outerTypeface=new OuterTypeface();
     bigger.setOnClickListener(outerTypeface);
     smaller.setOnClickListener(outerTypeface);
     //字体样式事件
     bold=(Button) findViewById(R.id.bold);
     italic=(Button) findViewById(R.id.italic);
     naomal=(Button) findViewById(R.id.normal);
      bold.setOnClickListener(this);
      italic.setOnClickListener(this);
      naomal.setOnClickListener(this);
      //编辑框文件
     edit=(EditText) findViewById(R.id.edit);
     //4.匿名内部类
     edit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
         @Override
         public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
             text.setText(edit.getText().toString().trim());
             return true;
         }
     });
    }
    //1.定义内部类
    public class InterColour implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.red:
                    text.setTextColor(Color.RED);
                    break;
                case R.id.green:
                    text.setTextColor(Color.GREEN);
                    break;
                case R.id.blue:
                    text.setTextColor(Color.BLUE);
                    break;
                default:
                    break;
            }
        }
    }
    //3.使用类自身继承
  //flot=0 默认 flot=1加粗 flot=2 倾斜  flot=3加粗又倾斜
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bold:
                if(flot==2||flot==3){
                   text.setTypeface(Typeface.MONOSPACE,Typeface.BOLD_ITALIC);
                   flot=3;
                } else{
                    text.setTypeface(Typeface.MONOSPACE,Typeface.BOLD);
                    flot=1;
                }break;
            case R.id.italic:
                if (flot==1||flot==3){
                    text.setTypeface(Typeface.MONOSPACE,Typeface.BOLD_ITALIC);
                     flot=3;
                }else {
                    text.setTypeface(Typeface.MONOSPACE,Typeface.ITALIC);
                    flot=2;
                }break;
            case R.id.normal:
                text.setTypeface(Typeface.MONOSPACE,Typeface.NORMAL);
                flot=0;
                break;
            default:
                break;
        }


    }

}

2. External class:

package com.example.a4_com;

import android.view.View;
import android.widget.TextView;

public class OuterTypeface implements View.OnClickListener {
    private TextView text;
    private  float size=10;
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bigger:
                size+=4;
                break;
            case R.id.smaller:
                size-=4;
                break;
            default:
                break;
        }
        if (size>=72){
            size=72;
        if (size<=4) {
            size = 4;
        }
        }
    }
}

Published 15 original articles · Likes0 · Visits 142

Guess you like

Origin blog.csdn.net/qq_44230959/article/details/105439758