Android应用开发基础篇(1)-----Button
2013-06-17 13:44
471 查看
Android应用开发基础篇(1)-----Button
一、概述Button,顾名思义就是按钮的意思,它主要的功能是响应用户按下按钮时的动作。
二、应用
新建一个工程,名字为MyButton,在/res/layout/main.xml文件中添加以下内容:
1 <Button 2 android:id="@+id/button" 3 android:layout_width="fill_parent" 4 android:layout_height="wrap_content" 5 android:text="Click" 6 />
添加后main.xml文件的内容为:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" > 6 7 <TextView 8 android:layout_width="fill_parent" 9 android:layout_height="wrap_content" 10 android:text="@string/hello" /> 11 12 <Button 13 android:id="@+id/button" 14 android:layout_width="fill_parent" 15 android:layout_height="wrap_content" 16 android:text="Click" 17 /> 18 19 </LinearLayout>
接着修改MyButtonActivity.java文件,在MyButtonActivity类里声明一个Button对象mButton
private Button mButton = null;
在onCreate()函数里通过findViewById()函数实例化mButton
mButton = (Button)findViewById(R.id.button);
紧接着编写mButton的监听函数
mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub PlayToast("You Clicked Button"); } });
其中PlayToast()函数是通过Toast类用来显示"You Clicked Button"这串字符串的,比较简单,如下所示:
private void PlayToast(String s) { Toast toast = Toast.makeText(this, s , Toast.LENGTH_LONG); toast.show(); }
好了。下面是MyButtonActivity.java文件的完整内容:
1 package com.nan.button; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.View; 6 import android.widget.Button; 7 import android.widget.Toast; 8 9 10 11 public class MyButtonActivity extends Activity 12 { 13 private Button mButton = null; 14 15 16 /** Called when the activity is first created. */ 17 @Override 18 public void onCreate(Bundle savedInstanceState) 19 { 20 super.onCreate(savedInstanceState); 21 setContentView(R.layout.main); 22 23 mButton = (Button)findViewById(R.id.button); 24 mButton.setOnClickListener(new View.OnClickListener() 25 { 26 27 @Override 28 public void onClick(View v) 29 { 30 // TODO Auto-generated method stub 31 PlayToast("You Clicked Button"); 32 } 33 }); 34 35 } 36 37 38 private void PlayToast(String s) 39 { 40 Toast toast = Toast.makeText(this, s , Toast.LENGTH_LONG); 41 toast.show(); 42 } 43 44 }
运行程序,并点击按钮,效果如下:
相关文章推荐
- 基础Android应用开发 Button显示系统时间+TextView显示随机数+简单计算器
- 【Android开发基础】应用界面主题Theme使用方法
- 【Android 应用开发】 ActionBar 基础
- Android应用开发基础篇(7)-----BroadcastReceiver
- Android基础教程之简单的Button事件响应综合提示控件Toast的应用
- Android官方开发指南-应用基础(Application Fundamentals)
- Android应用开发基础篇(2)-----Notification(状态栏通知)
- 【Android开发基础】应用界面主题Theme使用方法
- 【Android 应用开发】 ActionBar 基础
- Android应用开发基础篇(5)-----Handler与多线程
- android应用基础开发
- android应用开发 button显示两行文本
- Android应用开发基础之六:页面跳转和数据传递
- Android应用开发基础篇(3)-----ListView
- 精品Android讲堂--“面向移动终端的应用开发前景及android开发基础实例”[
- 【Android 应用开发】 ActionBar 基础
- Android应用开发基础篇(2)-----Notification(状态栏通知)
- Android中的蓝牙Bluetooth应用开发基础
- Android应用开发基础篇(10)-----Menu(菜单)
- Android应用开发笔记(10):制作自定义背景Button按钮、自定义形状Button的全攻略