Android tab, timer, Radio button, Check button

Source: Internet
Author: User
<span id="Label3"></p><p><p>Write a small program to concatenate the basic usage of several basic components in the Android Program.</p></p><p><p>As shown in the Following:</p></p><p><p><br></p></p><p><p>In the Android program, a timer is constantly ticking, popping a hint every 10 seconds.</p></p><p><p>Mainactivity is divided into two parts by a tab, a radio button and a check button, and a Submit button,</p></p><p><p>The results are displayed in another tab.</p></p><p><p>Use this program to illustrate the use of wirelessly tabs, timers, radio buttons, and check Buttons.</p></p><p><p>first, paste the strings of each component in the Res\values\string.xml.</p></p><p><p></p></p><pre name="code" class="html"><pre name="code" class="html"><?xml version= "1.0" encoding= "utf-8"?><resources> <string name= "app_name" > Tabs and basic Components </ string> <string name= "action_settings" >Settings</string> <string name= "tab1_ Radiogroup1_radiobutton1 "> Radio Option 1</string> <string name=" tab1_radiogroup1_radiobutton2 "> Radio Option 2 </string> <string name= "tab1_checkbox1" > Check options 1</string> <string name= "tab1_checkbox2 > Check Option 2</string> <string name= "tab1_button1" > Submit </string> <string name= "tab2_ TextView1 "> Label Page 2</string></resources></pre></pre>The whole program of components, has basically been able to glimpse twos,<p><p></p></p><p><p>The title of the tab page "component", "result" is set in Java in a Moment.</p></p><p><p>At the same time, the label text TextView on the Results tab is "label 2" by default before the Submit button is pressed on the Components tab page.</p></p><p><p>1. First create a new two XML in the Res\layout folder, one for tab1.xml, and one for Tab2.xml as Shown:</p></p><p><p><br></p></p><p><p>If you decide to use a tab in an activity, the basic layout file of your activity, here mainactivity default is Activity_main.xml is dead, must be written like this:</p></p><p><p></p></p><pre name="code" class="html"><pre name="code" class="html"><?xml version= "1.0" encoding= "utf-8"? ><tabhost xmlns:android= "http://schemas.android.com/apk/res/android " android:id=" @android: id/tabhost " android:layout_width=" fill_parent " android:layout_height=" Fill_ Parent "> <linearlayout android:layout_width=" fill_parent " android:layout_height=" fill_parent " android:orientation=" Vertical "> <tabwidget android:id=" @android: id/tabs " android: Layout_width= "fill_parent" android:layout_height= "wrap_content" /> <framelayout android:id= "@android: id/tabcontent" android:layout_width= "fill_parent" android:layout_height= "fill_ Parent "/> </LinearLayout></TabHost></pre></pre>Here you can obviously notice, the way to set the ID here, and usually we define the Android component of the situation is different, where the ID is not android:id= "@+id/xx" in the middle without a plus. This is because this tab component is a fixed constant in the system, tabhost, tabs These ids, must not be renamed, the equivalent of KEYWORDS.<p><p></p></p><p><p>2. After that, the layout of the "components" tab is written tab1.xml.</p></p><p><p>It's simple here, with a basic linear layout, where the components are arranged vertically, a timer, a radio button group, a check button x2, a button in the Components tab, and an ID assigned to the component to be controlled by Java.</p></p><p><p>It is also very important to give the ID to the overall linear layout, and for a moment to declare it in mainactivity.java, label 1 loads the Layout.</p></p><p><p></p></p><pre name="code" class="html"><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:id= "@+id/linearlayout1" Android:layout_width= "match_parent" android:layout_height= "match_parent" android:orientation= "vertical" > < !--timer--<chronometer android:id= "@+id/chronometer1" android:layout_width= "match_parent" android:layout_height= "wrap_content" android:textsize= "24sp"/> <!--radio button Group It is important to note that the radio button group must be controlled for radio button groups, individual radio buttons Give the appropriate id, or the program will error-<radiogroup android:id= "@+id/tab1_radiogroup1" android:layout_width= "wrap_c Ontent "android:layout_height=" wrap_content "android:orientation=" Horizontal "> <radiobutton android:id= "@+id/tab1_radiogroup1_radiobutton1" android:layout_width= "wrap_content" Android : layout_height= "wrap_content" android:checked= "true" android:text= "@string/tab1_radiogroup1_radiobut Ton1 "android:textsize= "24sp"/> <radiobutton android:id= "@+id/tab1_radiogroup1_radiobutton2" android:layout _width= "wrap_content" android:layout_height= "wrap_content" android:text= "@string/tab1_radiogroup1_ra DioButton2 "android:textsize=" 24sp "/> </RadioGroup> <!--check Box X2--<checkbox android:id= "@+id/tab1_checkbox1" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "@string/tab1_checkbox1" android:textsize= "24sp"/> <checkbox android:id= "@+id/t Ab1_checkbox2 "android:layout_width=" wrap_content "android:layout_height=" wrap_content "android:text= "@string/tab1_checkbox2" android:textsize= "24sp"/> <!--button-<button android:id= "@+id/t Ab1_button1 "android:layout_width=" match_parent "android:layout_height=" wrap_content "android:text=" @ String/tab1_button1 " Android:textsize= "24sp"/></linearlayout> </pre>3, The final is the "result" tab layout Tab2.xml writing. It's very simple here, placing a label text with an ID textview ok.<br><pre name="code" class="html"><pre name="code" class="html"><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:id= "@+id/linearlayout2" android:layout_width= "match_parent" android:layout_height= "match_parent" android:orientation= " Vertical "> <textview android:id=" @+id/tab2_textview1 " android:layout_width=" wrap_content " android:layout_height= "wrap_content" android:text= "@string/tab2_textview1" android:textsize= "24sp "/></linearlayout></pre></pre>4, layout finished, began to mainactivity.java this file to Write. Although a tabbed layout is used, this Java file is able to control individual components in multiple tabs at Once.<p><p></p></p><p><p>The OnCreate method in the program is divided into three parts, one is the initialization of the tab page, One is the initialization of the timer, and the other is a button click Event. There's nothing special about That.</p></p><p><p></p></p><pre name="code" class="java">Package Com.tabcomponent;import Android.os.bundle;import Android.app.activity;import android.view.LayoutInflater; Import Android.view.menu;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.checkbox;import Android.widget.chronometer;import Android.widget.chronometer.onchronometerticklistener;import Android.widget.radiobutton;import Android.widget.radiogroup;import Android.widget.tabhost;import Android.widget.textview;import Android.widget.toast;public class Mainactivity extends Activity {//declare the component to be controlled private tabhost tabhost;private chronometer Chronometer;private radiogroup tab1_radiogroup1;private checkbox tab1_checkbox1;private checkbox tab1_CheckBox2; Private Button tab1_button1;private TextView tab2_textview1, @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);// TAB initialization begins Tabhost = (tabhost) Findviewbyid (android. r.id.tabhost); Tabhost.setup (); Layoutinflater layoutinflater = layoutinflater.from (this); layoutinflater.inflate (r.layout.tab1, Tabhost.gettabcontentview ()); layoutinflater.inflate (r.layout.tab2, tabhost.gettabcontentview ()); tabHost.addTab ( Tabhost.newtabspec (""). setindicator ("component"). setcontent (r.id.linearlayout1));//set label 1 is titled "component" and the Layout is LinearLayout1, Tabhost.addtab (tabhost.newtabspec (""). setindicator ("result"). setcontent (r.id.linearlayout2));//tab initialization end// Timer initialization starts chronometer = (chronometer) Findviewbyid (r.id.chronometer1); chronometer.setformat ("program has run:%s");//% S is the time of constant jumping, its format is 00:00, accurate to the second Chronometer.start ();//start timing, default does not start Chronometer//timer listener events, the timer every change to trigger once, This is triggered once every 1 seconds. setonchronometerticklistener (new onchronometerticklistener () {int i = 0;//this i, since the program was run, every second in the self-increment public void Onchronometertick (chronometer chronometer) {i++;if (i 10 = = 0) {//every 10 seconds, pop up a toast hint toast.maketext (mainactivity.this , "the program has started" + i + "seconds", toast.length_long). show ();}}); /timer End//button 1 Click event start Tab1_button1 = (button) Findviewbyid (r.id.tab1_button1); Tab1_buttOn1.setonclicklistener (new Onclicklistener () {//for button1 Add click event @overridepublic void OnClick (View v) {String str = "";//received The value of the radio button group Tab1_radiogroup1 = (radiogroup) Findviewbyid (r.id.tab1_radiogroup1); for (int i = 0; i < tab1_ Radiogroup1.getchildcount (); I++) {RadioButton RadioButton = (RadioButton) tab1_radiogroup1.getchildat (i); if (radiobutton.ischecked ()) {str + = Radiobutton.gettext () + "selected,"; break;}} Gets whether each check box is selected Tab1_checkbox1 = (CheckBox) Findviewbyid (r.id.tab1_checkbox1); if (tab1_checkbox1.ischecked ()) {str + = Tab1_checkbox1.gettext () + "selected,";} Tab1_checkbox2 = (CheckBox) Findviewbyid (r.id.tab1_checkbox2), if (tab1_checkbox2.ischecked ()) {str + = tab1_ Checkbox2.gettext () + "selected,";} Tab2_textview1 = (TextView) Findviewbyid (r.id.tab2_textview1); tab2_textview1.settext (str);}); /button 1 Click event end} @Overridepublic boolean oncreateoptionsmenu (menu menu) {//inflate The menu; this adds items to the action Bar If it is present.getmenuinflater (). inflate (r.menu.main, menu); return true;}}</pre>This completes the entire Android Program.<p><p></p></p><p><p>Android tab, timer, Radio button, Check button</p></p></span>

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.