您的位置:首页 > 移动开发 > Android开发

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

2014-09-02 19:31 295 查看
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代码  


<Button  
    android:id="@+id/btn1"  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:text="这是一个Button组件"  
    android:textColor="#FDF5E6"  
    android:textSize="16dp" />  

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



    再来看第二个Button示例:

Xml代码  


<Button  
    android:id="@+id/btn2"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:layout_gravity="center"  
    android:layout_marginTop="20dp"  
    android:text="点击访问: http://www.google.com.hk"  
    android:textSize="10dp" />  

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



    可以看到最终的效果就是上边距为20dp,而左右边距没有变化,仅仅是包裹文字的宽度来显示的。

    下面再来看一个示例:

Xml代码  


    <Button  
        andr  
oid:id="@+id/btn3"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:layout_gravity="center"  
        android:layout_margin="15dp"  
        android:maxLength="14"  
        android:text="Powered By Nan Lei"  
        android:textSize="12dp" />  

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



    要注意的是这里如果设置layout_width为wrap_content,那么就不会得到左右边距15dp的效果,而仅仅是包裹文字了。具体所需的显示效果,我们可以根据具体需求去详细调整。

   从上面三个例子中可以很容易看出,Button组件的操作和TextView是基本一致的。最后我们来看看在程序中动态生成按钮,代码如下:

Java代码  


package org.ourpioneer;  
import android.app.Activity;  
import android.os.Bundle;  
import android.widget.Button;  
import android.widget.LinearLayout;  
public class ButtonDemoActivity extends Activity {  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        super.setContentView(R.layout.main);  
        LinearLayout layout = (LinearLayout) super.findViewById(R.id.layout);  
        Button btn = new Button(this);  
        btn.setText("我是在程序中动态创建的按钮");  
        layout.addView(btn);  
    }  
}  

    程序代码很简单,但是要说一点就是,我们通过findViewById()方法获取布局管理器时,要在XML中为该布局管理器设置id,也就是说,在main.xml中,我们要修改代码:

Xml代码  


<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:id="@+id/layout"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    android:orientation="vertical" >  
......  
</LinearLayout>  

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



    至此,我们已经看到了最后的显示效果,我们把一个Button动态的添加到了布局管理器中,此时来看看R.java中都有什么:

Java代码  


package org.ourpioneer;  
public final class R {  
    public static final class attr {  
    }  
    public static final class drawable {  
        public static final int ic_launcher=0x7f020000;  
    }  
    public static final class id {  
        public static final int btn1=0x7f050001;  
        public static final int btn2=0x7f050002;  
        public static final int btn3=0x7f050003;  
        public static final int layout=0x7f050000;  
    }  
    public static final class layout {  
        public static final int main=0x7f030000;  
    }  
    public static final class string {  
        public static final int app_name=0x7f040001;  
        public static final int hello=0x7f040000;  
    }  
}  

    可以看到我们为布局管理器加的ID也显示出来了。

    最后,我们将该程序安装到Android设备上来具体看看效果(运行设备为Motorola Defy+ Android 2.3.7 MIUI):



    这部分的演示代码请参考附件。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: