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的更多相关文章

  1. ReactNative Android之原生UI组件动态addView不显示问题解决

    ReactNative Android之原生UI组件动态addView不显示问题解决 版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请表明出处:http://www.cnblogs.com ...

  2. Android开发 ---基本UI组件4:拖动事件、评分进度条、圆圈式进度条、进度条控制

    Android开发 ---基本UI组件4 1.activity_main.xml 描述: 定义了一个按钮 <?xml version="1.0" encoding=" ...

  3. Android开发 ---基本UI组件3:单选按钮、多选按钮、下拉列表、提交按钮、重置按钮、取消按钮

    Android开发 ---基本UI组件2 1.activity_main.xml 描述: 定义一个用户注册按钮 <?xml version="1.0" encoding=&q ...

  4. Android开发 ---基本UI组件2:图像按钮、单选按钮监听、多选按钮监听、开关

    Android开发 ---基本UI组件2 1.activity_main.xml 描述: 定义一个按钮 <?xml version="1.0" encoding=" ...

  5. Android学习笔记⑤——UI组件的学习TextView相关

    TextView是一个强大的视图组件,直接继承了View,同时也派生出了很多子类,TextView其作用说白了就是在布局中显示文本,有点像Swing编程中的JLabel标签,但是他比JLabel强大的 ...

  6. Android经常使用UI组件 - TextView

    TextView是Android里面用的最多的UI组件,一般使用在须要显示一些信息的时候,其不能输入,仅仅能初始设定或者在程序中改动. 实例:TextViewDemo 执行效果: 代码清单: 布局文件 ...

  7. UI 组件 | Button

    最近在与其他自学 Cocos Creator 的小伙伴们交流过程中,发现许多小伙伴对基础组件的应用并不是特别了解,自己在编写游戏的过程中也经常对某个属性或者方法的用法所困扰,而网上也没有比较清晰的用法 ...

  8. Android开发 ---基本UI组件6 :只定义一个listView组件,然后通过BaseAdapter适配器根据数据的多少自行添加多个ListView显示数据

    效果图: 1.activity_main.xml 描述: 定义了一个按钮 <?xml version="1.0" encoding="utf-8"?> ...

  9. Android系列之UI组件----Menu菜单

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

随机推荐

  1. spring in action 学习笔记九:如何证明在scope为prototype时每次创建的对象不同。

    spring 中scope的值有四个:分别是:singleton.prototype.session.request.其中session和request是在web应用中的. 下面证明当scope为pr ...

  2. 行为型设计模式之职责链模式(Chain of Responsibility)

    结构 意图 使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系.将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止. 适用性 有多个的对象可以处理一个请求,哪个 ...

  3. bzoj 3744 Gty的妹子序列 区间逆序对数(在线) 分块

    题目链接 题意 给定\(n\)个数,\(q\)个询问,每次询问\([l,r]\)区间内的逆序对数. 强制在线. 思路 参考:http://www.cnblogs.com/candy99/p/65795 ...

  4. 【linux高级程序设计】(第十一章)System V进程间通信 2

    消息队列 消息队列是消息的链式队列,模型如下: 包括两种数据结构: msqid_ds消息队列数据结构 msg消息队列数据结构 struct msg_msg{ struct list_head m_li ...

  5. RobotFramework自动化1-环境搭建【转载】

    本篇转自博客:上海-悠悠 原文地址:http://www.cnblogs.com/yoyoketang/tag/robotframework/ 前言 Robot Framework是一款python编 ...

  6. 在线查看PDF文档

    http://www.cnblogs.com/morang/p/4598894.html http://78re52.com1.z0.glb.clouddn.com/resource%2Fscenar ...

  7. 【转载】无需图片,使用CSS3实现圆角按钮

    原文地址:http://www.open-open.com/home/space-37924-do-blog-id-5789.html 首先来看看效果: 事例HTML代码: <a href=&q ...

  8. 解析 Lambda 表达式

    我们先创建一个表达式树: Expression<Func<int, int, int>> expression = (a,b) => a + b; 我们的例子是一个Exp ...

  9. JDK7集合框架源码阅读(四) LinkedHashMap

    基于版本jdk1.7.0_80 java.util.LinkedHashMap 代码如下 /* * Copyright (c) 2000, 2010, Oracle and/or its affili ...

  10. Ubuntu角色登录答疑

    1.su 命令验证出错: $ su - rootPassword: su: Authentication failureSorry. 这时候输入 $ sudo passwd rootEnter new ...