1. 程式人生 > >安卓利用按鈕Button更改的字型大小、字型顏色、背景顏色程式碼

安卓利用按鈕Button更改的字型大小、字型顏色、背景顏色程式碼

本文章來介紹在android開發中,我們通過button按鈕來動態改變字型大小、字型顏色、背景顏色程式碼,有需要了解的朋友可參考參考。

實現的邏輯:通過遍歷View的方式,判斷View是否是TextView、EditText和Button型別,如果是的話,就修改。

程式碼如下:
1、xml佈局檔案,檔名:test4.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"
   android:id="@+id/mainLayout">
    
     
    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
         
        <EditText android:id="@+id/fontSize"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="30"
        android:hint="請輸入數字"/>
         
        <Button android:id="@+id/ChangeSize"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="改變字型"/>    
         
    </LinearLayout>
     
    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
         
        <EditText android:id="@+id/fontColor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="#ffffff"
        android:hint="請輸入字型顏色"/>
         
        <Button android:id="@+id/ChangeColor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="改變字型顏色" />    
         
    </LinearLayout>
     
     
    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
         
        <EditText android:id="@+id/bgColor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="#ff0000"
        android:hint="請輸入背景顏色"/>
         
        <Button android:id="@+id/ChangeBgColor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="改變背景顏色"/>    
         
    </LinearLayout>
     
    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
      
    
   <TextView android:id="@+id/TextView01"
                   android:layout_width="fill_parent"
                   android:layout_height="50dp"
                   android:text="top"
                   android:gravity="top"
                   android:textColor="#ffffff"
                   android:background="#00ff00"
                   android:layout_margin="2px"/>
                    
   <TextView android:id="@+id/TextView02"
                   android:layout_width="fill_parent"
                   android:layout_height="50dp"
                   android:text="bottom"
                   android:gravity="bottom"
                   android:textColor="#ffffff"
                   android:background="#00ff00"
                   android:layout_margin="2px"/>
                    
   <TextView android:id="@+id/TextView03"
                   android:layout_width="fill_parent"
                   android:layout_height="50dp"
                   android:text="left"
                   android:gravity="left"
                   android:textColor="#ffffff"
                   android:background="#00ff00"
                   android:layout_margin="2px"/>
                    
   <TextView android:id="@+id/TextView04"
                   android:layout_width="fill_parent"
                   android:layout_height="50dp"
                   android:text="right"
                   android:gravity="right"
                   android:textColor="#ffffff"
                   android:background="#00ff00"
                   android:layout_margin="2px"/>
                    
   <TextView android:id="@+id/TextView05"
                   android:layout_width="fill_parent"
                   android:layout_height="50dp"
                   android:text="center_vertical"
                   android:gravity="center_vertical"
                   android:textColor="#ffffff"
                   android:background="#00ff00"
                   android:layout_margin="2px"/>
                    
   <TextView android:id="@+id/TextView06"
                   android:layout_width="fill_parent"
                   android:layout_height="50dp"
                   android:text="fill_vertical"
                   android:gravity="fill_vertical"
                   android:textColor="#ffffff"
                   android:background="#00ff00"
                   android:layout_margin="2px"/>
                    
   <TextView android:id="@+id/TextView07"
                   android:layout_width="fill_parent"
                   android:layout_height="50dp"
                   android:text="center_horizontal"
                   android:gravity="center_horizontal"
                   android:textColor="#ffffff"
                   android:background="#00ff00"
                   android:layout_margin="2px"/>
                    
   <TextView android:id="@+id/TextView08"
                   android:layout_width="fill_parent"
                   android:layout_height="50dp"
                   android:text="fill_horizontal"
                   android:gravity="fill_horizontal"
                   android:textColor="#ffffff"
                   android:background="#00ff00"
                   android:layout_margin="2px"/>                                                                                                                                     
 
   <TextView android:id="@+id/TextView09"
                   android:layout_width="fill_parent"
                   android:layout_height="50dp"
                   android:text="center"
                   android:gravity="center"
                   android:textColor="#ffffff"
                   android:background="#00ff00"
                   android:layout_margin="2px"/>
                    
   <TextView android:id="@+id/TextView10"
                   android:layout_width="fill_parent"
                   android:layout_height="50dp"
                   android:text="fill"
                   android:gravity="fill"
                   android:textColor="#ffffff"
                   android:background="#00ff00"
                   android:layout_margin="2px"/>
                    
   <TextView android:id="@+id/TextView11"
                   android:layout_width="fill_parent"
                   android:layout_height="50dp"
                   android:text="clip_vertical"
                   android:gravity="clip_vertical"
                   android:textColor="#ffffff"
                   android:background="#00ff00"
                   android:layout_margin="2px"/>
                    
   <TextView android:id="@+id/TextView12"
                   android:layout_width="fill_parent"
                   android:layout_height="50dp"
                   android:text="clip_horizontal"
                   android:gravity="clip_horizontal"
                   android:textColor="#ffffff"
                   android:background="#00ff00"
                   android:layout_margin="2px"/>
    </LinearLayout>                                                                                               
