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; } } }}

更多相关文章

  1. Android Media Recorder录音播放源代码
  2. android实现自动关机代码
  3. Android 用源代码写layout布局
  4. 记录代码合并时产生的bug
  5. Android常用功能代码块
  6. Android拍照上传代码样例
  7. 如何避免输入法弹出时遮挡住按钮或输入框
  8. android打开联系人的代码

随机推荐

  1. Android Non-UI to UI Thread Communicat
  2. Ubuntu 试用Android L版本
  3. android 之摄像头
  4. pointer location
  5. android开源新闻小程序、3D翻转公告效果
  6. Android基本组件
  7. “Error:ShouldNotReachHere()”错误的原
  8. Android P 指纹 Framework
  9. 【Android】一种提高Android应用进程存活
  10. SystemUI源码分析