从零开始学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;
    }

}
}

(0)

相关推荐

  • android教程之把自己的应用加入到系统分享中

    在自己应用的AndroidManifest.xml某个activity配置不同类型的intent-filter, 这里添加的是图片, 也可以添加其它类型 复制代码 代码如下: <intent-filter>   <action android:name="android.intent.action.SEND" />   <data android:mimeType="image/*"/>   <category androi

  • android教程之textview解析带图片的html示例

    复制代码 代码如下: public class MainActivity extends Activity { private Handler handler; private String html; private TextView tv; private ProgressBar bar; @Override protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  s

  • Android创建服务之started service详细介绍

    创建started service 应用组件(例如Activity)调用startService()来启动一个Service,将需要的参数通过Intent传给Service,Service将会在onStartCommand函数中获得Intent. 有两种方式可以创建started service,一种是扩展Service类,另外一种是扩展IntentService类 扩展Service       这是所有服务的基类.扩展这个类的时候,特别重要的一点是,需要创建一个新的线程来做服务任务,因为se

  • android配合viewpager实现可滑动的标签栏示例分享

    复制代码 代码如下: package com.example.playtabtest.view; import com.example.playtabtest.R; import android.app.Activity;import android.content.Context;import android.support.v4.view.ViewPager;import android.support.v4.view.ViewPager.OnPageChangeListener;impor

  • android显示TextView文字的倒影效果实现代码

    今天记录一下TextView的倒影效果,显示一串文字,然后在文字的下方显示出它的倒影,先上效果图: 最重要的就是View中getDrawingCache()方法,该方法可以获取cache中的图像,然后绘制出来. 废话不多说,我是想写一个带有倒影的时间,时间可以走动.首先先写一个带有时间走动的View,这个很简单,获取当前时间,然后开启一个线程,隔一秒获取当前时间一次,然后显示在TextView上,当然,我们写控件,就需要继承TextView,代码如下: 复制代码 代码如下: package co

  • android教程之intent的action属性使用示例(intent发短信)

    Action :规定了Intent要完成的动作,是一个字符串常量.使用setAction()来设置Action属性,使用getAction()来获得Action属性.既可以使用系统内置的Action,也可以自己定义.系统自定义的action,如ACTION_VIEW, ACTION_EDIT, ACTION_MAIN等等. 1.自定义Action 在"目的Activity"的AndroidManifest.xml中指定action常量. 复制代码 代码如下: <activity

  • android开发教程之使用listview显示qq联系人列表

    首先还是xml布局文件,在其中添加ListView控件: 主布局layout_main.xml 复制代码 代码如下: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"

  • android开发教程之系统资源的使用方法 android资源文件

    一.颜色资源1.颜色XML文件格式 复制代码 代码如下: <?xml version="1.0" encoding="utf-8"?><resources> //resources根元素   <color name="" >#000000</color>//color子元素</resouces> 2.引用格式: java代码中:R.color.color_name  (这是一个int型的

  • android教程之使用asynctask在后台运行耗时任务

    , Android中实现了默认的进度提示对话框,即ProgressDialog,通过实例化和一些简单设置,就可以使用了. 复制代码 代码如下: private class DownloadDBTask extends AsyncTask<String, Integer, String> {           // 可变长的输入参数,与AsyncTask.exucute()对应           ProgressDialog pdialog;           public Downloa

  • 从零开始学android实现计算器功能示例分享(计算器源码)

    下面是效果展示: 复制代码 代码如下: <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match

  • Android中实现可滑动的Tab的3种方式

    1. 第一种,使用 TabHost + ViewPager 实现该方法会有一个Bug,当设置tabHost.setCurrentTab()为0时,ViewPager不显示(准确的说是加载),只有点击其他任意一个tab后才会加载. 有解的同学吼一声~~~~~~~ Activity: 复制代码 代码如下: package com.swordy.demo.android.fragment; import java.util.Random; import android.os.Bundle;import

  • android教程之service使用方法示例详解

    Service的生命周期 (适用于2.1及以上) 1. 被startService的无论是否有任何活动绑定到该Service,都在后台运行.onCreate(若需要) -> onStart(int id, Bundle args).  多次startService,则onStart调用多次,但不会创建多个Service实例,只需要一次stop.该Service一直后台运行,直到stopService或者自己的stopSelf()或者资源不足由平台结束. 2. 被bindService的调用bin

  • Android ImageButton自定义按钮的按下效果的代码实现方法分享

    使用Button时为了让用户有"按下"的效果,有两种实现方式:1.在代码里面. 复制代码 代码如下: imageButton.setOnTouchListener(new OnTouchListener(){ @Override                          public boolean onTouch(View v, MotionEvent event) {                                  if(event.getAction()

  • android开发教程之listview使用方法

    首先是布局文件,这里需要两个布局文件,一个是放置列表控件的Activity对应的布局文件 main.xml,另一个是ListView中每一行信息显示所对应的布局  list_item.xml    这一步需要注意的问题是ListView 控件的id要使用Android系统内置的 android:id="@android:id/list"   [注意形式] main.xml 复制代码 代码如下: <?xml version="1.0" encoding=&quo

  • android教程之使用popupwindow创建菜单示例

    PopupWindow是一个弹出式窗口,它可以展示任意View.他会浮在当前窗口的上方展示. 下面看代码: 复制代码 代码如下: public class MyActivity extends Activity{    private PopupWindow menu;    private LayoutInflater inflater;    private View layout; @Override    public void onCreate(Bundle savedInstance

  • android开发教程之实现listview下拉刷新和上拉刷新效果

    复制代码 代码如下: public class PullToLoadListView extends ListView implements OnScrollListener { private static final String TAG = PullToLoadListView.class.getSimpleName(); private static final int STATE_NON = 0; private static final int STATE_PULL_TO_REFRE

  • Android中的Looper对象详细介绍

    Java 官网对Looper对象的说明: public class Looperextends ObjectClass used to run a message loop for a thread. Threads by default do not have a message loop associated with them; to create one, call prepare() in the thread that is to run the loop, and then loo

  • android教程之hockeyapp捕获异常示例

    复制代码 代码如下: package com.example.testhockeyapp;import net.hockeyapp.android.CrashManager;import net.hockeyapp.android.CrashManagerListener;import net.hockeyapp.android.UpdateManager;import android.os.Bundle;import android.app.Activity;import android.vi

随机推荐