从零开始学android小示例程序

 更新时间:2014年02月08日 14:54:14   作者:  
这篇文章主要介绍了一个学习android开发的小示例程序,需要的朋友可以参考下



布局文件

复制代码 代码如下:

<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;
    }

    }
}

相关文章

  • Android自定义ViewGroup实现弹性滑动效果

    Android自定义ViewGroup实现弹性滑动效果

    这篇文章主要为大家详细介绍了Android自定义ViewGroup实现弹性滑动效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-12-12
  • 小谈Kotlin的空处理的使用

    小谈Kotlin的空处理的使用

    这篇文章主要介绍了小谈Kotlin的空处理的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-01-01
  • phonegap教程使用jspdf库在应用中生成pdf文件(pdf生成方法)

    phonegap教程使用jspdf库在应用中生成pdf文件(pdf生成方法)

    在PhoneGap应用中生成pdf文件,实现起来很简单,使用JSPDF这个标准的JavaScript类库来实现这个功能
    2014-01-01
  • Android显示系统SurfaceFlinger详解

    Android显示系统SurfaceFlinger详解

    本文详细讲解了Android显示系统SurfaceFlinger,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-12-12
  • Flutter实现底部导航

    Flutter实现底部导航

    这篇文章主要为大家详细介绍了Flutter实现底部导航的实现方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-02-02
  • Android9.0 静默安装源码的实现

    Android9.0 静默安装源码的实现

    这篇文章主要介绍了Android9.0 静默安装源码的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-01-01
  • Flutter学习之构建、布局及绘制三部曲

    Flutter学习之构建、布局及绘制三部曲

    这篇文章主要给大家介绍了关于Flutter学习之构建、布局及绘制三部曲的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Flutter具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-04-04
  • Android微信图片浏览框架设计

    Android微信图片浏览框架设计

    这篇文章主要为大家详细介绍了Android微信图片浏览框架设计,感兴趣的小伙伴们可以参考一下
    2016-08-08
  • Android使用手势实现翻页效果

    Android使用手势实现翻页效果

    这篇文章主要介绍了Android使用手势实现翻页效果,本程序使用了一个ViewFlipper组件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-09-09
  • 多面分析HarmonyOS与Android的特点

    多面分析HarmonyOS与Android的特点

    请教身边的大佬们,公司的CTO、中台部门的总监、老东家数十年行业经验的老架构、以及在中科院读研究生的大学老室友、技术圈的网友等等,他们都给出了自己独特的看法,让我从多方面更好的去了解到了大家对鸿蒙的认识
    2021-08-08

最新评论