从零开始学android小示例程序
程序员文章站
2024-02-07 16:30:52
布局文件
复制代码 代码如下:
布局文件
<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;
}
}
}
布局文件
复制代码 代码如下:
<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小示例程序
-
Android程序员学PHP开发(9)-赋值比较逻辑运算-PhpStorm
-
微信小程序webview实现长按点击识别二维码功能示例
-
Android程序员学PHP开发(10)-流程控制-PhpStorm
-
微信小程序单选radio及多选checkbox按钮用法示例
-
Android程序员学PHP开发(11)- 表单提交-PhpStorm
-
Android程序员学PHP开发(12)-表单POST失败解决方案-PhpStorm
-
微信小程序页面间传值的实现方法示例
-
Android程序员学PHP开发(13)-简易计算器-PhpStorm
-
Android程序员学PHP开发(14)-whlie练习-PhpStorm