Android笔记(十四) Android中的基本组件——按钮
Android中的按钮主要包括Button和ImageButton两种,Button继承自TextView,而ImageButton继承自ImageView。Button生成的按钮上显示文字,而ImageButton上则显示图片。
主要功能是在UI界面上生成一个按钮,当用户点击这个按钮时,出发一个OnClick事件来执行某项任务。
简单示例
<TableLayout
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"
tools:context=".MainActivity"
android:orientation="horizontal"
> <TableRow>
<!-- 普通文字按钮 -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="普通按钮"
android:textSize="10pt"
/>
<!-- 普通图片按钮 -->
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/blue"
android:background="#000000"
/>
<!-- 图片背景文字按钮 -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="图片背景文字按钮"
android:background="@drawable/red"
android:textSize="10pt"
/>
</TableRow> </TableLayout>
运行结果:

一个小demo,要求界面显示一个文本一个数字文本,点击按钮,数字增加
MainActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView; public class ButtonActivity extends Activity { TextView tv = null;
String count = null;
int num = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button); tv = (TextView) findViewById(R.id.count);
count = (String) tv.getText();
num = Integer.parseInt(count); findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tv.setText((++num) + " ");
onRestart();
}
});
} @Override
protected void onRestart() {
super.onRestart();
Log.d("TEST",num + ""); }
}
activity_main.xml
<TableLayout 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"
tools:context="cn.lixyz.layoutdemo.ButtonActivity"> <!-- 普通文字按钮 -->
<TableRow>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/red"
android:text="普通按钮"
android:textSize="10pt"/> </TableRow>
<TextView
android:id="@+id/count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50pt"
android:textColor="#ff211c"
android:text="1"/> </TableLayout>
运行结果:

Android笔记(十四) Android中的基本组件——按钮的更多相关文章
- Android进阶(十四)Android Adapter详解
Android Adapter详解 Android是完全遵循MVC模式设计的框架,Activity是Controller,layout是View.因为layout五花八门,很多数据都不能直接绑定上去, ...
- 《C++游戏开发》笔记十四 平滑过渡的战争迷雾(二) 实现:真正的迷雾来了
本系列文章由七十一雾央编写,转载请注明出处. http://blog.csdn.net/u011371356/article/details/9712321 作者:七十一雾央 新浪微博:http:/ ...
- python3.4学习笔记(十四) 网络爬虫实例代码,抓取新浪爱彩双色球开奖数据实例
python3.4学习笔记(十四) 网络爬虫实例代码,抓取新浪爱彩双色球开奖数据实例 新浪爱彩双色球开奖数据URL:http://zst.aicai.com/ssq/openInfo/ 最终输出结果格 ...
- SLAM十四讲中Sophus库安装
Sophus截止目前有很多版本,其中大体分为两类,一种是用模板实现的方法,一种是用非模板类实现的,SLAM十四讲中使用的是非模板类库,clone Sophus: git clone http://gi ...
- Android笔记(六十四) android中的动画——补间动画(tweened animation)
补间动画就是只需要定义动画开始和结束的位置,动画中间的变化由系统去补齐. 补间动画由一下四种方式: 1.AplhaAnimation——透明度动画效果 2.ScaleAnimation ——缩放动画效 ...
- Android笔记(四十九) Android中的资源访问——asset
1.文件读取方式 AssetManager.open(String filename),返回的是一个InputSteam类型的字节流,这里的filename必须是文件,而不能是文件夹, ...
- Android笔记(四十八) Android中的资源访问——SDCard
访问存储在SD卡中的文件 使用 Environment.getExternalStorageState(); 判断是否存在内存卡 使用 Environment.getExternalStorageDi ...
- Android笔记(四十六) Android中的数据存储——XML(二)PULL解析
PULL 的工作原理: XML pull提供了开始元素和结束元素.当某个元素开始时,可以调用parser.nextText()从XML文档中提取所有字符数据.当解析到一个文档结束时,自动生成EndDo ...
- Android笔记(四十五) Android中的数据存储——XML(一)DOM解析器
DOM解析XML在j2ee开发中比较常见,在Dom解析的过程中,是先把dom全部文件读入到内存中,然后使用dom的api遍历所有数据,检索想要的数据,这种方式显然是一种比较消耗内存的方式,对于像手机这 ...
- Android笔记(四十二) Android中的数据存储——SQLite(四)update
update方法的四个参数: update()方法参数 对应的sql部分 描述 table update table_name 更新的表名 values set column=xxx ContentV ...
随机推荐
- 转 mysql 文件系统空间满了
#######################sample [OIP - 互联网开放平台]在2019-07-28 21:30:11发生10.194.42.19 - - Linux上的监控项[磁盘空间] ...
- 123457123456#0#----com.ppGame.ChengYu43--前拼后广--成语caicaicai_pp
com.ppGame.ChengYu43--前拼后广--成语caicaicai_pp
- iphone那些事儿
1.微信文件如何发送到钉钉? 打开微信的某文件,点击右上角三个点,选择其他应用打开,选择钉钉.即可复制文件到钉钉. 2.无限网地址 设置-通用-关于本机-无线局域网地址 锂离子电池,受气温影响,气温低 ...
- 【Leetcode_easy】985. Sum of Even Numbers After Queries
problem 985. Sum of Even Numbers After Queries class Solution { public: vector<int> sumEvenAft ...
- 【Leetcode_easy】897. Increasing Order Search Tree
problem 897. Increasing Order Search Tree 参考 1. Leetcode_easy_897. Increasing Order Search Tree; 完
- ready与load的区别
JQuery里有ready和load事件 $(document).ready(function() { // ...代码... }) //document ready 简写 $(function() ...
- Ubuntu18安装虚拟机virtualbox
环境查看 安装虚拟机 apt install virtualbox 图形化界面启动 virtualbox 其余创建虚拟机和安装和在Windows安装配置一样,不详述.
- INTEL 7代CPU I5 7500 集显HD630 WIN7 64位
HD630 在WIN7 下的硬件ID (在设备管理器 - 显卡 - 属性 中查看): PCI\VEN_8086&DEV_5912&SUBSYS_D0001458&REV_04 ...
- Django orm练习
ORM练习题 models生成 from django.db import models # Create your models here. # 书籍管理 class Book(models.Mod ...
- STM32 MDK摘记
题记:这人是越懒越懒,记性也也来越差,前段时间改了个链接文件,今天想用,竟然忘了咋写....还是勤记记吧... 随时更新,笔记帖. 不喜勿喷! 1,关于MDK链接文件宏的定义 #! armcc -E ...