Button自己在xml文件中绑定监听器

<!-- 设定onclick属性,然后在activity中定义相应的方法 -->
<!-- 通过xml布局中通过button的android:onClick属性指定一个方法,
以替代在activity中为button设置OnClickListener。 -->
<Button
android:id="@+id/my_button_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Button自己绑定点击事件"
android:layout_marginTop="16dp"
android:onClick="buttonListener"/>

代码中写一个方法:

	/**当用户点击按钮时,Android系统调用buttonListener(View)方法。
* 为了正确执行,这个方法必须是public并且仅接受一个View类型的参数
* @param v button传过来的view对象
* 需要注意的是这个方法必须符合三个条件:
   1.public
   2.返回void
   3.只有一个参数View,这个View就是被点击的这个控件。
*/
public void buttonListener(View v){
switch (v.getId()) {
case R.id.my_button_id:
Toast.makeText(getApplicationContext(), "button自己绑定一个触发函数", 0).show();
break; default:
break;
}
}

自定义点击,按下的样式

 <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="16dp"
android:background="@drawable/button_selector"
android:text="自定义按钮样式" />

button_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/login_button2" />
<item android:state_focused="true"
android:drawable="@drawable/login_button2" />
<item android:drawable="@drawable/login_button" />
</selector>

用代码来设定按钮的图片样式

<Button
android:id="@+id/style_button_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="16dp"
android:text="用代码动态设定点击样式" />

activity中里:

Button styleBt = (Button)findViewById(R.id.style_button_id);
styleBt.setBackgroundColor(Color.parseColor("#b9e3d9"));
styleBt.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO 自动生成的方法存根
if(event.getAction()==MotionEvent.ACTION_DOWN){
//v.setBackgroundResource(R.drawable.bar_color);
v.setBackgroundColor(Color.WHITE);
}
else if(event.getAction()==MotionEvent.ACTION_UP){
v.setBackgroundColor(Color.parseColor("#b9e3d9"));
}
return false;
}
});

用shape画按钮

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/shape"
android:layout_marginTop="16dp"
android:text="用shape画的圆角按钮" />

shape.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- 填充的颜色 -->
<solid android:color="#FFFFFF" />
<!-- 设置按钮的四个角为弧形 -->
<!-- android:radius 弧形的半径 -->
<corners android:radius="5dip" /> <!-- padding:Button里面的文字与Button边界的间隔 -->
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>

用style来设定样式

        <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/style_selector"
android:layout_marginTop="16dp"
android:text="用selector设计的按钮样式" />

style_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true">
<shape>
<gradient android:startColor="#0d76e1" android:endColor="#0d76e1"
android:angle="270" />
<stroke android:width="1dip" android:color="#f403c9" />
<corners android:radius="2dp" />
<padding android:left="10dp" android:top="10dp"
android:right="10dp" android:bottom="10dp" />
</shape>
</item> <item android:state_focused="true">
<shape>
<gradient android:startColor="#ffc2b7" android:endColor="#ffc2b7"
android:angle="270" />
<stroke android:width="1dip" android:color="#f403c9" />
<corners android:radius="2dp" />
<padding android:left="10dp" android:top="10dp"
android:right="10dp" android:bottom="10dp" />
</shape>
</item> <item>
<shape>
<gradient android:startColor="#CEEDEB" android:endColor="#ffffff"
android:angle="-90" />
<stroke android:width="1dip" android:color="#f403c9" />
<corners android:radius="5dip" />
<padding android:left="10dp" android:top="10dp"
android:right="10dp" android:bottom="10dp" />
</shape>
</item>
</selector> <!--
gradient 主体渐变
startColor开始颜色,
endColor结束颜色 ,
angle开始渐变的角度(值只能为90的倍数,0时为左到右渐变,90时为下到上渐变,依次逆时针类推)
stroke 边框 width 边框宽度,color 边框颜色
corners 圆角 radius 半径,0为直角
padding text值的相对位置 -->

有音效的按钮

        <Button
android:id="@+id/music_button_id"
android:layout_gravity="center"
android:layout_marginTop="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="有音效的按钮" />

activity中写:

	private SoundPool sp;//声明一个SoundPool
private int music;//定义一个整型用load();来设置suondID
 sp= new SoundPool(10, AudioManager.STREAM_SYSTEM, 5);//第一个参数为同时播放数据流的最大个数,第二数据流类型,第三为声音质量
music = sp.load(this, R.raw.click,1); //把你的声音素材放到res/raw里,第2个参数即为资源文件,第3个为音乐的优先级 Button musicBt = (Button)findViewById(R.id.music_button_id);
musicBt.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
sp.play(music, 1, 1, 0, 0, 1);
}
});

源码下载:http://download.csdn.net/detail/shark0017/7592855

