Android基于监听的事件处理

1.常见事件监听接口及处理方法

事件接口处理方法说明
单击事件OnClickListeneronClick()单击组件时触发
单击事件OnLongClickListeneronLongClick()长安组件时触发
键盘事件OnkeyListeneronKey()处理键盘事件
焦点事件OnFocusChangeListeneronFocusChange当焦点发生改变时触发
触摸事件OnTouchListeneronTouch当屏幕上触摸组件时触发
编辑活动时间OnEditorActionListeneronEditorAction当编辑时触发

2.View类的常见事件监听器注册方法

方法说明
setOnClickListener注册单击事件监听器
setOnLongClickListener注册长按事件监听器
setOnkeyListener注册键盘事件监听器
setOnFocusChangeListener注册焦点事件监听器
setOnTouchListener注册触摸事件监听器
setOnEditorActionListener注册编辑活动时间监听器

案例:
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>

在这里插入图片描述
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.外部类:

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值