android之创建button

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情
实现的功能:  
                    运行程序,弹出ok和exit两个按钮,
                    点击ok按钮,标题出现this is ok,
                    点击exit按钮,退出程序.
一:
实现如下:
             
  @Override public void onCreate(Bundle icicle){
                    super.onCreate(icicle);
                    setContentView(R.layout.main);              //创建主界面并显示,读取main.xml
                    Button  btn = (Button)findViewById(R.id.button);  //定义button
                    Button btn2=(Button)findViewById(R.id.button2);
                    btn.setOnClickListener(listener);                        //设置监听
                    btn2.setOnClickListener(listener2);  
                    //这指明將btn 按钮的事件传递给listener 物件的onClick()函数处?。
                    }
    OnClickListener listener = new OnClickListener(){
                              public void onClick(View v){   
                                                steTitle("this is ok");
                                                 }
                              };
     OnClickListener listener2 = new OnClickListener(){
                               public void onClick(View v){
                                                finish();
                                                }
                              };



二:使用图片button实现:

@Override public void onCreate(Bundle icicle) {
                  super.onCreate(icicle);
                  setContentView(R.layout.main);
                  ImageButton btn = (ImageButton)findViewById(R.id.button);    //初始化按钮与xml文件关联
                  ImageButton btn2 = (ImageButton)findViewById(R.id.button2);
                  btn.setOnClickListener(listener);
                  btn2.setOnClickListener(listener2);
                }
OnClickListener listener = new OnClickListener() {
                  public void onClick(View v) {
                  setTitle("this is OK button");
                  }
         };
OnClickListener listener2 = new OnClickListener() {
                  public void onClick(View v)
                  { finish(); }
         };


注意此文件与main.xml文件的关联:


    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />

    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text=""
    />

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    
    android:src="@drawable/video2"
    />

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/video"
    />
    
Android Button控件的使用详解(系列教程二) Android Button控件的使用详解(Android系列教程二) 按钮的常用使用方式和技巧。 阅读详情

相关推荐

Android 动态添加Button(1)

这是一个非常简单的动态添加Button的例子:   package com.kkkk.llll; import android.app.Activity; import android.os.Bundle; import android.widget.Button; import android.widget.LinearLayout; public class AddButton ...

u011555996的博客 7123