< /LinearLayout>

2、實現的程式碼檔案:MainActivity.java,程式碼如下:

程式碼如下 複製程式碼

package org.shuxiang.test;
 
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity{
         
        private LinearLayout mainLayout;
        private Button changeSize, changeColor, changeBgColor;
        private EditText fontSize, fontColor, bgColor;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       

requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.test4);
         
        mainLayout = (LinearLayout) findViewById(R.id.mainLayout);
        changeSize = (Button) findViewById(R.id.ChangeSize);
        fontSize = (EditText) findViewById(R.id.fontSize);
         
        changeSize.setOnClickListener(new OnClickListener()
        {
                        @Override
                        public void onClick(View v)
                        {
                                // TODO Auto-generated method stub
                                setFontSize(mainLayout, Float.parseFloat(fontSize.getText().toString()));
                        }                
        });
         
        changeColor = (Button) findViewById(R.id.ChangeColor);
        fontColor = (EditText) findViewById(R.id.fontColor);
         
        changeColor.setOnClickListener(new OnClickListener()
        {
                        @Override
                        public void onClick(View v)
                        {
                                // TODO Auto-generated method stub
                                int color = Integer.parseInt(fontColor.getText().toString().replace("#", ""), 16);
                                int red = (color & 0xff0000) >> 16; 
                            int green = (color & 0x00ff00) >> 8; 
                            int blue = (color & 0x0000ff); 
                             
                            setFontColor(mainLayout, Color.rgb(red, green, blue));                                
                        }                
        });
         
        changeBgColor = (Button) findViewById(R.id.ChangeBgColor);
        bgColor = (EditText) findViewById(R.id.bgColor);
         
        changeBgColor.setOnClickListener(new OnClickListener()
        {
                        @Override
                        public void onClick(View v)
                        {
                                // TODO Auto-generated method stub
                                int color = Integer.parseInt(bgColor.getText().toString().replace("#", ""), 16);
                                int red = (color & 0xff0000) >> 16; 
                            int green = (color & 0x00ff00) >> 8; 
                            int blue = (color & 0x0000ff);
                             
                                setBgColor(mainLayout, Color.rgb(red, green, blue));
                        }                
        });        
 
        
    }
     
    /**
     * 改變字型
     * @param v
     * @param fontSize
     */
    public void setFontSize(View v, float fontSizeValue)
    {
            if(v instanceof TextView)
                {
                        ((TextView) v).setTextSize(fontSizeValue);
                }
                else if(v instanceof EditText)
                {
                        ((EditText) v).setTextSize(fontSizeValue);
                }
                else if(v instanceof Button)
                {
                        ((Button) v).setTextSize(fontSizeValue);
                }
                else
                {
                        int vChildCount = ((ViewGroup) v).getChildCount();
                    for(int i=0; i<vChildCount; i++)
                    {
                            View v1 = ((ViewGroup) v).getChildAt(i);
                            setFontSize(v1, fontSizeValue);
                    }
                }        
    }
     
    /**
     * 改變字型顏色
     * @param v
     * @param fontSize
     */
    public void setFontColor(View v, int fontColorValue)
    {
            if(v instanceof TextView)
                {
                        ((TextView) v).setTextColor(fontColorValue);
                }
                else if(v instanceof EditText)
                {
                        ((EditText) v).setTextColor(fontColorValue);
                }
                else if(v instanceof Button)
                {
                        ((Button) v).setTextColor(fontColorValue);
                }
                else
                {
                        int vChildCount = ((ViewGroup) v).getChildCount();
                    for(int i=0; i<vChildCount; i++)
                    {
                            View v1 = ((ViewGroup) v).getChildAt(i);
                            setFontColor(v1, fontColorValue);
                    }
                }        
    }
     
    /**
     * 改變背景字型
     * @param v
     * @param fontSize
     */
    public void setBgColor(View v, int bgColorValue)
    {
            if(v instanceof TextView)
                {
                        ((TextView) v).setBackgroundColor(bgColorValue);
                }
                else if(v instanceof EditText)
                {
                        ((EditText) v).setBackgroundColor(bgColorValue);
                }
                else if(v instanceof Button)
                {
                        ((Button) v).setBackgroundColor(bgColorValue);
                }
                else
                {
                        int vChildCount = ((ViewGroup) v).getChildCount();
                    for(int i=0; i<vChildCount; i++)
                    {
                            View v1 = ((ViewGroup) v).getChildAt(i);
                            setBgColor(v1, bgColorValue);
                    }
                }        
    }    
 
}