main.xml中设置两个按钮

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<!-- 设置两个按钮文件,包裹着内容 -->
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点我"
/>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点我2"
/>
</LinearLayout>

主函数MainActivity编写按钮操作代码

package com.szy.button.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast; public class MainActivity extends Activity
{
private Button btn1=null; //设置两个按钮对象
private Button btn2=null; public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1=(Button)findViewById(R.id.btn1); //按钮对象指明了用到哪个按钮的id,绑定id号
btn2=(Button)findViewById(R.id.btn2);
btn1.setOnClickListener(listener); //设置按钮点击后的事件listener
btn2.setOnClickListener(listener); } //事件函数,表示按下后所发生的动作
private OnClickListener listener=new OnClickListener()
{ public void onClick(View v)
{
Button btn=(Button)v;
switch (btn.getId()) //判断哪个按钮被按下,获取按钮id
{
case R.id.btn1:
Toast.makeText(MainActivity.this, "你点击了按钮", Toast.LENGTH_LONG).show();
break;
case R.id.btn2:
System.out.println("你点击了Button2");
Toast.makeText(MainActivity.this, "你点击了按钮2", Toast.LENGTH_LONG).show();
break;
}
}
};
}

Android--->Button按钮操作的更多相关文章

  1. 【转】Android - Button(按钮)的响应点击事件的4种写法

    原文网址:http://www.yrom.net/blog/2011/12/12/android-4-onclicklistener-of-button/ Button控件setOnclickList ...

  2. Android Button 按钮 设置 各种状态 图片 颜色

    有2个方法可以实现,一种是用 选择器 定义每种状态的图片 selec.xml <?xml version="1.0" encoding="utf-8"?& ...

  3. Android - Button(按钮)的响应点击事件的4种写法

    Button控件setOnclickListener(View.OnClickListener listener)来接收一个点击事件的监听器 自定义一个点击事件监听器类让其实现View.OnClick ...

  4. Android学习笔记-Button(按钮)

    Button是TextView的子类,所以TextView上很多属性也可以应用到Button 上!我们实际开发中对于Button的,无非是对按钮的几个状态做相应的操作,比如:按钮按下的时候 用一种颜色 ...

  5. android Button 切换背景,实现动态按钮和按钮颜色渐变

        android Button 切换背景,实现动态按钮和按钮颜色渐变 一.添加android 背景筛选器selector实现按钮背景改变     1.右键单击项目->new->Oth ...

  6. Android 自定义Button按钮显示样式(正常、按下、获取焦点)

    现在的用户对APP的外观看得很重要,如果APP内所有元件都用Android默认样式写,估计下面评论里就有一堆在骂UI丑的.今天学习自定义Button按钮样式.Button样式修改的是Button的背景 ...

  7. android中在java代码中设置Button按钮的背景颜色

    android中在java代码中设置Button按钮的背景颜色 1.设置背景图片,图片来源于drawable: flightInfoPanel.setBackgroundDrawable(getRes ...

  8. Android学习之Button按钮在程序运行时全部变大写的处理

    问题: 在layout布局文件中,我们命名的按钮名称是“button1”,程序运行过后,在app上显示出来的是“BUTTON1”,先看源代码和效果: 按钮源代码: 运行效果: 解决办法: 方法一: 在 ...

  9. android 按钮特效 波纹 Android button effects ripple

    android 按钮特效 波纹 Android button effects ripple 作者:韩梦飞沙 Author:han_meng_fei_sha 邮箱:313134555@qq.com E- ...

  10. Android处理ListView中的Item中的Button按钮不能点击的问题

    问题描述:ListView列表中的Button按钮按钮不能点击 解决办法:在ListView中的Item项的布局文件中加上:android:descendantFocusability="b ...

随机推荐

  1. win8删除无线网络其中的一项配置

    netsh wlan delete profile name="无线名称"

  2. kloxo面板教程-折腾了一天

    ------------------------------------------------------------------------------- 前一晚安装了掉线,不得不重新来,有点慢, ...

  3. Internet History, Technology and Security (Week1)

    Week1. History: Dawn of Electronic Computing War Time Computing and Conmmunication Keywords: Electro ...

  4. angular 搜索记录保留

    #方法1: 点击后退到home后,再点击搜索, locationChangeStart 事件会多次触发. # $scope.keyword = $location.search().search # ...

  5. laravel 获取最后一条sql的小函数

    function lastSql(){ $sql = DB::getQueryLog(); $query = end($sql); return $query; }

  6. sublime 2

    just baidu sublime license and find a free one!

  7. iOS 导航栏去阴影

    if ([[[UIDevicecurrentDevice] systemVersion] floatValue] >= 6.0) { // 首先要判断版本号,否则在iOS 6 以下的版本会闪退 ...

  8. fatal error: gst/gst.h

    ln命令使用 ln -s  源文件(src)  目标文件(dest) 进到这个文件:~/LowDA/sysroots/mx6q/usr/include$ ln -s gstreamer-0.10/gs ...

  9. wget mirror

    wget -r -np -c xxx-url-xxx -r: recursive-np: no-parent-c: continue -D: domains to follow, comma sepa ...

  10. 一篇非常适合git入门的文章

    http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/00137583770360579 ...