Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 538|回复: 0

[默认分类] Android基础控件之Button的基本使用

[复制链接]
  • TA的每日心情
    开心
    2021-12-13 21:45
  • 签到天数: 15 天

    [LV.4]偶尔看看III

    发表于 2018-6-2 10:40:39 | 显示全部楼层 |阅读模式
    Button基础
      
      用户界面部分学起来还真是无处下手哇,总不能一个控件发一篇文吧,略有点费时间啊。。。这个难道不是边用边学才给力吗。。所以我打算从最实用的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中实现该方法:
      

    1.   /** Called when the user touches the button */
    2.   public void yourMethodName(View view)
    3.   {
    4.        // Do something in response to button click
    5.   }
    复制代码


      
      需要注意的是这个方法必须符合三个条件:
      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方法就会被调用。
      当然这里也不一定要自己写一个内部类出来,比如这个例子
      

    1.   Button button = (Button) findViewById(R.id.button_send);
    2.   button.setOnClickListener(new View.OnClickListener() {
    3.       public void onClick(View v)
    4.    {
    5.           // Do something in response to button click
    6.       }
    7.   });
    复制代码


      
      
    按钮基本操作实例
      奉上实例一个:
      XML布局文件



    XML布局文件  
      
    1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2.     xmlns:tools="http://schemas.android.com/tools"
    3.     android:layout_width="match_parent"
    4.     android:layout_height="match_parent" >
    5.     <TextView
    6.         android:layout_width="wrap_content"
    7.         android:layout_height="wrap_content"
    8.         android:layout_centerHorizontal="true"
    9.         android:layout_centerVertical="true"
    10.         android:padding="@dimen/padding_medium"
    11.         android:text="@string/hello_world"
    12.         tools:context=".ButtonActivity" />
    13.     <Button
    14.         android:id="@+id/button_first"
    15.         android:layout_height="wrap_content"
    16.          android:layout_width="wrap_content"
    17.          android:text="@string/buttonText"
    18.          android:onClick="changeButtonColor"
    19.         >      
    20.     </Button>   
    21.    
    22.     <Button
    23.         android:id="@+id/button_second"
    24.         android:layout_height="wrap_content"
    25.          android:layout_width="wrap_content"
    26.          android:text="@string/buttonText2"
    27.          android:layout_below="@id/button_first"
    28.          
    29.         >      
    30.     </Button>
    31. </RelativeLayout>
    复制代码

      

      
      Activity代码



    Activity代码
      
    1. package com.example.buttontest;
    2. import android.app.Activity;
    3. import android.os.Bundle;
    4. import android.view.Menu;
    5. import android.view.View;
    6. import android.view.View.OnClickListener;
    7. import android.widget.Button;
    8. public class ButtonActivity extends Activity
    9. {
    10.     private Button button01 = null;
    11.     private Button button02 = null;
    12.     @Override
    13.     public void onCreate(Bundle savedInstanceState)
    14.     {
    15.         super.onCreate(savedInstanceState);
    16.         setContentView(R.layout.activity_button);
    17.         
    18.         button01 = (Button)findViewById(R.id.button_first);
    19.         button02 = (Button)findViewById(R.id.button_second);
    20.       
    21.         //绑定事件源和监听器对象
    22.         button02.setOnClickListener(new MyButtonListener());
    23.     }
    24.     @Override
    25.     public boolean onCreateOptionsMenu(Menu menu)
    26.     {
    27.         getMenuInflater().inflate(R.menu.activity_button, menu);
    28.         return true;
    29.     }
    30.    
    31.     //按钮1的点击事件
    32.     public void changeButtonColor(View view)
    33.     {
    34.         button01.setBackgroundColor(getResources().getColor(R.color.red));
    35.         
    36.     }
    37.    
    38.     //内部类,实现OnClickListener接口
    39.     //作为第二个按钮的监听器类
    40.     class MyButtonListener implements OnClickListener
    41.     {
    42.         public void onClick(View v)
    43.         {
    44.             button02.setBackgroundColor(getResources().getColor(R.color.blue));
    45.             
    46.         }
    47.         
    48.         
    49.     }
    50.    
    51. }
    复制代码

      

      
      运行截图                        
      点击前:
      

      
      
      点击后:
      

      
      
      相关的资源文件中内容
      strings.xml



    strings.xml
      
    1. <resources>
    2.     <string name="app_name">ButtonTest</string>
    3.     <string name="hello_world">Hello world!</string>
    4.     <string name="menu_settings">Settings</string>
    5.     <string name="title_activity_button">ButtonActivity</string>
    6.     <string name="buttonText">ThisIsAButton</string>
    7.     <string name="buttonText2">ThisIsAnotherButton</string>
    8. </resources>
    复制代码

      

      
      colors.xml



    colors.xml
      
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <resources>
    3.     <color name="red">#f00</color>
    4.     <color name="green">#0f0</color>
    5.     <color name="blue">#00f</color>
    6.     <color name="black">#000</color>
    7. </resources>
    复制代码

      

      
      
      
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-24 11:02 , Processed in 0.381288 second(s), 35 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表