Use inner classes to implement event listeners

1. Steps to implement event listener using inner class

  • The first step (define member variables): define the components to be controlled in the inner class as member variables of the current Activity
  • The second step (get components): get the required components in the onCreate () method of the current Activity
  • The third step (define the inner class): define the inner class members in the current Activity, and write event handler code
  • The fourth step (creating a listener): create a listener in the onCreate () method of the current Activity
  • The fifth step (register the listener for the event source): register the listener for the event source in the onCreate () method of the current Activity
    2.
Layout下的activity_main_xml
<?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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/test"
        android:text="测试文字"
        android:layout_marginTop="20dp"
        android:layout_gravity="center_horizontal"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_margin="10dp"
        android:gravity="center_horizontal">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/red"
            android:text="红色" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/green"
            android:text="绿色"
            android:layout_marginLeft="10dp"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/blue"
            android:text="蓝色"
            android:layout_marginLeft="10dp"/>
    </LinearLayout>
</LinearLayout>

Test interface layout

Code under MainActivity.java

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {
    private TextView test;             //定义成员变量
    private Button red, green, blue;   //定义成员变量
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        test=(TextView) findViewById(R.id.test);  //获取组件
        red=(Button) findViewById(R.id.red);
        green=(Button) findViewById(R.id.green);
        blue=(Button) findViewById(R.id.blue);
        InterLinsten interLinsten=new InterLinsten();//创建监听器
        red.setOnClickListener(interLinsten);        //注册监听器
        blue.setOnClickListener(interLinsten);
        green.setOnClickListener(interLinsten);
    }
    //定义内部类InterLinsten实现(implement)事件响应
    private class InterLinsten implements View.OnClickListener{ 

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.red:
                    test.setTextColor(Color.RED);
                    break;
                case R.id.blue:
                    test.setTextColor(Color.BLUE);
                    break;
                case R.id.green:
                    test.setTextColor(Color.GREEN);
                default:break;
            }

        }
    }


}
Published 15 original articles · Likes0 · Visits 146

Guess you like

Origin blog.csdn.net/qq_44230959/article/details/105392823
Recommended