监听按钮点击事件

3中注册监听方式比较

1.匿名内部类

需要获取控件对象,使用变量不方便;适用于单个事件

2.实现接口

需要获取控件对象,使用变量方便;适用于于多个事件

3.设置onClick属性

无需获得控件对象,使用变量方便;不便维护。


1匿名内部类 代码实现

Button btn_show;
btn_show = (Button) findViewById(R.id.btn_show); //强转对象
btn_show.setOnClickListener(this);


//注册监听器
btn_show.setOnClickListener(new View.OnClickListener() { //匿名内部类

    @Override
    public void onClick(View view) {
        //响应
        String text = "你点击了按钮";
        Toast.makeText(MainActivity.this,text,Toast.LENGTH_SHORT).show();
        Log.i("sp1",text);
    }
});





2 实现接口 代码实现
public class MainActivity extends AppCompatActivity implements View.OnClickListener 
Button btn_show;
btn_show = (Button) findViewById(R.id.btn_show); //强转对象
btn_show.setOnClickListener(this);

@Override
public void onClick(View view) { //实现接口
    //响应
           String text = "你点击了按钮!!!";
           Toast.makeText(this,text,Toast.LENGTH_SHORT).show();
           Log.i("sp1",text);
}


3.设置onClick属性 在xml main.xml文件中的Butten中添加一行代码
android:onClick="btnClick"
public void btnClick(View v){ //自定义方法
    String text = "你点击了按钮!!?";
    Toast.makeText(this,text,Toast.LENGTH_SHORT).show();
    Log.i("sp1",text);
}
推荐使用的话是前两种。

内部定义
运行截图

<Button
    android:id="@+id/loginbutten"
    android:layout_marginTop="90dp"
    android:text="点我"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"/>
    <EditText
        android:id="@+id/username"
        android:hint="用户名"
        android:inputType="textPassword"
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:textColorHint="#0f0"
        android:layout_marginLeft="30dp"
        />
    <EditText
        android:id="@+id/password"
        android:hint="密码"
        android:inputType="textPassword"
        android:textColorHint="#0f0"
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:layout_marginLeft="30dp"
        />

public class MainActivity extends AppCompatActivity {

    private Button loginbutten;
    private TextView username;
    private TextView password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        loginbutten = (Button) findViewById(R.id.loginbutten);
        username = (TextView) findViewById(R.id.username);
        password = (TextView) findViewById(R.id.password);
        loginbutten.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View view) {
                String msg = "用户名"+username.getText()+"密码是"+password.getText();
                Toast.makeText(MainActivity.this,msg,Toast.LENGTH_SHORT).show();
            }
        });

    }
内部定义2
package com.example.deligence.myapp1;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private Button loginbutten;
    private TextView username;
    private TextView password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        loginbutten = (Button) findViewById(R.id.loginbutten);
        username = (TextView) findViewById(R.id.username);
        password = (TextView) findViewById(R.id.password);

        Button.OnClickListener listener = new Button.OnClickListener(){
            @Override
            public void onClick(View view) {
                String msg = "用户名"+username.getText()+"密码是"+password.getText();
                Toast.makeText(MainActivity.this,msg,Toast.LENGTH_SHORT).show();
            }
        };
        loginbutten.setOnClickListener(listener);
    }
}










posted @ 2017-02-23 21:15  Philtell  阅读(89)  评论(0编辑  收藏  举报