监听按钮点击事件

3中注册监听方式比较android

1.匿名内部类app

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

2.实现接口this

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

3.设置onClick属性xml

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


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);
    }
}
相关文章
相关标签/搜索