初识安卓小程序(点击按钮切换屏幕颜色)

时间:2014-05-09 22:30:47   收藏:0   阅读:441

bubuko.com,布布扣

如图,点击按钮就会切换屏幕的颜色

首先,先创建一个安卓项目(我的版本是4.4.2的),名字为"world",当然,也可以别的名称

然后在res文件夹下找到layout文件夹,找到activity_main.xml或fragment_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: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"
    android:id="@+id/layout"
    tools:context="com.example.world.MainActivity$PlaceholderFragment">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="14dp"
        android:layout_marginTop="22dp"
        android:layout_toRightOf="@+id/textView1"
        android:text="红色" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="44dp"
        android:text="蓝色" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/button2"
        android:layout_below="@+id/button2"
        android:layout_marginTop="51dp"
        android:text="绿色" />

</RelativeLayout>

然后在values文件夹下创建一个color.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <color name="red">#FF0000</color>
    <color name="green">#008000</color>
    <color name="blue">#0000FF</color>

</resources>

最后在src下的java文件里MainActivity.java

package com.example.world;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.os.Build;

public class MainActivity<ActivityLayout> extends Activity {
	
	private Button btn1,btn2,btn3;
	private RelativeLayout layout;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_main);

			
		layout=(RelativeLayout) this.findViewById(R.id.layout);
		btn1=(Button) this.findViewById(R.id.button1);
		//点击事件
		btn1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				layout.setBackgroundColor(getResources().getColor(R.color.red));
				
			}
		});
		btn2=(Button) this.findViewById(R.id.button2);
		
		btn2.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				layout.setBackgroundColor(getResources().getColor(R.color.blue));
				
			}
		});
		btn3=(Button) this.findViewById(R.id.button3);
		
		btn3.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				layout.setBackgroundColor(getResources().getColor(R.color.green));
				
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
}

最后运行,点击相应的按钮就会出现以下效果了:

bubuko.com,布布扣bubuko.com,布布扣bubuko.com,布布扣


初识安卓小程序(点击按钮切换屏幕颜色),布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!