用户界面部分学起来还真是无处下手哇,总不能一个控件发一篇文吧,略有点费时间啊。。。这个难道不是边用边学才给力吗。。所以我打算从最实用的Button开始下手。

  先贴几个链接,好东西:

  android用户界面的详尽教程实例系列:

  http://www.cnblogs.com/aimeng/archive/2012/06/26/2563762.html

  android用户界面教程实例汇总:

  http://www.cnblogs.com/aimeng/archive/2012/06/25/2560905.html

  本文主要内容是Button,相关的链接:

  一个强大的学习贴:

  http://www.apkbus.com/android-48448-1-1.html

  官方文档资料:

  http://developer.android.com/reference/android/widget/Button.html

  http://developer.android.com/guide/topics/ui/controls/button.html

Button基本使用方法

  首先,添加Button控件到XML布局文件中。也可通过程序添加。

  在布局文件中设置按钮的一些属性,如位置,宽高,按钮上的字,颜色等。

  比较重要的是要给按钮一个id号,这是按钮唯一的名字。

  这样在程序中可以通过如下形式获得按钮:

  button = (Button)findViewById(R.id.buttonId);

处理按钮点击

  按钮点击有两种处理方法。

  第一种是通过onClick属性,通过这个属性设置处理点击事件的方法名,在Activity中实现这个方法。

  另一种方法是典型的事件监听机制的应用形式,下面详细说明这两种方法。

1.通过onClick属性设置处理方法

  在XML布局文件中设置Button的属性:

  android:onClick="yourMethodName"

  然后在该布局文件对应的Acitivity中实现该方法:

  /** Called when the user touches the button */

  public void yourMethodName(View view)

  {

       // Do something in response to button click

  }

  需要注意的是这个方法必须符合三个条件:

  1.public

  2.返回void

  3.只有一个参数View,这个View就是被点击的这个控件。

2.使用setOnClickListener添加监听器对象

  关于事件监听模式,参见

  http://www.cnblogs.com/mengdd/archive/2012/09/08/2676587.html

  可以写一个内部类,实现OnClickListener接口,在这个类中实现onClick方法,方法里面写在按钮点击时想做的具体工作。

  将这个内部类的对象传入按钮的setOnClickListener方法中,即完成监听器对象和按钮的绑定(在事件源Button上注册了事件监听器),这时候只要按钮被点击,那么监听器对象的onClick方法就会被调用。

  当然这里也不一定要自己写一个内部类出来,比如这个例子

  Button button = (Button) findViewById(R.id.button_send);

  button.setOnClickListener(new View.OnClickListener() {

      public void onClick(View v) 

   {

          // Do something in response to button click

      }

  });

按钮基本操作实例

  奉上实例一个:

  XML布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="@dimen/padding_medium"
android:text="@string/hello_world"
tools:context=".ButtonActivity" /> <Button
android:id="@+id/button_first"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/buttonText"
android:onClick="changeButtonColor"
>
</Button> <Button
android:id="@+id/button_second"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/buttonText2"
android:layout_below="@id/button_first" >
</Button> </RelativeLayout>

  Activity代码

package com.example.buttontest;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class ButtonActivity extends Activity
{
private Button button01 = null;
private Button button02 = null; @Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button); button01 = (Button)findViewById(R.id.button_first);
button02 = (Button)findViewById(R.id.button_second); //绑定事件源和监听器对象
button02.setOnClickListener(new MyButtonListener());
} @Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.activity_button, menu);
return true;
} //按钮1的点击事件
public void changeButtonColor(View view)
{
button01.setBackgroundColor(getResources().getColor(R.color.red)); } //内部类,实现OnClickListener接口
//作为第二个按钮的监听器类
class MyButtonListener implements OnClickListener
{ public void onClick(View v)
{
button02.setBackgroundColor(getResources().getColor(R.color.blue)); } } }

  运行截图

  点击前:

  点击后:

  相关的资源文件中内容

  strings.xml

<resources>

    <string name="app_name">ButtonTest</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_button">ButtonActivity</string>
<string name="buttonText">ThisIsAButton</string>
<string name="buttonText2">ThisIsAnotherButton</string>
</resources>

  colors.xml

<?xml version="1.0" encoding="UTF-8"?>
<resources>
<color name="red">#f00</color>
<color name="green">#0f0</color>
<color name="blue">#00f</color>
<color name="black">#000</color>
</resources>

转自:链接