Android应用开发(7)按键(Button

本节介绍按钮控件的常见用法,包括:按钮控件大小写属性与文本属性,如何响应点击事件和长按事件,如何禁用和启用按钮等。

zsyf33078的专栏 1万+

android 动态创建带图标的Button

用XML创建: <Button android:id="@+id/btn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="test button" android:drawableLeft="@drawable/items1" android:onC

xuhuan_wh的专栏 1657

Android:动态创建button

Button bt1 = new Button(allContactsActivity.this); bt1.setText("新按钮"); ll_tier.addView(bt1); ll_tier是LinearLayout布局,已经在xml里面画好位置了

谢桥的博客 1270

[Android] 013_利用for循环创建100个Button控件并添加监听器

Android_013_利用for循环创建100个Button控件并给其添加监听器   有些时候, 在一个Activity中可能会需要大量的类型相同的控件, 比如果需要显示100 按钮, 难道我们真的就要布局文件中写100个按钮的代码吗? 其实可以在java 源文件中利用for 循环就可以一次创建100 个按钮了. 1. 先在布局文件中添加一个<ScrollView>&l...

bamian4814的博客 1545

Android创建圆形或圆角按钮Button 真菜鸟食用

Android创建圆形或圆角按钮Button 真菜鸟食用 1.在res\drawable下创建.xml文件(我起名为round_button.xml) 你可以右击res新建Android资源文件,将Resource type改为Drawable,将Root element改为shape来创建round_button.xml 也可以右击drawable文件创建Drawable资源文件直接创建 2.编辑.xml文件绘制圆形、圆角图形 圆形: <?xml version="1.0" encoding=

佳慧的博客 3567

android创建平移的button

TranslateAnimation animation=new TranslateAnimation(-100,100,0,0); //设置偏移量 animation.setDuration(2000); //时间周期 animation.setRepeatCount(1000); //重复次数 Button button_down = (B

小明的博客 2716

Android开发探秘之一:创建可以点击的Button

感觉到自己有必要学习下手机开发方面的知识,不论是为了以后的工作需求还是目前的公司项目。 当然,任何新东西的开始,必然伴随着第一个HelloWorld,Android学习也不例外。既然才开始,我就不做过多的描述了。 对于Android开发的IDE:ADT来说,打开的第一眼有点迷糊,不过看了网上各种目录结构的介绍,慢慢的就明白了,做这个实例,我们尤其需要关注两个地方,一个是src目录,一个就是res目...

weixin_33924312的博客 130

Android 开发学习笔记---创建button、使用Toast以及创建菜单

一、运行一个Android程序步骤 1.需要创建活动 目录:Project/app/src/main/java/com.example.activitytest/MainActivity(手动创建) 2.创建活动和加载布局 3.在AndroidManifest.xml中注册 MainActivity.java代码: package com.example.firstactivity; impor...

Gopuwe的博客 271

Android Code_C1_创建自己的第一个BUTTON APP

使用Android Stduio空项目创建自己的第一个BUTTON APP

m0_66701835的博客 849

Android中,Button不会在R中自动创建id

用的MyEclipse 8.5和ADT 平台开发 在layout文件夹中的main.xml文件中,在Graphical Layout视图下,拖进去一个button,修改了Button的id后,但是在R.java文件中并没有显示这个Button的ID,之后想要在mainactivity中声明一个Button button = (Button) this.findViewById(R.id.butt

Cinway的专栏 2299

Android 笔记之动态创建button 一行三列

package com.example.mytest2; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.Menu; import android.vi

yungcs的专栏 2234

Button创建、监听与继承

目录简介Button创建使用XML布局创建使用Java代码创建Button的常用属性Button的监听OnClickListenerOnLongClickListener设置监听器的方式Button的继承 简介  Button继承自TextView。Button提供的基本响应有:单击(Click)和长按(LongClick)。可以通过触摸事件管理实现自定义响应,例如双击、滑动等。 Button的...

Lishaoyin 1258

AAPT: error: resource drawable/soil_backgroud_content3 (aka cn.bloghut:drawable/soil_backgroud_conte

AAPT: error: resource drawable/soil_backgroud_content3 (aka cn.bloghut:drawable/soil_backgroud_content3) not found.

闲言 460

安卓之使用标签创建按钮

安卓很灵活因为他的内核是linux的,又有C++的库,应用层还是java完成。导致实现同一个功能可以由java,html,C#,C++4种方法实现。这次分享标签实现。 首先进入项目找到res(resource资源)文件夹,在这个文件夹里面找到layout(布局)文件夹,在这里我们可以看到你的活动界面的主xml。点击这个xml进入代码界面在布局标签里面(默认RelativeLayout或者Linea...

GodGump的博客 366

Android开发 之 基本按钮Button

原文地址链接:http://www.apkbus.com/android-48436-1-1.html 感谢作者:tijing! 本文向你展示了在你的Android应用程序中创建一个简单的Button或ImageButton控件的步骤。首先,你会学到如何向你的布局文件中添加按钮控件。然后你会学习如何用两种方法处理用户对按钮的点击。最后,我们讨论Android中按钮控

CodingMan 2701

java动态生成button,java – 如何在Android中动态创建Button

创建/删除按钮onClick按钮和 – 按钮如下:public void onClick(View v) {switch(v.getId()){case (R.id.plusbutton):Button myButton = new Button(this);myButton.setText("Add Me");LinearLayout ll = (LinearLayout)findViewByI...

weixin_42561143的博客 369

Android checkbox 自定义点击效果

安卓默认的效果 自定义后的效果 前面的图片当然可以自己修改。 实现这个效果的步骤如下 1.建立 一个selector 的xml <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.andro...

dizha5554的博客 467
上一篇: final的作用
下一篇: Intent 和 Intent Filter
prince_amir
博客等级 码龄16年 14粉丝 116原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值