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. 从K近邻算法、距离度量谈到KD树、SIFT+BBF算法

    转载自:http://blog.csdn.net/v_july_v/article/details/8203674/ 从K近邻算法.距离度量谈到KD树.SIFT+BBF算法 前言 前两日,在微博上说: ...

  2. 每天一个小算法(insertion sort3)

    今天多看看插入排序的理论部分. 先贴几个概念吧: 1.伪代码(英语:pseudocode),又称为虚拟代码,是高层次描述算法的一种方法.它不是一种现实存在的编程语言(已经出现了类似伪代码的语言,参见N ...

  3. Codeforces Round #504:D. Array Restoration

    D. Array Restoration 题目链接:https://codeforces.com/contest/1023/problem/D 题意: 给出一个序列,现在要求对一个全为0的序列执行q次 ...

  4. HDU 1711 Number Sequence---KMP原始

    #include<stdio.h> #include<string.h> #include<math.h> #include<stdlib.h> #in ...

  5. [ CodeVS冲杯之路 ] P3027

    不充钱,你怎么AC? 题目:http://codevs.cn/problem/3027/ 显然是DP题,先按线段的右端点升序排序 设 f[i] 为dp到第 i 个线段时最大的价值 目标状态为 max( ...

  6. 手一抖误删了根目录 /usr 之后的挽救过程

    一切悲剧来源于写的Shell没有好好检查,执行后把开发机的根目录 /usr 目录给删除了,而且是root执行,众所周知,/usr目录里有大量的应用层程序,删除之后导致大量命令无法使用,如 ssh / ...

  7. (七)insmod/rmmod

    insmod: insmod命令用于将给定的模块加载到内核中.Linux有许多功能是通过模块的方式,在需要时才载入kernel.如此可使kernel较为精简,进而提高效率,以及保有较大的弹性.这类可载 ...

  8. C 实现删除非空文件夹

    /* 文件名:   rd.c ---------------------------------------------------- c中提供的对文件夹操作的函数,只能对空文件夹进行 删除,这使很多 ...

  9. 【Android开发日记】之入门篇(一)——开发环境的搭建

    写给自己的话:至此,大学的时光已经剩下一年的时光,下一年等毕业设计结束后就算是正式地踏入社会.自己学android也不过几个月的时间,为了更好管理文档,写点东西记录下自己曾经做过的点点滴滴是一个不错的 ...

  10. Appium+python自动化18-brew、carthage和appium-doctor【转载】

    前言 本篇安装brew.carthage,解决启动appium时的报错问题,另外安装appium-doctor检查appium的环境 1.brew 2.carthage 3.appium-doctor ...