Android studio实现按钮的点击事件

main.xml中的代码如下

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.LinearLayoutCompat
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="app.yu.com.test.Main">


    <Button
        android:id="@+id/bt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第一种" />
    <Button
        android:id="@+id/bt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第二种" />
    <Button
        android:id="@+id/bt3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第三种" />
    <Button
        android:id="@+id/bt31"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第三一种" />
    <Button
        android:id="@+id/bt32"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第三二种" />
    <Button
        android:id="@+id/bt33"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第三三种" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第四种"
        android:onClick="youyiyi"
        tools:layout_editor_absoluteY="0dp"
        tools:layout_editor_absoluteX="0dp" />


</android.support.v7.widget.LinearLayoutCompat>


Main.java的代码如下
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


public class Main extends AppCompatActivity implements View.OnClickListener {



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

        Button btn1 = (Button)findViewById(R.id.bt1);

        btn1.setOnClickListener(new MyListener());

        Button btn2 = (Button)findViewById(R.id.bt2);

        btn2.setOnClickListener(new MyListener());

        Button btn3 = (Button)findViewById(R.id.bt3);

//        btn3.setOnClickListener(new MyListener());

        btn3.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        Toast.makeText(getApplicationContext(),"实现点击第三个按钮",Toast.LENGTH_LONG).show();//显示时长分为
Toast.LENGTH_SHORT和Toast.LENGTH_LONG两种模式
} class MyListener implements View.OnClickListener{ @Override public void onClick(View v) { int id = v.getId(); switch (id) { case R.id.bt1: Toast toast = Toast.makeText(getApplicationContext(), "点击第一个按钮", Toast.LENGTH_SHORT); toast.show(); System.out.println("点击第一个按钮"); break; case R.id.bt2: System.out.println("点击第二个按钮"); break; case R.id.bt3: System.out.println("点击第三个按钮"); break; } } }}

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值