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

下面是第一个练习:

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

  • 首先新建一个安卓项目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. Ant学习-001-ant 基础知识及windows环境配置

    一.Ant 概要基础知识 Apache Ant 是一个将软件编译.测试.部署等步骤联系在一起加以自动化的一个工具,大多用于Java环境中的软件开发,用以构建应用,或结合其他开源测试工具例如 git.T ...

  2. http://blog.csdn.net/maosidiaoxian/article/details/40109337

    http://blog.csdn.net/maosidiaoxian/article/details/40109337

  3. 源码搭建SVN+Apache+Setpass

    1.安装配置apache2.2.18 http://download.csdn.net/download/YH555/3299526tar xf httpd-2.2.18.tar.bz2cd http ...

  4. js转换 /Date(1464671903000)/ 格式的日期的方法

    转换成:2016-07-11 function getFDate(date) { var d = eval('new ' + date.substr(1, date.length - 2)); var ...

  5. Java8 新特性default

    在JDK1.8的Iterator接口中 package java.util; import java.util.function.Consumer; public interface Iterator ...

  6. [g2o]一个备忘

    g2o使用的一个备忘 位姿已知,闭环的帧已知,进行图优化. #include "stdafx.h" #include <vector> #include "P ...

  7. Linux 源码安装httpd

    安装apr 下载解压apr-1.4.5 ./configure --prefix=/usr/local/apr make sudo make install 安装apr-util 下载解压apr-ut ...

  8. css3实现动画效果

    一.动画效果的常用属性 实现动画效果需要借助css3的下列属性:transform,transion,animation(具体可以参见教材) 二.动画效果实例 1)文字闪烁的动画效果 /*文字的闪烁效 ...

  9. python gzip,bz2学习

    一.gzip import gzip 1.解压缩 a = gzip.open('a.tar.gz') b = open('a.tar','wb') b.write(a.read()) a.close( ...

  10. Excessive AWR Growth From Partitioned Objects Such as SYS.WRH$_EVENT_HISTOGRAM Causing Sysaux to Grow

    AWR数据增长较快,导致sysaux表空间使用较高 SQL> select f.tablespace_name, 2 a.total, 3 f.free, 4 round((f.free / a ...