Android学习笔记五:基本视图组件:Button

来源:互联网 发布:淘宝店店铺介绍范文 编辑:程序博客网 时间:2024/06/18 13:55
Button组件也是我们前面看到过的一个简单组件,这里我们来进行深入的介绍。按钮的基本功能就是供用户点击,然后产生一些效果,比如Web开发中的登录按钮,点击之后即可实现登录效果。
    这里我们没有对Button的事件处理操作,还是简单的了解Button的配置。首先来看一下Button的文档:

java.lang.Object
   ↳ android.view.View
   ↳ android.widget.TextView
   ↳ android.widget.Button

   可以看到Button类是TextView类的子类,而不是直接继承自View类的。可以说Button是一个特殊的TextView,下面在Eclipse中新建一个项目来演示Button组件:
Xml代码  收藏代码
  1. <Button  
  2.     android:id="@+id/btn1"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:text="这是一个Button组件"  
  6.     android:textColor="#FDF5E6"  
  7.     android:textSize="16dp" />  

    可以看出,Button组件的配置和TextView是极其类似的,因为从类的继承结构上就不难得出这个结论。这里我们也是设置了按钮组件显示的文本,文本的颜色以及文本的大小,下面运行程序来看一下具体效果:

    再来看第二个Button示例:
Xml代码  收藏代码
  1. <Button  
  2.     android:id="@+id/btn2"  
  3.     android:layout_width="wrap_content"  
  4.     android:layout_height="wrap_content"  
  5.     android:layout_gravity="center"  
  6.     android:layout_marginTop="20dp"  
  7.     android:text="点击访问: http://www.google.com.hk"  
  8.     android:textSize="10dp" />  

    这个Button组件中,我们设置layout_width为wrap_content,也就是包裹文字大小,而不是充满屏幕。同时设置了layout_gravity属性为center,也就是居中显示,而layout_marginTop的意思就是距离上个组件的上边距为20dp,这和CSS中的概念也是一致的。重新设置Button的显示文本和文字大小,再次运行程序,这次在横屏下显示,来看一下效果:

    可以看到最终的效果就是上边距为20dp,而左右边距没有变化,仅仅是包裹文字的宽度来显示的。
    下面再来看一个示例:
Xml代码  收藏代码
  1.     <Button  
  2.         andr  
  3. oid:id="@+id/btn3"  
  4.         android:layout_width="fill_parent"  
  5.         android:layout_height="wrap_content"  
  6.         android:layout_gravity="center"  
  7.         android:layout_margin="15dp"  
  8.         android:maxLength="14"  
  9.         android:text="Powered By Nan Lei"  
  10.         android:textSize="12dp" />  

    这里我们调整了layout_width为fill_parent,而设置layout_margin为15dp,也就是上下左右边距都为15dp,再设置最大的显示长度为14个字符,之后运行程序,我们可以看到如下的显示效果:

    要注意的是这里如果设置layout_width为wrap_content,那么就不会得到左右边距15dp的效果,而仅仅是包裹文字了。具体所需的显示效果,我们可以根据具体需求去详细调整。
   从上面三个例子中可以很容易看出,Button组件的操作和TextView是基本一致的。最后我们来看看在程序中动态生成按钮,代码如下:
Java代码  收藏代码
  1. package org.ourpioneer;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. import android.widget.Button;  
  5. import android.widget.LinearLayout;  
  6. public class ButtonDemoActivity extends Activity {  
  7.     @Override  
  8.     public void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         super.setContentView(R.layout.main);  
  11.         LinearLayout layout = (LinearLayout) super.findViewById(R.id.layout);  
  12.         Button btn = new Button(this);  
  13.         btn.setText("我是在程序中动态创建的按钮");  
  14.         layout.addView(btn);  
  15.     }  
  16. }  

    程序代码很简单,但是要说一点就是,我们通过findViewById()方法获取布局管理器时,要在XML中为该布局管理器设置id,也就是说,在main.xml中,我们要修改代码:
Xml代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/layout"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:orientation="vertical" >  
  7. ......  
  8. </LinearLayout>  

   这样,上面的程序代码就可以执行了,效果如下:

    至此,我们已经看到了最后的显示效果,我们把一个Button动态的添加到了布局管理器中,此时来看看R.java中都有什么:
Java代码  收藏代码
  1. package org.ourpioneer;  
  2. public final class R {  
  3.     public static final class attr {  
  4.     }  
  5.     public static final class drawable {  
  6.         public static final int ic_launcher=0x7f020000;  
  7.     }  
  8.     public static final class id {  
  9.         public static final int btn1=0x7f050001;  
  10.         public static final int btn2=0x7f050002;  
  11.         public static final int btn3=0x7f050003;  
  12.         public static final int layout=0x7f050000;  
  13.     }  
  14.     public static final class layout {  
  15.         public static final int main=0x7f030000;  
  16.     }  
  17.     public static final class string {  
  18.         public static final int app_name=0x7f040001;  
  19.         public static final int hello=0x7f040000;  
  20.     }  
  21. }  

    可以看到我们为布局管理器加的ID也显示出来了。
    最后,我们将该程序安装到Android设备上来具体看看效果(运行设备为Motorola Defy+ Android 2.3.7 MIUI):

    这部分的演示代码请参考附件。
0 0
原创粉丝点击