android Button基础

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情

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

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代码

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

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

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>
复制代码

 

 

安卓程序设计之基础界面设计button,textview以及线下布局,相对布局等使用 今天我们来学习一些安卓程序设计里的一些基础界面设计,里面用到了线性布局,相对布局,还有一些基础控件button,textview的使用。 首先先上效果图 设计思路:这是一个不太美观但是包含了基本控件和布局的使用。首先我们利用权重1:1将界面分为俩部分,并设置合适边距。然后将上部分设置为线性布局,内容垂直分布,分为上下俩个内容,分别含有textview和相对布局,相对布局里含有一个textview... 阅读详情

相关推荐

中文版android教程

android中文学习资料,指导你搭建环境,进行开发,易学、

Android基础控件之Button的基本使用

Button基础     用户界面部分学起来还真是无处下手哇,总不能一个控件发一篇文吧,略有点费时间啊。。。这个难道不是边用边学才给力吗。。所以我打算从最实用的Button开始下手。     先贴几个链接,好东西:   android用户界面的详尽教程实例系列:   http://www.cnblogs.com/aimeng/archive/2012/06/26/2563762.htm...

weixin_34204057的博客 677

Android-AhMythAndroidRat一个Android远程管理工具

AhMyth Android Rat一个Android远程管理工具

Android开发Button控件使用详解

Android Button点击效果、button控件、Button两种设置方式、button按钮带圆角、自定义button按钮

GRT609的博客 4235

文章标题:Android常见控件Button的使用方法

Button是程序用于和用户进行交互的一个重要控件,如果想要在界面上显示这个控件,我们也必须要在XML 文件中调加控件的资源,具体的代码如下: <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_cont

Violence 544

Android动态生成Relativelayout + button

要求:输入每行btn个数,输出一共200个btn。 直接上代码,参考了csdn上的博客 android:动态创建多个按钮 不需要任何XML文件即可显示出btn。 import androidx.appcompat.app.AppCompatActivity; import android.content.Context; import android.os.Bundle; import ...

smebkoo70的博客 924

Android动态操作RelativeLayout里面的Button

1.关于RelativeLayout设置layout_height = "wrap_content"的时候,Button动态设置的RelativeLayout.ALIGN_PARRENT_BOTTOM会撑破容器; 2.如果设置一个初始在XML文件中ID为iniBut的Button的属性Visially="gone"的时候,动态添加的button以它为参考,设置(RelativeLayout.AL...

weixin_30807779的博客 477

Android安卓开发基础-Button与ImageButton

介绍安卓控件Button及ImageButton在java文件及xml文件中的使用(主要以Button为例)

weixin_53743049的博客 1166

Android设置text按钮,安卓基础控件使用(TextView、Button、ImageView、EditText)

一、文本控件TextView1.布局文件android:text="@string/content"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="@color/green"android:textSize="@dimen/title"android:lines="1...

weixin_30127493的博客 2630

Android开发基础之按钮Button

        一、基础属性 1、layout_width 宽度 2、layout_height 高度 3、id 设置组件id 4、text 设置显示的内容 5、textColor 设置字体颜色 6、textStyle 设置字体风格:normal(无效果)、bold(加粗)、italic(斜体) 7、textSize 字体大小,单位常用sp 8、background 控件背景颜色 9、gravity 设置字体对齐方向 &

WYuan的博客 3404

Android控件与布局——基础控件Button

最近在用原生的控件和布局绘制一些界面并使用,虽然这些都是Android基本知识,但是有的时候真的感觉力不从心,感觉有必要对Android常用的控件和布局做一个系统的了解。后续一个月甚至更多的时间都会围绕这个主题展开,毕竟这里面还是有不少高级控件的,我也会尽量结合应用深入的进行了解。 项目GitHub地址入口 下一篇:ToggleButton 今天,我们的主题是基础控件Butt...

hfut_why的博客 4019

Android基础button imageview

Java技术博客,不再更新Android内容 460

Android Button基础

Button基础     用户界面部分学起来还真是无处下手哇,总不能一个控件发一篇文吧,略有点费时间啊。。。这个难道不是边用边学才给力吗。。所以我打算从最实用的Button开始下手。     先贴几个链接,好东西:   android用户界面的详尽教程实例系列:   http://www.cnblogs.com/aimeng/archive/2012/06/26

codepython的专栏 430

安卓基础控件(EditText、Button)以及布局(约束布局)的介绍

目录一、EditText代码展示二、Button代码展示三、应用例子(1)布局代码展示(2)方法代码例子(3)功能实现第一步第二步(4)变量(补充知识点)四、约束布局 一、EditText EditText是允许用户输入和编辑内容的文本框,并且可以在程序中对这些内容进行处理。EditText的应用场景非常普遍,在网页搜索、发微信聊天、登陆页面输入账号等操作中必然要用到EditText。例如: 代码展示 那么就让我们看看如何在界面上加入EditText。 第一步:修改activity_main.xml 中的

qq_51669241的博客 2809

android基础 button 按钮

Button自定义背景颜色的形状 1,先在res中的drawable新建的 Drawable Resource File 新建 Root Element 的shape文件 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid ..

红尘紫陌博客 284

Android基础Button按钮

Button

在路上 6364

安卓开发基础 —TextView、button和EditText

安卓开发基础 —TextView、button和EditText 布局里面有个button,所以我们要在外面声明这个button。 关闭当前的文件夹,重新创建一个工程,选择Java语言,(不使用kotlin) id:为TextView设置一个组件id,根据id,我们可以在Java代码中通过findViewById()的方法获取到该对象,然后进行相关属性的设置,又或者使用RelativeLayout时,参考组件用的也是id! layout_width:组件的宽度,一般写:wrap_conte

cs_loser的博客 1090
上一篇: c++程序改写成C#代码
下一篇: java模拟键盘按键
zm_21
博客等级 码龄14年 147粉丝 73原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值