android 教程实例系列的更多相关文章

  1. Android教程2020 - RecyclerView使用入门

    本文介绍RecyclerView的使用入门.这里给出一种比较常见的使用方式. Android教程2020 - 系列总览 本文链接 想必读者朋友对列表的表现形式已经不再陌生.手机上有联系人列表,文件列表 ...

  2. Android教程2020 - RecyclerView响应点击

    本文介绍RecyclerView设置点击的方法.这里给出比较常见的使用方式. Android教程2020 - 系列总览 本文链接 前面我们已经知道如何用RecyclerView显示一列数据. 用户点击 ...

  3. Android教程2020 - RecyclerView实际使用

    示例,用RecyclerView的item做出一个列表. Android教程2020 - 系列总览 本文链接 前面我们已经知道如何用RecyclerView显示一列数据.这里我们做出一个具体的例子.尽 ...

  4. Android教程2020 - RecyclerView显示多种item

    Android教程2020 - 系列总览 本文链接 前面我们已经用RecyclerView显示一些数据.也知道如何获取滑动的距离. 前面我们的列表中显示的都是同类数据.如果要在一个列表中显示不同类别的 ...

  5. Android教程2020 - RecyclerView获取滑动距离

    获取RecyclerView滑动的距离. Android教程2020 - 系列总览 本文链接 前面我们已经用RecyclerView显示一些数据. 本文演示如何获取RecyclerView的滑动距离. ...

  6. Android图像处理实例教程

    Android图像处理实例教程 原始出处 http://vaero.blog.51cto.com/4350852/856750

  7. android用户界面详尽教程实例

    android用户界面详尽教程实例 1.android用户界面之AlarmManager教程实例汇总http://www.apkbus.com/android-48405-1-1.html2.andr ...

  8. android用户界面的教程实例---转自qianqianlianmeng的博客

    1.android用户界面之AlarmManager教程实例汇总http://www.apkbus.com/android-48405-1-1.html2.android用户界面之文本编辑教程实例汇总 ...

  9. Android控件系列之CheckBox

    学习目的: 1.掌握在Android中如何建立CheckBox 2.掌握CheckBox的常用属性 3.掌握CheckBox选中状态变换的事件(监听器) CheckBox简介: CheckBox和Bu ...

随机推荐

  1. 使用自定义tld标签简化jsp的繁琐操作

    最近做一个树形结构的展示,请求目标页面后,后台只返回简单的List,虽然有想过在jsp页面内做一些操作简化,但是太繁琐了,其他的标签又不能满足需求,所以只能自己做一个.使用tld标签可以简化jsp代码 ...

  2. 移动端浏览器和微信浏览器上禁止body的滚动条

    一般禁止body滚动的做法就是设置overflow:hidden. 但是很奇怪的发现在移动端浏览器和微信浏览器上这个不起作用,然后我分析了我的写法,就是在body上加了一个class去定义属性,然后改 ...

  3. Promise deeply lookup

    Motivation Consider the following synchronous JavaScript function to read a file and parse it as JSO ...

  4. MYSQL开启慢查询日志实施

    查看当前服务器是否开启慢查询:1.快速办法,运行sql语句show VARIABLES like "%slow%" 2.直接去my.conf中查看.my.conf中的配置(放在[m ...

  5. Android 签名证书

    Android APK的数字签名的作用和意义 http://blog.csdn.net/gaomatrix/article/details/6568191 http://jingyan.baidu.c ...

  6. java高级特性

    第二章 XML(可扩展标记语言) XML格式能够表达层次结构,并且重复的元素不会被曲解. 与HTML不同,XML是大小写敏感的. 必须有结束符. 属性值必须用引号括起来. 所有属性必须都有属性值. X ...

  7. java8入门 错误:找不到或者无法加载主类

    如果你也遇上的这个问题,但是如果你的Java版本不是6以上,这个解决方案可能就不适合你... 最近在跟着李兴华老湿的视频<<编程开发入门Java 8>>的学习Java... 但 ...

  8. VTK初学一,比较常见的错误1

      错误原因: 通常是在文件头部没有初始化 #ifndef INITIAL_OPENGL #define INITIAL_OPENGL #include <vtkAutoInit.h> V ...

  9. [Java] Java执行Shell命令

    Methods ProcessBuilder.start() 和 Runtime.exec() 方法都被用来创建一个操作系统进程(执行命令行操作),并返回 Process 子类的一个实例,该实例可用来 ...

  10. Floyed判环/龟兔算法

    求[(5+2√6)2^x+1 ] mod p 的值,其中 0 ≤ x < 232 , p 是个质数,p ≤ 46337 .(这里介绍的是一种暴力的做法) (5+2√6)2^n+1 = an + ...