Button 在布局文件中定义监听器,文字阴影,自定义图片,代码绘制样式,添加音效的方法
1.Button自己在xml文件中绑定监听器
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#DFDDDE"> <!-- 设定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"/> </LinearLayout>
对应的方法
/**当用户点击按钮时,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;
}
}
2.自定义点击,按下的样式
<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>
3.用代码来设定按钮普通/按下后的图片样式
<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="用代码动态设定点击样式" />
代码中用onTouchListener监听器:
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) { 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;
}
});
4.用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>
用法:
<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画的圆角按钮" />
5.用style来指定样式
<?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值的相对位置 -->
6.指定按钮的音效
先定义对象
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);
}
});
7.加文字阴影的方式(设定阴影的偏移量和宽度)
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:shadowColor="#aa5"
android:shadowDx="5"
android:shadowDy="5"
android:shadowRadius="1"
android:textSize="25sp"
android:text="shadow" />
源码下载:http://download.csdn.net/detail/shark0017/7592855
Button 在布局文件中定义监听器,文字阴影,自定义图片,代码绘制样式,添加音效的方法的更多相关文章
- 在布局文件中使用Fragment的步骤
为了在Activity布局文件中使用Fragment我们需要四个步骤. 1.定义一个Activity,他继承android.support.v4.app.FragmentActivity,下面是关键代 ...
- Android 自定义View及其在布局文件中的使用示例
前言: 尽管Android已经为我们提供了一套丰富的控件,如:Button,ImageView,TextView,EditText等众多控件,但是,有时候在项目开发过程中,还是需要开发者自定义一些需要 ...
- Android 自定义View及其在布局文件中的使用示例(三):结合Android 4.4.2_r1源码分析onMeasure过程
转载请注明出处 http://www.cnblogs.com/crashmaker/p/3549365.html From crash_coder linguowu linguowu0622@gami ...
- ASP.NET MVC 4 (十一) Bundles和显示模式--asp.net mvc中 @Scripts.Render("~/bundles/jquery")是什么意思? 在布局文件中使用Scripts.Render()输出脚本包,Styles.Render()输出风格包:
ASP.NET MVC 4 (十一) Bundles和显示模式 ASP.NET MVC 4 引入的js打包压缩功能.打包压缩jquery目录下的文件,在布局文件中使用Scripts.Render()输 ...
- Android中布局文件中使用onClick属性
安卓开发中,布局文件中的控件有一个属性,是onClick,例如: <Button android:id="@+id/button1" ...
- android 布局文件中xmlns:android="http://schemas.android.com/apk/res/android"
http://blog.163.com/benben_long/blog/static/199458243201411394624170/ xmlns:android="http://sch ...
- Android查缺补漏(View篇)--布局文件中的“@+id”和“@id”有什么区别?
Android布局文件中的"@+id"和"@id"有什么区别? +id表示为控件指定一个id(新增一个id),如: <cn.codingblock.vie ...
- Android 自定义View及其在布局文件中的使用示例(二)
转载请注明出处 http://www.cnblogs.com/crashmaker/p/3530213.html From crash_coder linguowu linguowu0622@gami ...
- c语言头文件中定义全局变量的问题
c语言头文件中定义全局变量的问题 (转http://www.cnblogs.com/Sorean/) 先说一下,全局变量只能定义在 函数里面,任意函数,其他函数在使用的时候用extern声明.千万不要 ...
随机推荐
- net mvc webapi 实用
asp.net mvc webapi 实用的接口加密方法 在很多项目中,因为webapi是对外开放的,这个时候,我们就要得考虑接口交换数据的安全性. 安全机制也比较多,如andriod与webap ...
- 【LOJ】#2493. 「BJOI2018」染色
题面 题解 推结论大题--然而我推不出什么结论 奇环显然是NO 如果一个联通块里有两个分离的环,也是NO 如果一个联通块里,点数为n,边数为m m <= n的时候,是YES m >= n ...
- JDBC事务与事务隔离级别详解
事务基本概念 一组要么同时执行成功,要么同时执行失败的SQL语句.是数据库操作的一个执行单元. 事务开始于: 连接到数据库上,并执行一条DML语句insert.update或delete 前一个事务结 ...
- linux 时间戳
一.https://www.cnblogs.com/33debug/p/6632172.html 二.显示前一小时时间 date -d '-1 hour' '+%F-%H-%M-%S'
- mysql xtrabackup工具备份
一.注意事项 建议用xtrabackup备份时采用全备或增量备份的方式,楼主也尝试过单库备份,但是都以备份后恢复到已经存在数据库的mysqldata目录下后部分库会出幺蛾子而告终,建议使用mysqld ...
- hadoop日志数据分析开发步骤及代码
日志数据分析:1.背景1.1 hm论坛日志,数据分为两部分组成,原来是一个大文件,是56GB:以后每天生成一个文件,大约是150-200MB之间:1.2 日志格式是apache common日志格式: ...
- 算法初级面试题01——认识时间复杂度、对数器、 master公式计算时间复杂度、小和问题和逆序对问题
虽然以前学过,再次回顾还是有别样的收获~ 认识时间复杂度 常数时间的操作:一个操作如果和数据量没有关系,每次都是固定时间内完成的操作,叫做常数操作. 时间复杂度为一个算法流程中,常数操作数量的指标.常 ...
- HDU5919 SequenceⅡ
从后向前建主席树,以位置为下标建树,然后查询区间出现次数的第k/2大即可. 复杂度O(nlogn) #include<bits/stdc++.h> using namespace std; ...
- UVALive 6912 Prime Switch 状压DP
Prime Switch 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8& ...
- 导入导出CSV
const string dataPath = @"D:\Users\jin_h\Documents\Visual Studio 2013\Projects\ConsoleApplicati ...