3.25

所花时间(包括上课):2.5

打码量(行):255

博客量(篇):1

了解到知识点:学习文本变化监听器

import android.os.Bundle;

import android.text.Editable;

import android.text.TextWatcher;

import android.widget.EditText;

import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

 

public class MainActivity extends AppCompatActivity {

 

    private EditText editText;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        editText = findViewById(R.id.edit_text);

        editText.addTextChangedListener(new TextWatcher() {

            @Override

            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                // 在文本变化之前执行的操作

            }

 

            @Override

            public void onTextChanged(CharSequence s, int start, int before, int count) {

                // 在文本变化过程中执行的操作

            }

 

            @Override

            public void afterTextChanged(Editable s) {

                // 在文本变化之后执行的操作

                String enteredText = s.toString();

                Toast.makeText(MainActivity.this, "Entered text: " + enteredText, Toast.LENGTH_SHORT).show();

            }

        });

    }

}

<!-- activity_main.xml -->

<EditText

    android:id="@+id/edit_text"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:hint="Enter text" />

posted @ 2024-03-25 20:48  赵千万  阅读(4)  评论(0编辑  收藏  举报