Android uses button buttons to change font size, font color, background color code

Source: Internet
Author: User
Tags button type gettext stub

Implementation logic: By traversing the view, determine if the view is TextView, edittext, and button type, and if so, modify it.

The code is as follows:
1, XML layout file, FileName: Test4.xml, the contents are as follows:

The code is as follows Copy Code

<?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= "Please enter a number"/>





<button android:id= "@+id/changesize"


Android:layout_width= "Wrap_content"


android:layout_height= "Wrap_content"


android:text= "Change Font"/>





</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= "Please enter font color"/>





<button android:id= "@+id/changecolor"


Android:layout_width= "Wrap_content"


android:layout_height= "Wrap_content"


android:text= "Change font Color"/>





</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= "Please enter background color"/>





<button android:id= "@+id/changebgcolor"


Android:layout_width= "Wrap_content"


android:layout_height= "Wrap_content"


android:text= "Change the background color"/>





</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, the implementation of the code file: Mainactivity.java, the code is as follows:

The code is as follows Copy Code

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));


}


});








}





/**


* Change Font


* @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);


}


}


}





/**


* Change Font Color


* @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);


}


}


}





/**


* Change Background font


* @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);


}


}


}





}

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.