Android经常使用UI组件 - Button
button(Button)是Android其中一个经常使用的UI组件。非常小可是在开发中最经常使用到。一般通过与监听器结合使用。从而触发一些特定事件。
Button继承了TextView。它的功能就是提供一个button,这个button能够供用户点击。当用户对button进行操作的时候,触发对应事件,如点击。触摸。一般对于一个button而言,用的最多的就是点击事件,Button间接继承自View。而Android UI中的全部事件。都是定义在View中的。
实例:ButtonDemo
执行效果:
代码清单:
布局文件: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"
>
<Button android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="Button1" />
<Button android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="Button2" />
</LinearLayout>
Java源码文件:ActivityButton.java
package com.rainsong.buttondemo; 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 ActivityButton extends Activity
{
Button btn1;
Button btn2; OnClickListener listener1;
OnClickListener listener2; /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main); listener1 = new OnClickListener() {
public void onClick(View v) {
Toast.makeText(ActivityButton.this, "Button1 clicked",Toast.LENGTH_SHORT).show();
}
};
listener2 = new OnClickListener() {
public void onClick(View v) {
Toast.makeText(ActivityButton.this, "Button2 clicked",Toast.LENGTH_SHORT).show();
}
}; btn1 = (Button)findViewById(R.id.button1);
btn1.setOnClickListener(listener1); btn2 = (Button)findViewById(R.id.button2);
btn2.setOnClickListener(listener2);
}
}
API知识点
Activity
public class
Activity
extends ContextThemeWrapper
implements ComponentCallbacks2 KeyEvent.Callback LayoutInflater.Factory2 View.OnCreateContextMenuListener Window.Callback
View findViewById(int id)
Finds a view that was identified by the id attribute from the XML that was processed in onCreate(Bundle).
void setContentView(int layoutResID)
Set the activity content from a layout resource.
View
public class
View
extends Object
implements Drawable.Callback KeyEvent.Callback AccessibilityEventSource
void setOnClickListener(View.OnClickListener l)
Register a callback to be invoked when this view is clicked.
Button
public class
Button
extends TextView
View.OnClickListener
public static interface
View.OnClickListener
abstract void onClick(View v)
Called when a view has been clicked.
Toast
public class
Toast
extends Object
Constants
int LENGTH_LONG Showthe view or text notification for a long period of time.
int LENGTH_SHORT Showthe view or text notification for a short period of time.
static Toast
makeText(Context context, int resId, int duration)
Make a standard toast that just contains a text view with the text from a resource.
static Toast
makeText(Context context, CharSequence text, int duration)
Make a standard toast that just contains a text view.
void
show()
Show the view for the specified duration.
Android经常使用UI组件 - Button的更多相关文章
- ReactNative Android之原生UI组件动态addView不显示问题解决
ReactNative Android之原生UI组件动态addView不显示问题解决 版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请表明出处:http://www.cnblogs.com ...
- Android开发 ---基本UI组件4:拖动事件、评分进度条、圆圈式进度条、进度条控制
Android开发 ---基本UI组件4 1.activity_main.xml 描述: 定义了一个按钮 <?xml version="1.0" encoding=" ...
- Android开发 ---基本UI组件3:单选按钮、多选按钮、下拉列表、提交按钮、重置按钮、取消按钮
Android开发 ---基本UI组件2 1.activity_main.xml 描述: 定义一个用户注册按钮 <?xml version="1.0" encoding=&q ...
- Android开发 ---基本UI组件2:图像按钮、单选按钮监听、多选按钮监听、开关
Android开发 ---基本UI组件2 1.activity_main.xml 描述: 定义一个按钮 <?xml version="1.0" encoding=" ...
- Android学习笔记⑤——UI组件的学习TextView相关
TextView是一个强大的视图组件,直接继承了View,同时也派生出了很多子类,TextView其作用说白了就是在布局中显示文本,有点像Swing编程中的JLabel标签,但是他比JLabel强大的 ...
- Android经常使用UI组件 - TextView
TextView是Android里面用的最多的UI组件,一般使用在须要显示一些信息的时候,其不能输入,仅仅能初始设定或者在程序中改动. 实例:TextViewDemo 执行效果: 代码清单: 布局文件 ...
- UI 组件 | Button
最近在与其他自学 Cocos Creator 的小伙伴们交流过程中,发现许多小伙伴对基础组件的应用并不是特别了解,自己在编写游戏的过程中也经常对某个属性或者方法的用法所困扰,而网上也没有比较清晰的用法 ...
- Android开发 ---基本UI组件6 :只定义一个listView组件,然后通过BaseAdapter适配器根据数据的多少自行添加多个ListView显示数据
效果图: 1.activity_main.xml 描述: 定义了一个按钮 <?xml version="1.0" encoding="utf-8"?> ...
- Android系列之UI组件----Menu菜单
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...
随机推荐
- Codeforces 934.A A Compatible Pair
A. A Compatible Pair time limit per test 1 second memory limit per test 256 megabytes input standard ...
- myeclipse maven web项目配置
启用maven:window-->preference-->MyEclipse-->Maven4MyEclipse, 勾选复选框(Enable Mave4MyEclipse feat ...
- 汕头市队赛SRM14 T3覆盖
我们可以考虑两种情况 区间之间不相重叠 和 重叠 f[i][j]表示以当前最后一个区间以 i 结尾 并且选了 j 个区间 不相重叠的话 只要选 1-i-w 的max再加上 包含i在内的前四个数的和 相 ...
- 关于each()、find()、filter()遍历节点的操作方法
关于each().find().filter()遍历节点的操作方法 each语法: each(fn) ; 返回值:jQuery fn:代表对于每个匹配元素所要执行的函数 each()方法共有三种形式 ...
- MFC 获得各类指针、句柄的方法(转)
原文转自 https://blog.csdn.net/abcjennifer/article/details/7480019 1.MFC中获取常见类句柄<视图类,文档类,框架类,应用程序类> ...
- 计蒜客 17119.Trig Function-切比雪夫多项式+乘法逆元 (2017 ACM-ICPC 亚洲区(西安赛区)网络赛 F)
哈哈哈哈哈哈哈哈哈哈哈哈,终于把这道题补出来了_(:з」∠)_ 来写题解啦. _(:з」∠)_ _(:з」∠)_ _(:з」∠)_ _(:з」∠)_ _(:з」∠)_ 哈哈哈哈哈哈,从9月16日打了这 ...
- HDU 3466 Proud Merchants【贪心 + 01背包】
Recently, iSea went to an ancient country. For such a long time, it was the most wealthy and powerfu ...
- Python的网络编程[3] -> BOOTP 协议[1] -> BOOTP 的 Python 实现
BOOTP实现 / BOOTP Implement 目录 BOOTP 的服务器建立过程 BOOTP 的客户端建立过程 Note: 理论部分请参考文末相关阅读链接 1 BOOTP 的服务器建立过程 服务 ...
- GIF工具 | 分享几个Gif相关工具
文章目录 LICEcap GiFResizer LICEcap LICEcap 是一款屏幕录制工具,支持导出GIF动画图片格式,轻量级.使用简单,录制过程中可以随意改变录屏范围. 下载地址: 官方地址 ...
- linux 服务器信息查看
写项目总结报告,需要统计需要系统的配合 1.# uname -a (Linux查看版本当前操作系统内核信息) Linux localhost.localdomain 2.4.20-8 #1 Thu ...