来个Button看一看
0.目录
1.前言
2.基本属性与方法
3.点点更健康
4.我的Button有点多
5.震惊!TextView竟然...
1.前言
每次写代码总会忘记一些东西,又要重新Goooooooooogle,好烦呐~
本文参考网站(排名不分先后):
1.Android Button的基本使用
2.Android中设置文本颜色的三种方法
3.android:layout_gravity和android:gravity的区别
2.基本属性与方法
Button 支持的 XML 属性及相关方法:
| XML属性 | 相关方法 | 说明 |
|---|---|---|
| android:id | findViewById | 在XML中设置id,然后在.java中才可以调用这个按钮做其他事 |
| android:text | setText() | 设置文字 |
| android:textColor | setTextColor() | 设置文字颜色 |
| android:textSize | setTextSize() | 设置文字大小 |
| android:background | setBackground() | 设置背景颜色或者背景图片 |
| android:enabled | setEnabled() | 设置按钮是否可以被点击 |
| android:layout_gravity | 设置按钮的位置 | |
| android:gravity | 设置文字的位置 |
以下用实例来讲解:
XML文件为:
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.example.pylearn_01_1.MainActivity" >
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自古一楼没卵用" />
<Button
android:id="@+id/btn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="使用XML布局"
android:textColor="@android:color/white"
android:textSize="50sp" />
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/background_dark" />
<Button
android:id="@+id/btn5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="可远观而不可亵玩"
android:enabled="false" />
<Button
android:id="@+id/btn6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="使用layout_gravity让按钮居中"
android:layout_gravity="center" />
<Button
android:id="@+id/btn7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="使用gravity让文字居中"
android:gravity="center" />
</LinearLayout>
java文件为:
package com.example.pylearn_01_1;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
public class MainActivity extends Activity {
/* pylearn_01_1
* Button基本属性与方法
*/
Button btn3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn3=(Button)findViewById(R.id.btn3);
btn3.setText("使用java布局");//设置文字内容
btn3.setTextColor(android.graphics.Color.RED);//设置文字颜色
btn3.setTextSize(45);//设置文字大小
btn3.setEnabled(false);//设置按钮不能被点击
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
eclipse中看xml为:

模拟器中运行结果为:

源代码在此:pylearn_01_1
3.点点更健康
Button弄出来当然不是为了当花瓶的,咱们需要通过点击它来完成一些事情。
按钮的点击事件可以使用
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//do something
}
});
来实现。
源代码在此:pylearn_01_2
也可以使用
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
然后让MainActivity extends Activity implements OnClickListener实现onClick方法:
@Override
public void onClick(View v) {
// TODO 自动生成的方法存根
switch ( v.getId() ) {
case R.id.btn1:
fun_btn1();
break;
case R.id.btn2:
fun_btn2();
break;
}
}
源代码在此:pylearn_01_3
这两种方法都可以实现按钮点击事件的处理。
4.我的Button有点多
如何实现多个Button平分天下:
使用android:layout_weight控制各个按钮的权重,然后在parent布局中控制好权重和android:weightSum。最后设置各个按钮的占比为android:layout_width="0dp"。这样就实现了按钮的多个按钮的平均分配。
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.example.pylearn_01_1.MainActivity" >
<Button
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="4" >
<Button
android:id="@+id/btn1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1" />
<Button
android:id="@+id/btn2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2" />
<Button
android:id="@+id/btn3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3" />
<Button
android:id="@+id/btn4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="4" />
</LinearLayout>
</LinearLayout>
效果如下:

