效果图:
main.xml
package xiacl.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.graphics.Color;
import android.graphics.Typeface;
public class Test1Activity extends Activity {
private TextView mText;
private int[] mColors;
private int colornum;
private Button colorButton;
private Button sizeButton;
private Button fontButton;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mText=(TextView)findViewById(R.id.mytext);
colorButton=(Button)findViewById(R.id.colorbutton);
sizeButton=(Button)findViewById(R.id.sizebutton);
fontButton=(Button)findViewById(R.id.fontbutton);
mColors=new int[]{
Color.BLACK,Color.RED,Color.BLUE,
Color.GREEN,Color.MAGENTA,Color.YELLOW
};
colornum=0;
colorButton.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(colornum<mColors.length)
{
mText.setTextColor(mColors[colornum]);
colornum++;
}
else
colornum=0;
}
});
sizeButton.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mText.setTextSize(20);
}
});
fontButton.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
/*必须事先在assets底下创建一fonts文件夹
* 并放入要使用的字体文件(.ttf)
* 并提供相对路径给creatFromAsset()来创建Typeface对象*/
mText.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/HandmadeTypewriter.ttf"));
}
});
}
}