这两天有空,打算把一些文档整理一下,快要考试了,找一些简单的例子来做做,重温安卓的知识.

下面是第一个练习:

实现很简单,下面我们来看看:

  • 首先新建一个安卓项目Demo1
  • 接着是界面的布局(包括activity_main.xml/strings.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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
 
    <TextView
        android:id="@+id/showWord"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/GreenBtn"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/BlueBtn"
        android:layout_marginTop="17dp"
        android:text="@string/ShowWordText"
        android:textStyle="bold" />
 
    <Button
        android:id="@+id/GreenBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/showWord"
        android:layout_marginLeft="22dp"
        android:layout_marginTop="28dp"
        android:gravity="left|center_vertical|center_horizontal"
        android:text="@string/GreenBtnText" />
 
    <Button
        android:id="@+id/BlueBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/YellowBtn"
        android:layout_alignBottom="@+id/YellowBtn"
        android:layout_toRightOf="@+id/YellowBtn"
        android:gravity="left|center_vertical|center_horizontal"
        android:text="@string/BlueBtnText" />
 
    <Button
        android:id="@+id/YellowBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/GreenBtn"
        android:layout_alignBottom="@+id/GreenBtn"
        android:layout_toRightOf="@+id/GreenBtn"
        android:gravity="left|center_vertical|center_horizontal"
        android:text="@string/YellowBtnText" />
 
</RelativeLayout>

----------------------------功能实现--------------------

package com.example.demo1;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
import android.graphics.Color;
 
 
public class MainActivity extends Activity implements OnClickListener{
 
    TextView tv_show=null;
    Button greenBtn=null;
    Button blueBtn=null;
    Button yellowBtn=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        tv_show=(TextView)findViewById(R.id.showWord);
        greenBtn=(Button)findViewById(R.id.GreenBtn);
        blueBtn=(Button)findViewById(R.id.BlueBtn);
        yellowBtn=(Button)findViewById(R.id.YellowBtn);
        
        greenBtn.setOnClickListener(this);
        blueBtn.setOnClickListener(this);
        yellowBtn.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
    switch (v.getId()) {
    case R.id.GreenBtn:
        tv_show.setTextColor(Color.GREEN);        
        break;
    case R.id.BlueBtn:
        tv_show.setTextColor(Color.BLUE);        
        break;
    case R.id.YellowBtn:
        tv_show.setTextColor(Color.YELLOW);        
        break;
    default:
        break;
    }
        
    }
 
 
 
}

源码下载

Technorati 标签: android,picture,listener

android实操--练习1的更多相关文章

  1. android实操--练习2

    练习2是实现一个计算器的功能:可以加减乘除:可以倒退,可以清空文本. 下面是效果展示: -----------------------布局------------------------------- ...

  2. Appium常用Api实操

    本文是基于python语言在android上实操的,仅记录(忽略排版~~~) 会不时更新的: from appium import webdriver from selenium.webdriver. ...

  3. ABP入门系列(1)——学习Abp框架之实操演练

    作为.Net工地搬砖长工一名,一直致力于挖坑(Bug)填坑(Debug),但技术却不见长进.也曾热情于新技术的学习,憧憬过成为技术大拿.从前端到后端,从bootstrap到javascript,从py ...

  4. 号外号外:9月13号《Speed-BI云平台案例实操--十分钟做报表》开讲了

    引言:如何快速分析纷繁复杂的数据?如何快速做出老板满意的报表?如何快速将Speed-BI云平台运用到实际场景中?         本课程将通过各行各业案例背景,将Speed-BI云平台运用到实际场景中 ...

  5. Mysql MHA(GTID)配置(实操)

    实现环境 centos6.7 MYSQL5.6.36 主:192.168.1.191 从1:192.168.1.145 从2:192.168.1.146 监测:放在从2上 192.168.1.146 ...

  6. Selenium之unittest测试框架详谈及实操

    申明:本文是基于python3.x及selenium3.x. unittest,也可以称为PyUnit,可以用来创建全面的测试套件,可以用于单元自动化测试(模块).功能自动化测试(UI)等等. 官方文 ...

  7. unittest测试框架详谈及实操(二)

    类级别的setUp()方法与tearDown()方法 在实操(一)的例子中,通过setUp()方法为每个测试方法都创建了一个Chrome实例,并且在每个测试方法执行结束后要关闭实例.是不是觉得有个多余 ...

  8. Android实训案例(九)——答题系统的思绪,自己设计一个题库的体验,一个思路清晰的答题软件制作过程

    Android实训案例(九)--答题系统的思绪,自己设计一个题库的体验,一个思路清晰的答题软件制作过程 项目也是偷师的,决心研究一下数据库.所以写的还是很详细的,各位看官,耐着性子看完,实现结果不重要 ...

  9. Android实训案例(八)——单机五子棋游戏,自定义棋盘,线条,棋子,游戏逻辑,游戏状态存储,再来一局

    Android实训案例(八)--单机五子棋游戏,自定义棋盘,线条,棋子,游戏逻辑,游戏状态存储,再来一局 阿法狗让围棋突然就被热议了,鸿洋大神也顺势出了篇五子棋单机游戏的视频,我看到了就像膜拜膜拜,就 ...

随机推荐

  1. js保留n位小数

    1.功能:将浮点数四舍五入,取小数点后2位 function toDecimal(num) { var f = parseFloat(num); if (isNaN(f)) { return; } f ...

  2. Quartzs -- Quartz.properties 配置

    Quartzs -- Quartz.properties 配置 文件加载位置 默认:优先顺序 Classpath:quartz.properties --> org/quartz/quartz. ...

  3. [代码片段]读取BMP文件(二)

    #include <stdio.h> #include <stdlib.h> #pragma pack(2) /*定义WORD为两个字节的类型*/ typedef unsign ...

  4. centos7安装openvswitch虚拟交换机

    What is Open vSwitch? Open vSwitch is a production quality, multilayer virtual switch licensed under ...

  5. Linux 文件系统错误的修复方法 ddrescue替代dd的恢复软件 备用超级块

    Linux 文件系统错误的修复方法  ddrescue替代dd的恢复软件  备用超级块 最近处理的一件 linux 服务器断电导致文件系统启动后文件系统不可读写,数据不可用的案例,现总结下 Linux ...

  6. Linux就这个范儿 第13章 打通任督二脉

    Linux就这个范儿 第13章 打通任督二脉 0111010110……你有没有想过,数据从看得见或看不见的线缆上飞来飞去,是怎么实现的呢?数据传输业务的未来又在哪里?在前面两章中我们学习了Linux网 ...

  7. php Memcache

    <?php $mem = new Memcache();//实例化一个对象 $mem->connect("localhost",11211);//连接memcache服 ...

  8. shell中如何判断某一命令是否存在

    参考: http://www.cnblogs.com/tuzkee/p/3755230.html https://segmentfault.com/q/1010000000156870 http:// ...

  9. ios-chart 不支持渐变的底色 --- 后面支持了渐变

    https://github.com/danielgindi/ios-charts/issues/186 后话,最近库更新了,可以支持渐变色

  10. throw 子句

    throws是声明方法时抛出可能出现的异常,但不能捕获异常,也就是说并不直接处理异常,而是把它向上传递.其格式如下: 方法声明 throws 异常类名列表 若一个方法声明抛出异常,则表示该方法可能会抛 ...