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看一看的更多相关文章

  1. 瞧一瞧,看一看呐,用MVC+EF快速弄出一个CRUD,一行代码都不用写,真的一行代码都不用写!!!!

    瞧一瞧,看一看呐用MVC+EF快速弄出一个CRUD,一行代码都不用写,真的一行代码都不用写!!!! 现在要写的呢就是,用MVC和EF弄出一个CRUD四个页面和一个列表页面的一个快速DEMO,当然是在不 ...

  2. 2015年4月27日---C语言:输出特殊图案,请在c环境中运行,看一看,Very Beautiful!

    ---恢复内容开始--- 题目:输出特殊图案,请在c环境中运行,看一看,Very Beautiful! 1.程序分析:字符共有256个.不同字符,图形不一样. 2.程序源代码: [code=c] #i ...

  3. Loj#6183. 看无可看

    Loj#6183. 看无可看 题目描述 首先用特征根求出通项公式\(A_n=p\cdot 3^n+q\cdot(-1)^n\).通过给定的\(f_0,f_1\)可以解出\(p,q\). 然后我们要求的 ...

  4. 看无可看 分治FFT+特征值方程

    题面: 看无可看(see.pas/cpp/c) 题目描述 “What’s left to see when our eyes won’t open?” “若彼此瞑目在即,是否终亦看无可看?” ---- ...

  5. Scrum模拟微信看一看“疫情专区”的敏捷开发过程

    无论作为产品用户还是管理咨询顾问,都非常非常喜欢微信.自认感情比较克制属于“高冷”挂,但从很多方面都太佩服太崇拜张小龙了(新书里微信也会是最喜欢的案例之一,真的不只是一个产品而已,很多方面都太牛了). ...

  6. Mysql数据库优化技术之配置篇、索引篇 ( 必看 必看 转)

    转自:Mysql数据库优化技术之配置篇.索引篇 ( 必看 必看 ) (一)减少数据库访问对于可以静态化的页面,尽可能静态化对一个动态页面中可以静态的局部,采用静态化部分数据可以生成XML,或者文本文件 ...

  7. iOS - 基础知识总结(OC版) 面试必看 再不看就要用swift了

    OC的理解与特性 OC作为一门面向对象的语言,自然具有面向对象的语言特性:封装.继承.多态.它既具有静态语言的特性(如C++),又有动态语言的效率(动态绑定.动态加载等).总体来讲,OC确实是一门不错 ...

  8. 今天做项目用到框架,关于angual,然后自己整理了一番,自己上网也看了看。

    1. Angular 1.1. 库与框架的区别 jQuery:库 库一般都是封装了一些常用的方法 自己手动去调用这些方法,来完成我们的功能 $('#txt').val('我是小明'): $('div' ...

  9. PHP_Bibel阅读学习(一)——看书看经典,写文写代码

    基础快速再看一下,然后每天有新的好玩的看. 这本书,反正好评不少,就是`PHP和MySQL Web开发`,机械工业出版社,澳洲人写的,红皮,有兴趣的可以看一下. 第一篇 使用PHP 一.入门 5分钟翻 ...

随机推荐

  1. svn 常用命令行

    1.将文件checkout到本地目录    svn checkout path(path是服务器上的目录)    例如:svn checkout svn://192.168.1.1/pro/domai ...

  2. OpenCV Image Watch 调试插件

    昨晚偶然发现vs2012的这个很神奇的插件,对于经常使用opencv的人来说,这个插件无疑是我们的调试神器.今天马上下载试用,感觉超级棒!~以后要想查看图像结果,不用再imshow了! Image W ...

  3. Treats for the Cows

     Treats for the Cows Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64 ...

  4. Problem W

    Problem Description Speakless很早就想出国,现在他已经考完了所有需要的考试,准备了所有要准备的材料,于是,便需要去申请学校了.要申请国外的任何大学,你都要交纳一定的申请费用 ...

  5. Phonegap开发相关问题

    环境搭建:参考http://www.phonegapcn.com/start/zh/1.3/#android 调试: 1.在线远程调试 http://debug.phonegap.com/ 通过USB ...

  6. 透过浏览器看HTTP缓存[转载]

    http://www.admin10000.com/document/6299.html     作为前端开发人员,对于我们的站点或应用的缓存机制我们能做的似乎不多,但这些却是与我们关注的性能息息相关 ...

  7. 开发一个基于 Android系统车载智能APP

    很久之前就想做一个车载相关的app.需要实现如下功能: (1)每0.2秒更新一次当前车辆的最新速度值. (2)可控制性记录行驶里程. (3)不连接网络情况下获取当前车辆位置.如(北京市X区X路X号) ...

  8. Problem J: 求个最大值

    Problem J: 求个最大值 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 871  Solved: 663[Submit][Status][Web ...

  9. Remove Untagged Images From Docker

    I've been playing around a lot with docker. It's awesome, and it creates a whole new world of possib ...

  10. VS2013创建Windows服务

    一.创建服务 1.文件->新建->项目->windows桌面->windows服务,修改你要的项目名称.我这不改名,仍叫WindowsService1,确定. 2.其中的Pro ...