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.

最新文章

  1. 用JavaScript来实现链表LinkedList
  2. iOS 适配https(AFNetworking3.0为例)
  3. [转] Spring注解@Component、@Repository、@Service、@Controller区别
  4. zepto源码--核心方法5(文本操作)--学习笔记
  5. 移植linux-2.6.32.2到qq2440
  6. python3批量删除豆瓣分组下的好友
  7. 【HDOJ】3308 LCIS
  8. Course Schedule ——LeetCode
  9. IOS 多于UIImageView 当加载较大的高清闪存管理
  10. Redis 部署主从哨兵 C#使用,实现自动获取redis缓存 实例1
  11. Angular5学习笔记 http请求
  12. OM模块功能&amp;API详解
  13. nginx server_name匹配顺序
  14. cxgrid合并值相同的某列
  15. 详解Paste deploy
  16. pip freeze
  17. 保护Hadoop集群三大方法
  18. JBoss 实战(1)
  19. springMVC之增删改查
  20. C# 通过form表单下载文本文件

热门文章

  1. a标签打电话
  2. C语言fopen函数了解
  3. 杭电oj2028、2034、2035、2041、2043-2046
  4. IOS深入学习(21)之Key-value coding
  5. Android 系统启动过程详解
  6. centos 资源链接
  7. 使用bottle进行web开发(2):http request
  8. org.hibernate.HibernateException: Unable to get the default Bean Validation factor
  9. hadoop之深入浅出
  10. #420 Div2 C