1.用代码设置控件的颜色:
import android.app.activity;
/*必须引用graphics.color才能使用color.*的对象*/
import android.graphics.color;
import android.graphics.typeface;
import android.os.bundle;
import android.view.view;
/*必须引用 widget.button才能声明使用button对象*/
import android.widget.button;
/*必须引用 widget.textview才能声明使用testview对象*/
import android.widget.textview;
public class ch3_6 extends activity
{
private button mbutton;
private textview mtext;
private int[] mcolors;
private int colornum;
private button fontbutton;
/** called when the activity is first created. */
@override
public void oncreate(bundle savedinstancestate)
{
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
/*通过findviewbyid构造器来使用main.xml与string.xml
中button与textview的参数*/
mbutton=(button) findviewbyid(r.id.mybutton);
mtext= (textview) findviewbyid(r.id.mytext);
fontbutton=(button) findviewbyid(r.id.mybutton1);
/*声明并构造一整数array来存储欲使用的文字颜色*/
mcolors = new int[]
{
color.black, color.red, color.blue,
color.green, color.magenta, color.yellow
};
colornum=0;
//得到color.xml文件里的颜色
int b = getresources().getcolor(r.drawable.blue);//得到配置文件里的颜色
mbutton.settextcolor(b);
/*使用setonclicklistener让按钮聆听事件*/
mbutton.setonclicklistener(new view.onclicklistener()
{
/*使用onclick让用户点下按钮来驱动变动文字颜色*/
public void onclick(view v)
{
if (colornum < mcolors.length)
{
mtext.settextcolor(mcolors[colornum]);
colornum++;
}
else
colornum=0;
}
});
fontbutton.setonclicklistener(new button.onclicklistener() {
public void onclick(view v) {
mtext.settypeface(typeface.createfromasset(getassets(),"fonts/handmadetypewriter.ttf"));//设置字体
fontbutton.settypeface(typeface.defaultfromstyle(typeface.italic));//用内部支持的方式设置
}
});
}
}
如对本文有疑问, 点击进行留言回复!!
intent.setFlag的参数 FLAG_ACTIVITY_CLEAR_TASK 说明
荐 「「第一行代码(第二版)」」学习笔记 5 碎片(连载中)
Android ViewBinding include怎么玩
安卓入门第三讲01-Activity的简单逻辑操作(对文本,图片进行设置(两种获取控件的方法),按钮点击-点击按钮打印日志,获取文本框的值+计算器简易案例)
网友评论