布局文件
复制代码 代码如下:
<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;
}
}
}