频道栏目
首页 > 资讯 > Android > 正文

Android studio实现按钮的点击事件

17-06-19        来源:[db:作者]  
收藏   我要投稿

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; } } }}
相关TAG标签
上一篇:Android apk 瘦身 去掉不需要的so
下一篇:block块中引用成员变量引起内存泄漏问题
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站