android常用的组件库,Android常用UI组件 - Button

本文介绍了Android中Button组件的使用,它作为UI交互的重要元素,常常用于触发点击事件。Button继承自TextView,可通过设置OnClickListener监听器来响应用户的点击操作。示例代码展示了如何在布局文件中创建两个Button,并在Java代码中为它们设置不同的点击事件,展示点击后显示Toast消息的功能。
摘要由CSDN通过智能技术生成

按钮(Button)是Android当中一个常用的UI组件,很小但是在开发中最常用到。一般通过与监听器结合使用,从而触发一些特定事件。Button继承了TextView。它的功能就是提供一个按钮,这个按钮可以供用户点击,当用户对按钮进行操作的时候,触发相应事件,如点击,触摸。一般对于一个按钮而言,用的最多的就是点击事件,Button间接继承自View,而Android UI中的所有事件,都是定义在View中的。

实例:ButtonDemo

运行效果:

A111957770-107142.png_small.png

代码清单:

布局文件:main.xml

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

android:layout_width="fill_parent"

android:layout_height="wrap_content" android:text="Button1" />

android:layout_width="fill_parent"

android:layout_height="wrap_content" android:text="Button2" />

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值