源代码在此:pylearn_01_4
5.震惊!TextView竟然...
忘记在哪看到的一句话:
能用TextView的地方就别用Button
Button能实现的基本上TextView也能实现
有兴趣可以试试:把上面所有程序中的Button换成TextView,毫无违和感。
来个Button看一看的更多相关文章
- 瞧一瞧,看一看呐,用MVC+EF快速弄出一个CRUD,一行代码都不用写,真的一行代码都不用写!!!!
瞧一瞧,看一看呐用MVC+EF快速弄出一个CRUD,一行代码都不用写,真的一行代码都不用写!!!! 现在要写的呢就是,用MVC和EF弄出一个CRUD四个页面和一个列表页面的一个快速DEMO,当然是在不 ...
- 2015年4月27日---C语言:输出特殊图案,请在c环境中运行,看一看,Very Beautiful!
---恢复内容开始--- 题目:输出特殊图案,请在c环境中运行,看一看,Very Beautiful! 1.程序分析:字符共有256个.不同字符,图形不一样. 2.程序源代码: [code=c] #i ...
- Loj#6183. 看无可看
Loj#6183. 看无可看 题目描述 首先用特征根求出通项公式\(A_n=p\cdot 3^n+q\cdot(-1)^n\).通过给定的\(f_0,f_1\)可以解出\(p,q\). 然后我们要求的 ...
- 看无可看 分治FFT+特征值方程
题面: 看无可看(see.pas/cpp/c) 题目描述 “What’s left to see when our eyes won’t open?” “若彼此瞑目在即,是否终亦看无可看?” ---- ...
- Scrum模拟微信看一看“疫情专区”的敏捷开发过程
无论作为产品用户还是管理咨询顾问,都非常非常喜欢微信.自认感情比较克制属于“高冷”挂,但从很多方面都太佩服太崇拜张小龙了(新书里微信也会是最喜欢的案例之一,真的不只是一个产品而已,很多方面都太牛了). ...
- Mysql数据库优化技术之配置篇、索引篇 ( 必看 必看 转)
转自:Mysql数据库优化技术之配置篇.索引篇 ( 必看 必看 ) (一)减少数据库访问对于可以静态化的页面,尽可能静态化对一个动态页面中可以静态的局部,采用静态化部分数据可以生成XML,或者文本文件 ...
- iOS - 基础知识总结(OC版) 面试必看 再不看就要用swift了
OC的理解与特性 OC作为一门面向对象的语言,自然具有面向对象的语言特性:封装.继承.多态.它既具有静态语言的特性(如C++),又有动态语言的效率(动态绑定.动态加载等).总体来讲,OC确实是一门不错 ...
- 今天做项目用到框架,关于angual,然后自己整理了一番,自己上网也看了看。
1. Angular 1.1. 库与框架的区别 jQuery:库 库一般都是封装了一些常用的方法 自己手动去调用这些方法,来完成我们的功能 $('#txt').val('我是小明'): $('div' ...
- PHP_Bibel阅读学习(一)——看书看经典,写文写代码
基础快速再看一下,然后每天有新的好玩的看. 这本书,反正好评不少,就是`PHP和MySQL Web开发`,机械工业出版社,澳洲人写的,红皮,有兴趣的可以看一下. 第一篇 使用PHP 一.入门 5分钟翻 ...
随机推荐
- Jenkins集成taffy进行自动化测试并输出测试报告
本文主要介绍Jenkins集成taffy/nose框架进行自动化测试并输出测试报告方法. 0. 测试环境 Jenkis主节点部署在CentOS系统上,子节点为Win10 64位系统(即我们本机运行自动 ...
- IOS 中的JS
文章摘自: http://www.cocoachina.com/ios/20150127/11037.html JSContext/JSValue JSContext 即JavaScript代码的 ...
- CentOS设置系统时间、硬件时间、以及定时校对时间
CentOS设置系统时间和时区 一.设置时区 方法一:使用setup工具 setup 选择Timezone configuration 选择Asia/Shanghai 空格键勾选上System clo ...
- Elasticsearch 索引别名与Template
在使用elasticsearch的时候,经常会遇到需要淘汰掉历史数据的场景. 为了方便数据淘汰,并使得数据管理更加灵活,我们经常会以时间为粒度建立索引,例如: 每个月建立一个索引:monthly-20 ...
- _.contains is not a function
在使用lodash的时候报_.contains is not a function,这是因为从lodash V3.10.1开始,将去掉contains函数,改用includes. 可以换为:_.con ...
- JavaScrpit中异步请求Ajax实现
在前端页面开发的过程中,经常使用到Ajax请求,异步提交表单数据,或者异步刷新页面. 一般来说,使用Jquery中的$.ajax,$.post,$.getJSON,非常方便,但是有的时候,我们只因为需 ...
- 透过浏览器看HTTP缓存[转载]
http://www.admin10000.com/document/6299.html 作为前端开发人员,对于我们的站点或应用的缓存机制我们能做的似乎不多,但这些却是与我们关注的性能息息相关 ...
- 开发一个基于 Android系统车载智能APP
很久之前就想做一个车载相关的app.需要实现如下功能: (1)每0.2秒更新一次当前车辆的最新速度值. (2)可控制性记录行驶里程. (3)不连接网络情况下获取当前车辆位置.如(北京市X区X路X号) ...
- linux 安装wordpress 无故往外发送大量垃圾邮件
linux 安装wordpress 无故往外发送大量垃圾邮件 始末 表现出来的现象就是, 网站运行没多久,mysql服务就挂了,重启也无法启动起来,提示 No such file or dicrion ...
- day2--第2章(计算机系统硬件核心知识)
第二章--计算机系统核心硬件知识 (一)互联网企业里PC服务器品牌及型号 互联网公司服务器品牌: DELL(大多数公司),HP,IBM(百度),浪潮,联想,航天联志. Dell服务器品牌: 1U = ...