Android学习 12-> 按钮Button
2015-06-23 17:18
591 查看
button按钮,和所有控件一样,在布局文件拖拽一个按钮,在代码文件中通过id过去即可,通常请求下使用button按钮,我们会为它写上一个监听器,用来控制按下button以后的操作,一般都是跳转另一个界面或者弹出某个信息。
要实现事件监听就要在xml中添加Button按钮时添加 android:onClick=" __ "即设置点击事件的方法;在java程序中
通过Intent来实现事件的跳转,用startActivity()来启动相应的事件。
android:shadowColor //阴影颜色
android:shadowRadius //阴影半径
android:shadowDx //阴影x坐标位移
android:shadowDy //阴影y坐标位移
android:background //带背景或者设置属性 若是android:background="@drawable/ped"将设置按钮的背景为图片资源;若是android:background="@android:color/black"则是将背景设置为系统颜色。
实现方法:可以用实现接口或匿名内部类的形式;由于匿名内部类用习惯了,此处以匿名内部类的方法操作
实现如图所示:
![](http://img.blog.csdn.net/20150623185249449?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcTM5NDg5NTMwMg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
其xml代码如下:
其Java运行方法为:
当然,以上的类是已经建立好的,在AndroidMafest.xml文件中也注册登记好了的。这样就可以实现点击相应的按钮直接跳转到相应的界面当中去了。
自定义Button:在Drawable文件下建立一个xml文件,其xml格式如下:
在上面的设置中也可以选择其他的:上面的state_pressed为按下时的状态;state_focused是选择焦点;state_enabled为单个属性;state_cheeked 为选中等。
在设置Button的属性中将背景设为应用该xml文件就可以实现其相应的自定义格式了,当然之上的是引用系统颜色变化来实现的。
要实现事件监听就要在xml中添加Button按钮时添加 android:onClick=" __ "即设置点击事件的方法;在java程序中
<span style="font-size:18px;"><span style="font-size:18px;"><span style="font-size:18px;">public void __ (View v) { Intent intent = new Intent(this, .class); startActivity(intent); }</span></span></span>
通过Intent来实现事件的跳转,用startActivity()来启动相应的事件。
android:shadowColor //阴影颜色
android:shadowRadius //阴影半径
android:shadowDx //阴影x坐标位移
android:shadowDy //阴影y坐标位移
android:background //带背景或者设置属性 若是android:background="@drawable/ped"将设置按钮的背景为图片资源;若是android:background="@android:color/black"则是将背景设置为系统颜色。
实现方法:可以用实现接口或匿名内部类的形式;由于匿名内部类用习惯了,此处以匿名内部类的方法操作
实现如图所示:
其xml代码如下:
<span style="font-size:18px;"><ScrollView 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" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" > <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="onLinerLayoutClickView" android:text="线型布局" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="onFrameCheckView" android:text="单帧布局" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="onRelativeCheckView" android:text="相对布局" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="onTableCheckView" android:text="表格布局" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="onRadioCheckView" android:text="单选,多选布局" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="onGridCheckView" android:text="网格布局" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="onLoginCheckView" android:text="登陆练习" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="onSendCheckView" android:text="传参练习" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="onListsViewCheckView" android:text="ListsView学习" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="onSimpleListViewCheckView" android:text="SimpleListView学习" /> </LinearLayout> </ScrollView></span>
其Java运行方法为:
<span style="font-size:18px;">package com.sc.android; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import com.sc.android.activity.SendActivity; import com.sc.android.ui.component.LoignViewActivity; import com.sc.android.ui.component.RadioButtonCheckBoxActivity; import com.sc.android.ui.layout.FrameLayoutActivity; import com.sc.android.ui.layout.GridLayoutActivity; import com.sc.android.ui.layout.LinerLayoutActivity; import com.sc.android.ui.layout.RelativeLayoutActivity; import com.sc.android.ui.layout.TableLayoutActivity; import com.sc.android.ui.listview.ListsView; import com.sc.android.ui.listview.SimpleListView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void onLinerLayoutClickView(View v) { Intent intent = new Intent(this, LinerLayoutActivity.class); startActivity(intent); } public void onRelativeCheckView(View v) { Intent intent = new Intent(this, RelativeLayoutActivity.class); startActivity(intent); } public void onTableCheckView(View v) { Intent intent = new Intent(this, TableLayoutActivity.class); startActivity(intent); } public void onGridCheckView(View v) { Intent intent = new Intent(this, GridLayoutActivity.class); startActivity(intent); } public void onFrameCheckView(View v) { Intent intent = new Intent(this, FrameLayoutActivity.class); startActivity(intent); } public void onRadioCheckView(View v) { Intent intent = new Intent(this, RadioButtonCheckBoxActivity.class); startActivity(intent); } public void onLoginCheckView(View v) { Intent intent = new Intent(this, LoignViewActivity.class); startActivity(intent); } public void onSendCheckView(View v) { Intent intent = new Intent(this, SendActivity.class); startActivity(intent); } public void onListsViewCheckView(View v) { Intent intent = new Intent(this, ListsView.class); startActivity(intent); } public void onSimpleListViewCheckView(View v) { Intent intent = new Intent(this, SimpleListView.class); startActivity(intent); } } </span>
当然,以上的类是已经建立好的,在AndroidMafest.xml文件中也注册登记好了的。这样就可以实现点击相应的按钮直接跳转到相应的界面当中去了。
自定义Button:在Drawable文件下建立一个xml文件,其xml格式如下:
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:drawable="@android:color/holo_orange_dark" android:state_pressed="true"/> <item android:drawable="@android:color/holo_green_dark"/> </selector></span>
在上面的设置中也可以选择其他的:上面的state_pressed为按下时的状态;state_focused是选择焦点;state_enabled为单个属性;state_cheeked 为选中等。
在设置Button的属性中将背景设为应用该xml文件就可以实现其相应的自定义格式了,当然之上的是引用系统颜色变化来实现的。
相关文章推荐
- android基本界面控件
- Freescale I.mx 6 Android 4.2.2源码编译环境搭建(基于ubuntu12.04 LTS)
- Freescale I.mx 6 Android 4.2.2源码编译环境搭建(基于ubuntu12.04 LTS)
- Freescale I.mx 6 Android 4.2.2源码编译环境搭建(基于ubuntu12.04 LTS)
- Freescale I.mx 6 Android 4.2.2源码编译环境搭建(基于ubuntu12.04 LTS)
- Freescale I.mx 6 Android 4.2.2源码编译环境搭建(基于ubuntu12.04 LTS)
- Freescale I.mx 6 Android 4.2.2源码编译环境搭建(基于ubuntu12.04 LTS)
- Freescale I.mx 6 Android 4.2.2源码编译环境搭建(基于ubuntu12.04 LTS)
- Freescale I.mx 6 Android 4.2.2源码编译环境搭建(基于ubuntu12.04 LTS)
- Freescale I.mx 6 Android 4.2.2源码编译环境搭建(基于ubuntu12.04 LTS)
- Freescale I.mx 6 Android 4.2.2源码编译环境搭建(基于ubuntu12.04 LTS)
- Freescale I.mx 6 Android 4.2.2源码编译环境搭建(基于ubuntu12.04 LTS)
- Freescale I.mx 6 Android 4.2.2源码编译环境搭建(基于ubuntu12.04 LTS)
- Freescale I.mx 6 Android 4.2.2源码编译环境搭建(基于ubuntu12.04 LTS)
- Freescale I.mx 6 Android 4.2.2源码编译环境搭建(基于ubuntu12.04 LTS)
- android学习笔记五—android的存储2 文件的读取与写入
- Android自动化打包之一:aapt的用法
- android Touch事件传递机制解析
- 《第一行代码——Android》
- android基础学习之RSA加密解密