Android control radiogroup drop-down menu Mode

Source: Internet
Author: User

Directory

4.6 left or right

Radiogroup and oncheckedchanged events

 

4.8 customize the drop-down menu Mode

Spinner and setdropdownviewresource

4.9 Add/delete a menu on the fly

4.6 left or right

Radiogroup and oncheckedchanged events

Example

Next we will introduce the radiogroup group events. Radiogroup can set different radiobutton to the same radio button group, which belongs to the same radiogroup and can only make a single choice (single answer). Although the previous chapter has introduced radiogroup and radiobutton, however, the button event was used at the time. Here we need to demonstrate that the "click" operation will run the event processing at the same time, without the assistance of the button.

First, design a textview widget and a radiogroup, and place two radiobutton in the radiogroup. By default, no radiobutton is selected. During the program running stage, oncheckedchanged is used as the startup event device, when a user selects a button, the selected content is displayed, and the option text of radiobutton is displayed in textview.

Sample program

Src/IRDC. ex04_06/ex04_06.java

Use oncheckedchangelistener to start the radiogroup event, and then display the selected radiobutton (mradio1.gettext () text in textview.

/* Import program omitted */

Public class ex04_06 extends Activity

{

Public textview mtextview1;

Public radiogroup mradiogroup1;

Public radiobutton mradio1, mradio2;

/** Called when the activity is first created .*/

@ Override

Public void oncreate (bundle savedinstancestate)

{

Super. oncreate (savedinstancestate );

Setcontentview (R. layout. Main );

/* Obtain textview, radiogroup, and radiobutton objects */

Mtextview1 = (textview) findviewbyid (R. Id. mytextview );

Mradiogroup1 = (radiogroup) findviewbyid (R. Id. myradiogroup );

Mradio1 = (radiobutton) findviewbyid (R. Id. myradiobutton1 );

Mradio2 = (radiobutton) findviewbyid (R. Id. myradiobutton2 );

/* Radiogroup runs with oncheckedchangelistener */

Mradiogroup1.setoncheckedchangelistener (mchangeradio );

}

Private radiogroup. oncheckedchangelistener mchangeradio = new

Radiogroup. oncheckedchangelistener ()

{

@ Override

Public void oncheckedchanged (radiogroup group, int checkedid)

{

// Todo auto-generated method stub

If (checkedid = mradio1.getid ())

{

/* Upload the content of mradio1 to mtextview1 */

Mtextview1.settext (Mradio1.gettext ());

}

Else if (checkedid = mradio2.getid ())

{

/* Upload the content of mradio2 to mtextview1 */

Mtextview1.settext (Mradio2.gettext ());

}

}

};

}

Extended learning

In extended learning, add two buttons, one for the answer and the other for clearing the selection status of radiobutton. The program has random answer options. When the user clicks the "Answer" button, the answer is correct. If yes, the answer result is displayed in the alertdialog dialog window.

Answerbutton. setonclicklistener (New button. onclicklistener ()

{

Public void onclick (view V)

{

New alertdialog. Builder (test_56.this)

. Seticon (R. drawable. Icon)

. Settitle (R. String. about_dialog_title)

. Setpositivebutton (R. String. about_dialog_ OK, null)

. Setmessage (R. String. about_dialog_thanks)

. Create ();

{

}

}). Show ();

}

In event processing to clear the button. onclicklistener, you only need to cancel the selected radiobutton and return to the waiting status.

Mradiogroup1.clearcheck ();

 

4.8 customize the drop-down menu Mode

Spinner and setdropdownviewresource

Example

The spinner is the drop-down menu, which is equivalent to the swing combo box and HTML <SELECT>. Due to limited mobile phone screen, you must select a project within a limited range, the drop-down menu is unique and a good choice.

The drop-down menu of the spinner widget provided by Android is very easy to use and the style is also applicable. However, the focus of this example is to customize the style in the drop-down menu. The key is to call the setdropdownviewresource method to define the display style of the drop-down menu in XML format. In addition to the custom drop-down menu, this example also uses a program to design an animation. When the user clicks the custom spinner in touch mode, the user will be prompted with an animation.

Sample program

Src/IRDC. ex04_08/ex04_08.java

In new arrayadapter, we will use the constructor arrayadapter (context, int textviewresourceid, t [] objects). textviewresourceid uses the resourceid provided by Android, objects is a required String Array (String Array ).

Setdropdownviewresource of the adapter allows you to set the display mode of the drop-down menu, define the XML in the Res/layout directory, and set the textview in the drop-down menu, as in the R. layout. myspinner _ dropdown is the custom textview style of the drop-down menu. In addition to changing the drop-down menu style, it also performs a dynamic effect on the spinner. When you click the spinner, shake the spinner and then display the drop-down menu (myanimation ).

/* Import program omitted */

Public class ex04_08 extends Activity

{

Private Static final string [] countriesstr =

{"Beijing", "Shanghai", "Tianjin", "Chongqing "};

Private textview mytextview;

Private spinner myspinner;

Private arrayadapter <string> adapter;

Animation myanimation;

/** Called when the activity is first created .*/

@ Override

Public void oncreate (bundle savedinstancestate)

{

Super. oncreate (savedinstancestate );

/* Load main. xml Layout */

Setcontentview (R. layout. Main );

/* Get the object with findviewbyid */

Mytextview = (textview) findviewbyid (R. Id. mytextview );

Myspinner = (spinner) findviewbyid (R. Id. myspinner );

Adapter = new arrayadapter <string> (this,

Android. R. layout. simple_spinner_item, countriesstr );

/* Myspinner_dropdown: Customize the drop-down menu style in the Res/layout directory */

Adapter. setdropdownviewresource (R. layout. myspinner_dropdown );

/* Add the arrayadapter to the spinner object */

Myspinner. setadapter (adapter );

/* Add onitemselectedlistener to myspinner */

Myspinner. setonitemselectedlistener

(New Spinner. onitemselectedlistener ()

{

@ Override

Public void onitemselected

(Adapterview <?> Arg0, view arg1, int arg2,

Long arg3)

{

/* Bring the value of the selected myspinner to mytextview */

Mytextview. settext ("Select"+ Countriesstr [arg2]);

/* Display myspinner */

Arg0.setvisibility (view. Visible );

}

@ Override

Public void onnothingselected (adapterview <?> Arg0)

{

// Todo auto-generated method stub

}

});

/* Obtain the animation defined in the Res/anim directory */

Myanimation = animationutils. loadanimation (this, R. anim. my_anim );

/* Add the ontouchlistener to myspinner */

Myspinner. setontouchlistener (new Spinner. ontouchlistener ()

{

@ Override

Public Boolean ontouch (view V, motionevent event)

{

/* Run animation on myspinner */

V. startanimation (myanimation );

/* Hide myspinner */

V. setvisibility (view. Invisible );

Return false;

}

});

Myspinner. setonfocuschangelistener (new Spinner. onfocuschangelistener ()

{

@ Override

Public void onfocuschange (view V, Boolean hasfocus)

{

// Todo auto-generated method stub

}

});

}

}

Res/layout/myspinner_dropdown.xml

Change the XML format of the drop-down menu. The component used in the drop-down menu is textview.

<? XML version = "1.0" encoding = "UTF-8"?>

<Textview

Xmlns: Android = "http://schemas.android.com/apk/res/android"

Android: Id = "@ + ID/text1"

Android: layout_width = "wrap_content"

Android: layout_height = "24sp"

Android: singleline = "true"

Style = "? Android: ATTR/spinnerdropdownitemstyle "/>

Res/anim/my_anim.xml

Android animations are composed of four types: Alpha, scale, translate, and rotate. The following custom animations use either of them.

<? XML version = "1.0" encoding = "UTF-8"?>

<Set

Xmlns: Android = "http://schemas.android.com/apk/res/android">

<Translate

Android: fromxdelta = "0"

Android: toxdelta = "-100% P"

Android: duration= "300"

>

</Translate>

<Alpha

Android: fromalphi = "1.0"

Android: toalpha = "0.0"

Android: Duration = "300">

</Alpha>

</Set>

Extended learning

Animation mainly has two dynamic modes: tweened animation (gradient animation) and frame by frame animation (screen conversion animation ). Tweened animation has the following four basic conversion methods.

· Alphaanimation (Transparency changes): Transparency conversion.

· Rotateanimation (rotations): rotation conversion.

· Scaleanimation (growing or shrinking): Scaling and conversion.

· Translateanimation (position changes): Location conversion.

After defining the animation XML you want, use animationutils. loadanimation to load the animation and try to use the startanimation method in the component that wants to add dynamic effects.

 

4.9 Add/delete a menu on the fly

Example

The previous example has a rough idea about the design method of the custom menu and interactive events of the spinner. However, to dynamically increase or decrease the menu options of the spinner drop-down menu in the Android system, the arraylist dependency must be used.

In the following example, an edittext is designed. When the user inputs a new text, when the "add" button is clicked, the input value is added to the spinner (to the last entry in the drop-down menu), and the spinner stays on the added option. When you click the delete button, delete the selected spinner option, which is often applied to the to-do list with an unknown number of spinner options or to add and maintain city and county data.

<! -- [Endif] -->

▲Figure 4-9 a menu with user input that can be dynamically added/deleted

Sample program

Src/IRDC. ex04_09/ex04_09.java

The onitemselectedlistener event is added to the spinner. When you click the drop-down menu, the value is taken to the text-view on the top. In the previous example, a string array is input when the new adapter is added. This time, arraylist is required because the adapter is to be added and deleted. Otherwise, an error occurs when adding or deleting the adapter.

/* Import program omitted */

Public class ex04_09 extends Activity

{

Private Static final string [] countriesstr =

{"Beijing", "Shanghai", "Tianjin", "Chongqing "};

Private textview mytextview;

Private edittext myedittext;

Private button mybutton_add;

Private button mybutton_remove;

Private spinner myspinner;

Private arrayadapter <string> adapter;

Private list <string> allcountries;

/** Called when the activity is first created .*/

@ Override

Public void oncreate (bundle savedinstancestate)

{

Super. oncreate (savedinstancestate );

/* Load main. xml Layout */

Setcontentview (R. layout. Main );

Allcountries = newArraylist<String> ();

For (INT I = 0; I <countriesstr. length; I ++)

{

Allcountries. Add (countriesstr [I]);

}

/* New arrayadapter object and pass allcountries */

Adapter = new arrayadapter <string> (this,

Android. R. layout. simple_spinner_item, allcountries );

Adapter

. Setdropdownviewresource

(Android. R. layout. simple_spinner_dropdown_item );

/* Get the object with findviewbyid */

Mytextview = (textview) findviewbyid (R. Id. mytextview );

Myedittext = (edittext) findviewbyid (R. Id. myedittext );

Mybutton_add = (button) findviewbyid (R. Id. mybutton_add );

Mybutton_remove = (button) findviewbyid (R. Id. mybutton_remove );

Myspinner = (spinner) findviewbyid (R. Id. myspinner );

/* Add the arrayadapter to the spinner object */

Myspinner. setadapter (adapter );

/* Add onclicklistener to mybutton_add */

Mybutton_add.setonclicklistener (New button. onclicklistener ()

{

@ Override

Public void onclick (view arg0)

{

String newcountry = myedittext. gettext (). tostring ();

/* Compare whether the added value already exists and does not exist before adding it */

For (INT I = 0; I <Adapter. getcount (); I ++)

{

If (newcountry. Equals (adapter. getitem (I )))

{

Return;

}

}

If (! Newcountry. Equals (""))

{

/* Add the value to the adapter */

Adapter. Add (newcountry );

/* Obtain the position of the added value */

Int position =Adapter. getposition (newcountry);

/* Select the position of the spinner in the added value */

Myspinner. setselection (position );

/* Clear myedittext */

Myedittext. settext ("");

}

}

});

/* Add onclicklistener to mybutton_remove */

Mybutton_remove.setonclicklistener (New button. onclicklistener ()

{

@ Override

Public void onclick (view arg0)

{

If (myspinner. getselecteditem ()! = NULL)

{

/* Delete the value of myspinner */

Adapter. Remove (myspinner. getselecteditem (). tostring ());

/* Clear myedittext */

Myedittext. settext ("");

If (adapter. getcount () = 0)

{

/* Clear mytextview */

Mytextview. settext ("");

}

}

}

});

/* Add onitemselectedlistener to myspinner */

Myspinner. setonitemselectedlistener

(New Spinner. onitemselectedlistener ()

{

@ Override

Public void onitemselected (adapterview <?> Arg0, view arg1, int arg2,

Long arg3)

{

/* Bring the value of the selected myspinner to mytextview */

Mytextview. settext (Arg0.getselecteditem (). tostring ());

}

@ Override

Public void onnothingselected (adapterview <?> Arg0)

{

}

});

}

}

Extended learning

Setdropdownviewresource is mainly used to set the drop-down menu style that appears after the user clicks the spinner. In addition to the previous example, the custom mode is used to change the textview content, Android also provides two basic styles:

· Android. R. layout. simple_spinner_item: drop-down menu of textview;

· Android. R. layout. simple_spinner_dropdown_item: In addition to textview, there is also a radio drop-down menu on the right.

View simple_spinner_dropdown_item.xml In the android source code. The content is as follows:

<? XML version = "1.0" encoding = "UTF-8"?>

<Textview

Xmlns: Android = "http://schemas.android.com/apk/res/android"

Android: Id = "@ Android: ID/text1"

Android: layout_width = "fill_parent"

Android: layout_height = "? Android: ATTR/listpreferreditemheight"

Android: singleline = "true"

Style = "? Android: ATTR/spinnerdropdownitemstyle"

/>

After the custom modifications are made, layout applies to the spinner:

<? XML version = "1.0" encoding = "UTF-8"?>

<Textview

Xmlns: Android = "http://schemas.android.com/apk/res/android"

Android: Id = "@ Android: ID/text1"

Android: layout_width = "fill_parent"

Android: layout_height = "12sp"

Android: singleline = "true"

Style = "? Android: ATTR/spinnerdropdownitemstyle"

Android: textsize = "10sp"

/>

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.