Button 自定义图片,代码绘制样式,添加音效的方法的更多相关文章

  1. 如何使用SublimeText风格的代码高亮样式 添加Zed Coding(EMMET)插件

    因为觉得博客园自带的代码高亮样式很单一,不符合作为前端的我的审美习惯,于是下定决心要想办法折腾出一个方法来应用上另外一套代码高亮样式. 虽然探索的过程是很痛苦的,但最后还是成功了,但也不枉付出的那些努 ...

  2. MYSQL 代码删除和添加表格列方法

    一个表格建立后用代码删除或添加列: -- 删除列alter table teacher drop column create_time;-- 添加列alter table teacher add co ...

  3. Button 在布局文件中定义监听器,文字阴影,自定义图片,代码绘制样式,添加音效的方法

    1.Button自己在xml文件中绑定监听器 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/andro ...

  4. button 左边图片右边文字样式

        状态值 : 正常 状态值 : 选中   #pragma mark - buttonPress- (void)buttonPress:(UIButton * )sender {     if ( ...

  5. MySQL中给自定义的字段查询结果添加排名的方法

    我正在用 MySQL 客户端的时候,突然想到如果可以给查询结果添加排名该多好啊,然后就找到了一个简单的解决办法. 下面是一个示例表的数据:  然后我们要根据 Roll_No 字段进行排序并给出排名,我 ...

  6. Android自定义“图片+文字”控件四种实现方法之 二--------个人最推荐的一种

    http://blog.csdn.net/yanzi1225627/article/details/8633872 第二种方法也要新建一个图片+文字的xml布局文件,然后写一个类继承自LinearLa ...

  7. 自定义Button 的图片设置不显示问题。

    如果你是自定义button  那么你设置图片就要用 button.imageView.image = [UIImage imageName:@""]; 如果你是给系统原生的butt ...

  8. 自定义View实现图片的绘制、旋转、缩放

    1.图片 把一张JPG图片改名为image.jpg,然后拷贝到项目的res-drawable中. 2.activity_main.xml <LinearLayout xmlns:android= ...

  9. [PHP] JQuery+Layer实现添加删除自定义标签代码

    JQuery+Layer实现添加删除自定义标签代码 实现效果如下: 实现代码如下: <!doctype html> <html> <head> <meta c ...

随机推荐

  1. 用python实现一个无界面的2048

    转载请注明出处http://www.cnblogs.com/Wxtrkbc/p/5519453.html 以前游戏2048火的时候,正好用其他的语言编写了一个,现在学习python,正好想起来,便决定 ...

  2. 记录一下SparkStreaming中因为使用redis做数据验证而导致数据结果不对的问题

    业务背景: 需要通过redis判断当前用户是否是新用户.当出现新用户后,会将该用户放入到redis中,以标明该用户已不是新用户啦. 出现问题: 发现入库时,并没有新用户入库,但我看了数据了,确实应该是 ...

  3. DJANGO ADMIN 一些有用的设置(转)

    DJANGO ADMIN 一些有用的设置   Django自带的后台管理是Django明显特色之一,可以让我们快速便捷管理数据.后台管理可以在各个app的admin.py文件中进行控制.以下是我最近摸 ...

  4. BeanUtils工具

    什么是BeanUtils工具 BeanUtils工具是一种方便我们对JavaBean进行操作的工具,是Apache组织下的产品. BeanUtils工具一般可以方便javaBean的哪些操作? 1)b ...

  5. SQL语句之 多表管理

    SQL语句之 多表管理 一个数据库内通常会有不止一张表,有时候我们要把多张表联系起来,这就需要用到多表管理的语句. 1.外键约束 一个表中的非主键字段,如果在另外一张表中是主键,那么这个字段我们叫它做 ...

  6. BZOJ.4456.[ZJOI2016]旅行者(分治 Dijkstra)

    题目链接 \(Description\) 给定\(n\times m\)的带边权网格图.\(Q\)次询问从点\((x_i,y_i)\)到点\((x_j,y_j)\)的最短路. \(n\times m\ ...

  7. HDU 4741 Save Labman No.004 (2013杭州网络赛1004题,求三维空间异面直线的距离及最近点)

    Save Labman No.004 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  8. C++ Primer笔记3_默认实參_类初探_名字查找与类的作用域

    1.默认函数实參 在C++中,能够为參数指定默认值,C语言是不支持默认參数的,Java也不支持! 默认參数的语法与使用: (1)在函数声明或定义时,直接对參数赋值.这就是默认參数: (2)在函数调用时 ...

  9. USBDM RS08/HCS08/HCS12/Coldfire V1,2,3,4/DSC/Kinetis Debugger and Programmer -- MC9S08JM16/32/60

    Introduction The attached files provide a port of a combined TBDML/OSBDM/TBLCF code to a MC9S08JM16/ ...

  10. AES advanced encryption standard 3

    This optimized <../aesbench/> AES implementation conforms to FIPS-. aes.h #ifndef _AES_H #defi ...