Android开发---常用控件事件的监听

78 阅读1分钟

​本文已参与「新人创作礼」活动,一起开启掘金创作之路。

本文以实现文本框输入内容监听为例

1.在之前案例的基础上继续实现其他功能

2.首先绘制一下主页面(activity_main.xml)

主要包含一个可编辑文本以及一个按钮:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
   <EditText
       android:id="@+id/et"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"></EditText>
        <Button
            android:id="@+id/btn_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登录"
        android:background="@drawable/button_background2"
        >
    </Button>
</LinearLayout>

3.主页面功能实现类MainActivity.java:


public class MainActivity extends AppCompatActivity {
    Button btn_login;
    Button btn_regist;
    EditText et;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_login=findViewById(R.id.btn_login);
        et=findViewById(R.id.et);
        et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                System.out.println("beforeTextChanged");
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                System.out.println("onTextChanged");
            }

            @Override
            public void afterTextChanged(Editable editable) {

                System.out.println("afterTextChanged");
            }
        });

//        btn_login.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                System.out.println("按钮点击事件");
//            }
//        });
    }
}

4.目前的效果图为:

 每一次输出内容控制台都会输出内容:

 5.那么如何可以在控制台观察到屏幕上输入的内容呢?

那么我们将实现类的代码进行修改如下:


public class MainActivity extends AppCompatActivity {
    Button btn_login;
    Button btn_regist;
    EditText et;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_login=findViewById(R.id.btn_login);
        et=findViewById(R.id.et);
        et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                System.out.println(charSequence);
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                System.out.println(charSequence);
            }

            @Override
            public void afterTextChanged(Editable editable) {

                System.out.println(editable.toString());
            }
        });
}

效果图如下:

 自此我们实现了文本框输入内容的监听啦!