因為對Android系統感興趣,所以專題就決定朝這方向去做。然後每個禮拜都要做一個進度。這就是這禮拜的進度啦!功能是透過SeekBar去改變字體大小,RadioButton設定字體顏色。程式碼如下:

main.xml部分

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView0"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="HelloWorld!" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="字體大小" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="14px" />

    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="字體顏色" />

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >


        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="紅色" />


        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="藍色" />


        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="綠色" />

    </RadioGroup>

</LinearLayout>

MaianActivity.java

package CXY.COM;

import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.graphics.*;

public class TextChangeActivity extends Activity implements OnSeekBarChangeListener,OnCheckedChangeListener{
    /** Called when the activity is first created. */
   
   private  SeekBar ChangeSize;
   private  TextView myText;
   private  TextView Size;
   private  RadioGroup SelectColor;
   private  RadioButton Radio0,Radio1,Radio2;
  
   @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        ChangeSize=(SeekBar)findViewById(R.id.seekBar1);
        myText=(TextView)findViewById(R.id.textView0);
        Size=(TextView)findViewById(R.id.textView3);
        SelectColor=(RadioGroup)findViewById(R.id.radioGroup1);
        Radio0=(RadioButton)findViewById(R.id.radio0);
        Radio1=(RadioButton)findViewById(R.id.radio1);
        Radio2=(RadioButton)findViewById(R.id.radio2);
        ChangeSize.setMax(36);
        ChangeSize.setProgress(14);
        myText.setTextSize(14);
        myText.setTextColor(android.graphics.Color.RED);
        ChangeSize.setOnSeekBarChangeListener(this);
        SelectColor.setOnCheckedChangeListener(this);
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int value, boolean arg2) {
        // TODO Auto-generated method stub
        myText.setTextSize(value);
        Size.setText(value+"px");
    }
   
    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub
       
    }
   
    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub
       
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
        // TODO Auto-generated method stub
        if(checkedId==Radio0.getId())
            myText.setTextColor(android.graphics.Color.RED);
        else if(checkedId==Radio1.getId())
            myText.setTextColor(android.graphics.Color.BLUE);
        else
            myText.setTextColor(android.graphics.Color.GREEN);
    }
}

程式截圖

 

文章標籤
全站熱搜
創作者介紹
創作者 jeff810123 的頭像
jeff810123

在我心中有一個夢

jeff810123 發表在 痞客邦 留言(0) 人氣(226)