Learning common android widgets ①-button and textview

Source: Internet
Author: User

I learned about Button and textview this morning! The details are as follows!

 

This is the directory file I created.

First, set the layout: in Main. xml

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> <button <br/> Android: TEXT = "this is a button" <br/> Android: Id = "@ + ID/button" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content"> <br/> </button> </P> <p> <button <br/> Android: text = "textview" <br/> Android: id = "@ + ID/text_view_button" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content"> <br/> </button> <br/> </linearlayout> </P> <p>

Content in widgetdemo. activity:

 

Package COM. example. widgetdemo; </P> <p> Import android. app. activity; <br/> Import android. content. intent; <br/> Import android. OS. bundle; <br/> Import android. view. view; <br/> Import android. widget. button; </P> <p> public class widgetdemo extends activity {<br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> find_and_modify_button (); <br/>}< br/> private void find_and_modify_button () {<br/> button = (button) findviewbyid (R. id. button); <br/> button. setonclicklistener (button_listener); <br/> button text_view_button = (button) findviewbyid (R. id. text_view_button); <br/> text_view_button.setonclicklistener (text_view_button_listener); </P> <p >}< br/> private button. onclicklistener button_listener = new button. onclicklistener () {<br/> @ override <br/> Public void onclick (view V) {<br/> // todo auto-generated method stub <br/> settitle ("click it"); <br/>}< br/> }; <br/> private button. onclicklistener text_view_button_listener = new button. onclicklistener () {<br/> Public void onclick (view v) {<br/> intent = new intent (); <br/> intent. setclass (widgetdemo. this, textactivity. class); <br/> startactivity (intent); <br/>}< br/>}; </P> <p >}< br/>

Content in manifest. xml:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <manifest xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> package = "com. example. widgetdemo "<br/> Android: versioncode =" 1 "<br/> Android: versionname =" 1.0 "> <br/> <application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name"> <br/> <activity Android: Name = ". widgetdemo "<br/> Android: Label =" @ string/app_name "> <br/> <intent-filter> <br/> <action Android: Name =" android. intent. action. main "/> <br/> <category Android: Name =" android. intent. category. launcher "/> <br/> </intent-filter> <br/> </activity> <br/> <activity Android: name = "textactivity"> <br/> </activity> </P> <p> </Application> <br/> <uses-SDK Android: minsdkversion = "4"/> </P> <p> </manifest>

Code in text. xml:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> <textview <br/> Android: text = "this is textview, you can input anything you want "<br/> Android: Id =" @ + ID/text_view "<br/> Android: layout_width =" wrap_content "<br/> Android: layout_height = "wrap_content" <br/> Android: textsize = "16sp" <br/> Android: padding = "10dip" <br/> Android: background = "# cc0000" <br/> </textview> <br/> </linearlayout> <br/>

Textactivity. Java

Package COM. example. widgetdemo; </P> <p> Import android. app. activity; </P> <p> Import android. OS. bundle; <br/> Import android. widget. textview; </P> <p> public class textactivity extends activity {<br/>/** called when the activity is first created. */<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> settitle ("viewtextactivity"); <br/> setcontentview (R. layout. text); <br/> find_and_modify_text_view (); <br/>}</P> <p> private void find_and_modify_text_view () {<br/> textview text_view = (textview) findviewbyid (R. id. text_view); <br/> charsequence text_view_old = text_view.gettext (); <br/> text_view.settext ("Modify:" + text_view_old <br/> + "/n it can be modified. "); <br/>}</P> <p>

Running result:

 

 

 

 

 

The specific explanation is as follows:

1. Button introduction and application:

First, put a button on the main interface of the project. You need to set the layout in Main. xml. The specific code is as follows:
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> <button <br/> Android: text = "this is a button" <br/> Android: id = "@ + ID/button" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content"> <br/> </button> <br/> </linearlayout>
Code explanation: As shown in the code above: put a button on the main interface and set its width (layout_width) and height (layout_height) to be adjusted based on the content (wrap_content, set the text displayed above to "this is a button" and Its ID to "button". The running interface is as follows:


The implementation code is as follows:
 Private void find_and_modify_button () {<br/> button = (button) findviewbyid (R. id. button); <br/> button. setonclicklistener (button_listener); <br/>}< br/>

Code explanation: first, obtain the button in the template through the ID, and then use the setonclicklistener () method to set the listener to button_listener. The following also needs to declare this button_listener. The implementation code is as follows:
 Private button. onclicklistener button_listener = new button. onclicklistener () {<br/> @ override <br/> Public void onclick (view V) {<br/> // todo auto-generated method stub <br/> settitle ("click it"); <br/>}< br/>}; <br/>

Code explanation: the above listener code implements a button click event. Here, a settitle is used to set the title to "click it", run the program, and click the button, the following running result is displayed:

 

 

2. introduction and application of textview
Next, add a button in the button example project created above. When this button is clicked, the page for textview is displayed. add a button component in XML and set its ID to text_view_button. The implementation code is as follows:
<Button <br/> Android: text = "textview" <br/> Android: Id = "@ + ID/text_view_button" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content"> <br/> </button> <br/>

Code explanation: put a button on the main interface and set its width (layout_width) and height (layout_height) to be adjusted based on the content (wrap_content, set the text displayed above to "textview" and Its ID to text_view_button.
Then, add a listener and response to the click action in the widgetdemo. When an event is clicked, The textview demo interface is displayed, first, add the code for obtaining the text_view_button in the find_and_modify_button method, as shown below:
Button text_view_button = (button) findviewbyid (R. Id. text_view_button); <br/> text_view_button.setonclicklistener (text_view_button_listener );
Code explanation:
The listener set here is text_view_button_listener. When you click it, you need to open a new interface. The core code is as follows:
 Private button. onclicklistener text_view_button_listener = new button. onclicklistener () {<br/> Public void onclick (view v) {<br/> intent = new intent (); <br/> intent. setclass (widgetdemo. this, textactivity. class); <br/> startactivity (intent); <br/>}< br/>}; <br/>

Code explanation:
When you click this button, you first create an intent, and then call its setclass method to set the ativitity to jump to. The activity to jump here is textactivity, and call startactivity to open this interface.
Textactivity needs to be created, and a textview component is displayed on it. Then, modify the oncreate method and bind it to the specified panel. The implementation code is as follows:
Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> settitle ("viewtextactivity"); <br/> setcontentview (R. layout. text); <br/> find_and_modify_text_view (); <br/>}< br/>

Code explanation: We associate textactivity with text. xml and set its title to viewtextactivity. here we need to create a text. xml file and add a textview to it. The implementation code is as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> <textview <br/> Android: text = "this is textview, you can input anything you want "<br/> Android: Id =" @ + ID/text_view "<br/> Android: layout_width =" wrap_content "<br/> Android: layout_height = "wrap_content" <br/> Android: textsize = "16sp" <br/> Android: padding = "10dip" <br/> Android: background = "# cc0000" <br/> </textview> <br/> </linearlayout> <br/>

Code explanation: here we set some basic attributes, such as font, color, and padding, to indicate the size of gaps around the component, and background to set the background color.
After all this is done, we need to add the newly created textactivity to the <Application> Field of the manifest. xml file. The Code is as follows:
<Activity Android: Name = "textactivity"> </activity>
Finally, run the project and click the viewtext button on the main interface. The following result is displayed:

Extended learning:

 

Textview is generally used when you need to display some information, it cannot be input, but can only be set initially or modified in the program, if you need to dynamically modify this value in the program, then you need to use the Android: ID value. The following is a method to demonstrate the Code:
Private void find_and_modify_text_view () {<br/> textview text_view = (textview) findviewbyid (R. id. text_view); <br/> charsequence text_view_old = text_view.gettext (); <br/> text_view.settext ("Modify:" + text_view_old <br/> + "/n it can be modified. "); <br/>}< br/>

Code explanation:
First, use findviewbyid to put it in main. find the textview of XML, call its own gettext method, extract the original value and save it as text_view_old, and then use settext to modify its own value. The running result is:


It can be seen that it is very intuitive and convenient to modify the value of textview dynamically.

 

Related Article

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.