示例(1)按键和文本框监听

 参考教程:

3.1.1 基于监听的事件处理机制

http://www.runoob.com/w3cnote/android-tutorial-listen-event-handle.html

3.5 监听EditText的内容变化

http://www.runoob.com/w3cnote/android-tutorial-listener-edittext-change.html

http://blog.csdn.net/u010924834/article/details/48753835

过程记录

 

1布局管理器

1.1添加一个布局管理器

1.2添加两个按键

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context="com.example.dongdong.myapplication.MainActivity">
//1.1 布局管理器
    <LinearLayout
        android:layout_width="330dp"
        android:layout_height="61dp"
        android:layout_margin="5dp"
        android:orientation="horizontal"
        tools:context=".MainActivity"
        tools:layout_editor_absoluteX="16dp"
        tools:layout_editor_absoluteY="16dp">
//1.2 添加文本框
        <EditText
            android:id="@+id/edit_pawd"
            android:layout_width="0dp"
            android:layout_height="48dp"
            android:layout_weight="2"
            android:inputType="textPassword" />
//1.3 添加按钮 按钮改变 密码显示和隐藏
        <Button
            android:id="@+id/btnChange"
            android:layout_width="3dp"
            android:layout_height="48dp"
            android:layout_weight="1"
            android:text="密码可见" />
// 1.4 添加按钮 按钮单击 输出一句话

        <Button
            android:id="@+id/btnshow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button"
            tools:layout_editor_absoluteX="16dp"
            tools:layout_editor_absoluteY="94dp" />

    </LinearLayout>



</android.support.constraint.ConstraintLayout>

  2 主函数

2.0 导入包

2.1 声明变量

  1.    private EditText edit_pawd; // 文本框
  2.     private Button btnChange;   // 按键1
  3.     private boolean flag = false;  //判断
  4.     private Button btnshow;         //按键2

2.2 绑定变量与实际按键

  1. edit_pawd = (EditText) findViewById(R.id.edit_pawd);
  2.  btnChange = (Button) findViewById(R.id.btnChange);
  3. btnshow = (Button) findViewById(R.id.btnshow);

2.3 绑定按键的方法

第一种模式   1)直接用匿名内部类

  • 平时最常用的一种:直接setXxxListener后,重写里面的方法即可; 通常是临时使用一次,复用性不高!

btnChange.setOnClickListener(new View.OnClickListener() {  public void onClick(View view) {  //按键事件触发      }    });

第二种模式  2)使用内部类

  • 和上面的匿名内部类不同哦! 使用优点:可以在该类中进行复用,可直接访问外部类的所有界面组件!

先写方法

class BtnClickListener implements View.OnClickListener { public void onClick(View v) {  // 按键单击动作 }  }
 然后new出来

btnshow.setOnClickListener(new BtnClickListener());

注意:方法要在调用前出现
   

package com.example.dongdong.myapplication;


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.app.Activity;

public class MainActivity extends AppCompatActivity {

    private EditText edit_pawd; // 文本框
    private Button btnChange;   // 按键
    private boolean flag = false;  //判断

    private Button btnshow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        edit_pawd = (EditText) findViewById(R.id.edit_pawd);
        edit_pawd.setHorizontallyScrolling(true);    //设置EditText不换行

        btnChange = (Button) findViewById(R.id.btnChange);

        btnChange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(flag == true){
                    //设置文本框隐藏模式
                    edit_pawd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    flag = false;
                    btnChange.setText("密码隐藏");
                }else{
                    //设置文本框可见模式
                    edit_pawd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    flag = true;
                    btnChange.setText("密码可见");
                }
            }
        });


        //定义一个内部类,实现View.OnClickListener接口,并重写onClick()方法
        class BtnClickListener implements View.OnClickListener
        {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "按钮被点击了", Toast.LENGTH_SHORT).show();
            }
        }

        btnshow = (Button) findViewById(R.id.btnshow);
        //直接new一个内部类对象作为参数
        btnshow.setOnClickListener(new BtnClickListener());


    }

}

  

原文地址:https://www.cnblogs.com/kekeoutlook/p/8379993.html