Android--->Button按钮操作
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按钮操作的更多相关文章
- 【转】Android - Button(按钮)的响应点击事件的4种写法
原文网址:http://www.yrom.net/blog/2011/12/12/android-4-onclicklistener-of-button/ Button控件setOnclickList ...
- Android Button 按钮 设置 各种状态 图片 颜色
有2个方法可以实现,一种是用 选择器 定义每种状态的图片 selec.xml <?xml version="1.0" encoding="utf-8"?& ...
- Android - Button(按钮)的响应点击事件的4种写法
Button控件setOnclickListener(View.OnClickListener listener)来接收一个点击事件的监听器 自定义一个点击事件监听器类让其实现View.OnClick ...
- Android学习笔记-Button(按钮)
Button是TextView的子类,所以TextView上很多属性也可以应用到Button 上!我们实际开发中对于Button的,无非是对按钮的几个状态做相应的操作,比如:按钮按下的时候 用一种颜色 ...
- android Button 切换背景,实现动态按钮和按钮颜色渐变
android Button 切换背景,实现动态按钮和按钮颜色渐变 一.添加android 背景筛选器selector实现按钮背景改变 1.右键单击项目->new->Oth ...
- Android 自定义Button按钮显示样式(正常、按下、获取焦点)
现在的用户对APP的外观看得很重要,如果APP内所有元件都用Android默认样式写,估计下面评论里就有一堆在骂UI丑的.今天学习自定义Button按钮样式.Button样式修改的是Button的背景 ...
- android中在java代码中设置Button按钮的背景颜色
android中在java代码中设置Button按钮的背景颜色 1.设置背景图片,图片来源于drawable: flightInfoPanel.setBackgroundDrawable(getRes ...
- Android学习之Button按钮在程序运行时全部变大写的处理
问题: 在layout布局文件中,我们命名的按钮名称是“button1”,程序运行过后,在app上显示出来的是“BUTTON1”,先看源代码和效果: 按钮源代码: 运行效果: 解决办法: 方法一: 在 ...
- android 按钮特效 波纹 Android button effects ripple
android 按钮特效 波纹 Android button effects ripple 作者:韩梦飞沙 Author:han_meng_fei_sha 邮箱:313134555@qq.com E- ...
- Android处理ListView中的Item中的Button按钮不能点击的问题
问题描述:ListView列表中的Button按钮按钮不能点击 解决办法:在ListView中的Item项的布局文件中加上:android:descendantFocusability="b ...
随机推荐
- VBS 读取文本文件特殊字符前如逗号的值并赋值给变量
我使用的仿真终端SecureCRT需要使用一个脚本,支持VBS的.我需要实现如下功能: 首先文本文件在:D:\100.txt文本文件的内容为:9 0,randy,9 1,jeff,9 2,sameul ...
- OutputDebugString输出调试信息到debugtrack
OutPutDebugString()函数的输出则可以用DebugView捕获(DebugView也可以捕获TRACE宏的输出)eg: OutPutDebugString("输出第一调试信息 ...
- 使用inpaint例子,去除水印
http://www.opencv.org.cn/forum.php?mod=viewthread&tid=33151 #include "stdafx.h" #inclu ...
- Educational Codeforces Round 15_C. Cellular Network
C. Cellular Network time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- hdu_1513_Palindrome(LCS+滚动数组)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 题意:给你一个字符串,问你最少插入多少个字符使其为回文字符. 题解:将字符串倒着保存,然后求一下 ...
- hdu_1254_推箱子(双BFS)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1254 题解:以箱子为主体,第一层BFS,然后用第二层BFS来判断人是否可以到达,这里细节比较多,要注意 ...
- LeetCode OJ 337. House Robber III
The thief has found himself a new place for his thievery again. There is only one entrance to this a ...
- WisKey的眼神
WisKey的眼神 Time Limit : 1000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Sub ...
- Let'sencrypt认证的网站Https配置
推荐使用这个脚本,具体说明里面都有 https://github.com/xdtianyu/scripts/tree/master/le-dns 它是通过调用dns服务商的api更新txt记录实现,无 ...
- 12C cdb/pdb 配置监听
. PDB is not an instance, so using SID in the connection string will not work. When the database is ...