2011年7月21日星期四

利用程式碼更改文字顏色, setTextColor()

實例:

利用程式碼更改文字顏色, setTextColor()

package com.AndroidText;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class AndroidTextActivity extends Activity {

TextView text;
Button buttonChangeTextColor;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = (TextView)findViewById(R.id.text);
buttonChangeTextColor = (Button)findViewById(R.id.changetextcolor);

buttonChangeTextColor.setOnClickListener(new Button.OnClickListener(){

public void onClick(View arg0) {
// TODO Auto-generated method stub

/*
* setTextColor(
* ((alpha << 24) & 0xFF000000)
* + ((red << 16) & 0x00FF0000)
* + ((green << 8) & 0x0000FF00)
* + (blue & 0x000000FF));
*/


//setTypeface for TextView
text.setTextColor(0xFF008080);
//setTypeface for Button
buttonChangeTextColor.setTextColor(0xFFFF0000);
}});
}
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/changetextcolor"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Change Text Color"
/>
</LinearLayout>


沒有留言:

發佈留言