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.

时间: 2024-08-28 20:13:08

Android经常使用UI组件 - Button的相关文章

Android常用UI组件 - Button

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

Android 第九课——UI 组件

组件继承关系图: 组件通用属性: 1)layout_width:组件的宽度,一般只有三个值wrap_content.fill_parent(老版本中使用).match_parent,但他不是唯一控制宽度的元素 2)layout_height:组件的高度,一般只有三个值wrap_content.fill_parent(老版本中使用).match_parent,但他不是唯一控制高度的元素 3)width:组件的宽度,他也不是唯一控制宽度的元素,他和layout_width共同决定组件的宽度 4)he

安卓开发:UI组件-Button

2.3Button Button继承自TextView,除了通过属性设置按钮样式,还可以通过绑定drawable文件的方式来实现不同样式. 2.3.1按钮样式 新建Activity:ButtonActivity,添加Layout视图:activity_button.xml. activity_button.xml: 1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:an

Android基础——高级UI组件:图像视图和图片切换器

layout代码 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.

Android基础——高级UI组件:下拉框,列表,滚动条视图

布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.andr

Android基础——高级UI组件:选项卡

布局文件 <?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.c

Android UI组件进阶(2)——仿Windows对话框

Android UI组件进阶(2)--仿Windows对话框 在开始本章前先祝大家中秋节快乐哈,相信很多上班的朋友都是放三天假的哈! 有时间的话回家陪陪父母吧!树欲静而风不止,子欲养而亲不待!岁月不饶人! 好了,道理和祝福语就说到这里了,今天给大家准备的是模仿Windows风格对话框! 效果图: 相信大部分的AlertDialog都是下面这个样子的: 今天给大家讲解的对话框是下面这样的: 对比两种对话框,站在用户的角度,相信你更加钟情于第二种颜色鲜明的对话框 好了下面就开始讲解如何制作模仿win

Android UI组件之EditText 实现网站注册效果的校验

时间过得太快,还没有什么感觉就到周末了,人生五十载,如梦亦如幻.不过我还没那么老.前两天曾说过,如果需要实现输入IP的功能,那么我们可以整一个自定义控件,然后对他进行事件监听,巴拉巴拉一大堆,好不容易做完了.后来想想,这是为了符合我们平常在PC上的习惯而去自定义的.那么如果只是单纯的为了输入一个IP地址,然后得到结果,不想去这么折腾,有没有什么好办法呢? 很显然是有的,那就今天就来看看Android最常用的组件之一的EditText,虽然之前用的也是EditText,但是侧重点其实是自定义控件,

Android UI 组件 &#187; GifView

GifView 是一个为了解决android中现在没有直接显示gif的view,只能通过mediaplay来显示这个问题的项目,其用法和 ImageView一样,支持gif图片 使用方法: 1-把GifView.jar加入你的项目. 2-在xml中配置GifView的基本属性,GifView继承自View类,和Button.ImageView一样是一个UI控件.如: <com.ant.liao.GifView android:id="@+id/gif2" android:layo