<resources>
    <string name="app_name">ChangeColor</string>
    <string name="yellow">yellow</string>
    <string name="green">green</string>
    <string name="blue">blue</string>
    <string name="output">Hello World!</string>
</resources>



<?xml version="1.0" encoding="utf-8"?>
<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="com.iii.lcpan.changecolor.MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/yellow"
        android:id="@+id/yellow"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="126dp"
        android:textAllCaps="false"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/green"
        android:id="@+id/green"
        android:layout_alignTop="@+id/yellow"
        android:layout_centerHorizontal="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/blue"
        android:id="@+id/blue"
        android:layout_alignTop="@+id/green"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/output"
        android:id="@+id/output"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>



package com.iii.lcpan.changecolor;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private TextView output;
    private Button yellow , green , blue;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        output=(TextView) findViewById(R.id.output);
        yellow =(Button)findViewById(R.id.yellow);
        green =(Button)findViewById(R.id.green);
        blue =(Button)findViewById(R.id.blue);
        yellow.setOnClickListener(this);
        green.setOnClickListener(this);
        blue.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) { 你按了哪一個鈕,就傳回那個鈕的view物件
        switch(v.getId()){ //取得那個鈕的id,v.getId()取得的是16進元的數字0x1234這樣
            case R.id.yellow : output.setTextColor(Color.YELLOW); break;
            case R.id.green : output.setTextColor(Color.GREEN); break;
            case R.id.blue : output.setTextColor(Color.BLUE); break;
        }
    }
}
創作者介紹

學習筆記專區

乙方 發表在 痞客邦 PIXNET 留言(0) 人氣()