发布于 2016-03-10 21:55:01 | 104 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这篇文章主要介绍了Android编程之Button控件用法,较为详细的分析了Button控件的功能、定义及相关使用注意事项,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了Android编程之Button控件用法。分享给大家供大家参考,具体如下:

一、Button概述

android.widget.Button直接继承于android.wdiget.TextView.

直接子类有:CompoundButton.

间接子类有:CheckBox,RadioButton,Switch,ToggleButton.

Button类表示一个“按钮”控件。“按钮”控件可以被用户按下或者点击,来触发另一个操作。

二、Button的用法

一种典型的用法:


public class MyActivity extends Activity {
   protected void onCreate(Bundle icicle) {
     super.onCreate(icicle);
     setContentView(R.layout.content_layout_id);
     final Button button = (Button) findViewById(R.id.button_id);
     button.setOnClickListener(new View.OnClickListener() {
       public void onClick(View v) {
         // Perform action on click
       }
     });
   }
}

除了直接在Activity类中为Button绑定OnClickListener之外,还可以在XML文件中通过android:onClick属性来绑定触发的方法。

举个例子:


<Button 
   android:layout_height="wrap_content" 
   android:layout_width="wrap_content" 
   android:text="@string/self_destruct" 
   android:onClick="selfDestruct" />

现在,当用户按下这个按钮,系统会调用Activity中的selfDestruct(View)方法,为了让这个方法有效,方法必须为public且只能接受一个View参数。当方法被调用时,被点击的那个控件会作为View类型的参数传入selfDestruct(View)方法中。如:


public void selfDestruct(View view) { 
   // Kabloey 
} 

三、XML属性

Button的XML属性基本与TextView一致。感兴趣的朋友可参看本站相关文档。

四、常用public方法

Button的常用public方法基本与TextView一致。

希望本文所述对大家Android程序设计有所帮助。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务