Procesamiento de eventos de Android basado en monitoreo

1. Interfaz de monitoreo de eventos comunes y método de procesamiento

Eventos Interfaz Método de tratamiento Explicación
Haga clic en evento OnClickListener al hacer clic() Se dispara cuando se hace clic en el componente
Haga clic en evento OnLongClickListener onLongClick () Se activa cuando el componente Changan
Eventos de teclado OnkeyListener onKey () Manejar eventos de teclado
Evento de enfoque OnFocusChangeListener onFocusChange Se dispara cuando el foco cambia
Evento táctil OnTouchListener en contacto Se activa cuando se toca el componente en la pantalla
Editar hora del evento OnEditorActionListener onEditorAction Se activa al editar

2.Ver método de registro de escucha de eventos comunes de clase

Método Explicación
setOnClickListener Registrarse haga clic en escucha de eventos
setOnLongClickListener Registrarse oyente de eventos de prensa larga
setOnkeyListener Registrar oyente de eventos de teclado
setOnFocusChangeListener Registrar oyente de eventos de enfoque
setOnTouchListener Registrarse oyente de eventos táctiles
setOnEditorActionListener Oyente de tiempo de actividad de edición registrado

Caso:
diseño

<?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>

Inserte la descripción de la imagen aquí
Mainactividad:

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. Clase externa:

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;
        }
        }
    }
}

15 artículos originales publicados · Me gusta0 · Visitas 142

Supongo que te gusta

Origin blog.csdn.net/qq_44230959/article/details/105439758
Recomendado
Clasificación