Detailed usage of RadioGroup, RadioButton, Spinner, and EditText in android (including sample screenshots and source code)

Source: Internet
Author: User

Detailed usage of RadioGroup, RadioButton, Spinner, and EditText in android (including examples and source code)

 

Today, the commonly used controls such as RadioGroup, RadioButton, Spinner, and EditText in android are used in the project. Here, we will introduce their usage and hope to help our friends in need.

1. Use of RadioGroup and RadioButton

RadioButton is a common radio button. A RadioGroup can contain multiple radio buttons, but only one value can be selected at a time.

Let's take a look at the layout file:

 

 
     
           
   
  
 
Let's take a look at the listener interface of RadioGroup: RadioGroup. OnCheckedChangeListener

 

 

 public interface OnCheckedChangeListener {        /**         * 

Called when the checked radio button has changed. When the * selection is cleared, checkedId is-1.

** @ Param group the group in which the checked radio button has changed * @ param checkedId the unique identifier of the newly checked radio button */public void onCheckedChanged (RadioGroup group, int checkedId);} We need to implement RadioGroup. the OnCheckedChangeListener interface overrides the onCheckedChanged (RadioGroup group, int checkedId) function. The parameter group specifies the RadioGroup for adding listeners, and the parameter checkedId specifies the ID of the selected RadioButton.

 

Ii. Use of Spinner

The Spinner is a drop-down list selection box that can be bound to a data source. The data source can be defined in a program, a string array can be defined in values/strings, and can be queried and obtained in a database.

The layout is simple. For more information, see the following example.

Bind Data source:

 

String[] intervalTime = getResources().getStringArray(R.array.intervalTime);ArrayAdapter
 
   adapter = new ArrayAdapter
  
   (this, android.R.layout.simple_spinner_item, intervalTime);bstIntervalSpinner.setAdapter(adapter);
  
 
Listener setting interface: AdapterView. OnItemSelectedListener

 

 

public interface OnItemSelectedListener {        /**         * 

Callback method to be invoked when an item in this view has been * selected. this callback is invoked only when the newly selected * position is different from the previously selected position or if * there was no selected item.

** Impelmenters can call getItemAtPosition (position) if they need to access the * data associated with the selected item. ** @ param parent The AdapterView where the selection happened * @ param view The view within the AdapterView that was clicked * @ param position The position of the view in the adapter * @ param id row id of the item that is selected */void onItemSelected (AdapterView Parent, View view, int position, long id);/*** Callback method to be invoked when the selection disappears from this * view. the selection can disappear for instance when touch is activated * or when the adapter becomes empty. ** @ param parent The AdapterView that now contains no selected item. */void onNothingSelected (AdapterView Parent);} We need to implement the AdapterView. OnItemSelectedListener interface and override onItemSelected (AdapterView Parent, View view, int position, long id) method.

 

3. Use of EditText

EditText is very common. It is used to input text. Adding a listener to it can monitor input text content in real time, including checking whether there are errors, whether the input complies with the specifications, and whether the length is out of the range.

Add listener:

 

powerEditText.addTextChangedListener(editTextListener);
The listener implements the TextWatcher interface:

 

 

public interface TextWatcher extends NoCopySpan {    /**     * This method is called to notify you that, within s,     * the count characters beginning at start     * are about to be replaced by new text with length after.     * It is an error to attempt to make changes to s from     * this callback.     */    public void beforeTextChanged(CharSequence s, int start,                                  int count, int after);    /**     * This method is called to notify you that, within s,     * the count characters beginning at start     * have just replaced old text that had length before.     * It is an error to attempt to make changes to s from     * this callback.     */    public void onTextChanged(CharSequence s, int start, int before, int count);    /**     * This method is called to notify you that, somewhere within     * s, the text has been changed.     * It is legitimate to make further changes to s from     * this callback, but be careful not to get yourself into an infinite     * loop, because any changes you make will cause this method to be     * called again recursively.     * (You are not told where the change took place because other     * afterTextChanged() methods may already have made other changes     * and invalidated the offsets.  But if you need to know here,     * you can use {@link Spannable#setSpan} in {@link #onTextChanged}     * to mark your place and then look up from here where the span     * ended up.     */    public void afterTextChanged(Editable s);}
We can rewrite beforeTextChanged (CharSequence s, int start, int count, int after), onTextChanged (CharSequence s, int start, int before, int count), afterTextChanged (Editable s) as needed) these three methods.

 

Iv. Example and source code

First:

In, there are 3 radiogroups, 2 Spinner, and 1 EditText. Because the listener is set in the program, the values set in the preceding options are displayed in real time under the split line.

Source code:

Layout file:

 
     
          
           
                
                 
       
      
     
        
   
      
          
           
        
    
   
      
          
           
        
    
   
      
          
           
            
         
     
    
   
      
          
           
                
                 
       
      
     
        
   
      
          
           
                
                 
       
      
     
        
   
      
          
           
            
             
       
        
         
          
         
        
       
      
     
    
   
  
 
Java files:

 

 

Package readAndWriteOBU; import android. app. activity; import android. OS. bundle; import android. text. editable; import android. text. textWatcher; import android. view. view; import android. widget. adapterView; import android. widget. arrayAdapter; import android. widget. editText; import android. widget. radioButton; import android. widget. radioGroup; import android. widget. spinner; import android. widget. textView; import com. Hustxks. etcapp. r; public class InitActivity extends Activity {// ignore the fast response frame (single-byte private RadioGroup quickResponseRadioGroup; private RadioButton groups; private RadioButton noRadioButton; // The BST interval drop-down list is private Spinner bstinterval; // transaction Retry Interval drop-down list private Spinner transIntervalSpinner; // power level edit box private EditText powerEditText; // Channel No. private RadioGroup channelNumRadioGroup; private RadioButton channelRadi OButton1; private RadioButton failed; // CRC verification ticket javasprivate RadioGroup crw.eckradiogroup; private RadioButton failed; private RadioButton crcrcradiobutton2; // whether to ignore the fast response frame flag: 00: completely passthrough public static String ignoreQuickResFlag = 0; // BST interval, in ms, range: 1 ~ 10 mspublic static String bstInterval = 10; // transaction Retry Interval, in ms, range: 1 ~ 10 mspublic static String transRetryInterval = 10; // power series, range: 0 ~ 31 public static String power = 5; // channel number, value: 0, 1 public static String channelID = 0; // CRC verification flag, value: 0, 1 public static String crcCheckFlag = 0; private String [] intervalTime; // display setting result text box TextView resultTextView1; TextView resultTextView2; TextView finished; TextView resultTextView5; TextView resultTextView6; @ Overridepublic void onCreate (Bundle created) {super. onCreate (savedInstanceState); setContentView (R. layout. init_activity); initView (); RadioGroupListener listener = new RadioGroupListener (quickResponseRadioGroup); listener = new listener (channelNumRadioGroup); RadioGroupListener listener = new RadioGroupListener (listener ); editTextListener editTextListener = new EditTextListener (); quickResponseRadioGroup. setOnCheckedChangeListener (quickRadioGroupListener); channelNumRadioGroup. setOnCheckedChangeListener (channelRadioGroupListener); crw.eckradiogroup. setOnCheckedChangeListener (crcRadioGroupListener); powerEditText. addTextChangedListener (editTextListener); intervalTime = getResources (). getStringArray (R. array. intervalTime); ArrayAdapter
 
  
Adapter = new ArrayAdapter
  
   
(This, android. r. layout. simple_spinner_item, intervalTime); bstIntervalSpinner. setAdapter (adapter); transIntervalSpinner. setAdapter (adapter); SpinnerListener bstSpinnerListener = new SpinnerListener (bstIntervalSpinner); bstIntervalSpinner. setOnItemSelectedListener (bstSpinnerListener); SpinnerListener retrySpinnerListener = new SpinnerListener (transIntervalSpinner); transIntervalSpinner. setOnItemSelectedListener (retrySpinnerListener);} private void initView () {quickResponseRadioGroup = (RadioGroup) findViewById (R. id. quickResponseRG); yesRadioButton = (RadioButton) findViewById (R. id. yesRadioButton); noRadioButton = (RadioButton) findViewById (R. id. noRadioButton); bstIntervalSpinner = (Spinner) findViewById (R. id. BSTintervalSpinner); transIntervalSpinner = (Spinner) findViewById (R. id. transRetrySpinner); powerEditText = (EditText) findViewById (R. id. powerEditText); channelNumRadioGroup = (RadioGroup) findViewById (R. id. channelNumRG); channelRadioButton1 = (RadioButton) findViewById (R. id. channelRadioButton1); channelRadioButton2 = (RadioButton) findViewById (R. id. channelRadioButton2); crw.eckradiogroup = (RadioGroup) findViewById (R. id. crcCheckRG); crcRadioButton1 = (RadioButton) findViewById (R. id. crcRadioButton1); crcRadioButton2 = (RadioButton) findViewById (R. id. crcRadioButton2); resultTextView1 = (TextView) findViewById (R. id. result1); resultTextView2 = (TextView) findViewById (R. id. result2); resultTextView3 = (TextView) findViewById (R. id. result3); resultTextView4 = (TextView) findViewById (R. id. result4); resultTextView5 = (TextView) findViewById (R. id. result5); resultTextView6 = (TextView) findViewById (R. id. result6);} // listen to the RadioGroup class RadioGroupListener implements RadioGroup. onCheckedChangeListener {private RadioGroup myRadioGroup; public RadioGroupListener (RadioGroup radioGroup) {// TODO Auto-generated constructor stubthis. myRadioGroup = radioGroup;} @ Overridepublic void onCheckedChanged (RadioGroup group, int checkedId) {// TODO Auto-generated method stubswitch (myRadioGroup. getId () {case R. id. quickResponseRG: if (checkedId = R. id. yesRadioButton) {ignoreQuickResFlag = 00;} else if (checkedId = R. id. noRadioButton) {ignoreQuickResFlag = 01;} resultTextView1.setText (ignoreQuickResFlag); break; case R. id. channelNumRG: if (checkedId = R. id. channelRadioButton1) {channelID = channelRadioButton1.getText (). toString (). trim ();} else if (checkedId = R. id. channelRadioButton2) {channelID = channelRadioButton2.getText (). toString (). trim ();} resultTextView5.setText (channelID); break; case R. id. crcCheckRG: if (checkedId = R. id. crcRadioButton1) {crcCheckFlag = crcrcradiobutton1.gettext (). toString (). trim ();} else if (checkedId = R. id. crcRadioButton2) {crcCheckFlag = crcrcradiobutton2.gettext (). toString (). trim ();} resultTextView6.setText (crcCheckFlag); break; default: break; }}// class EditTextListener implements TextWatcher {@ Overridepublic void beforeTextChanged (charridepublic sequs, int start, int count, int after) {}@ Overridepublic void onTextChanged (CharSequence s, int start, int before, int count) {}@ Overridepublic void afterTextChanged (Editable s) {power = powerEditText. getText (). toString (). trim (); resultTextView4.setText (power) ;}// listens to the class SpinnerListener implements AdapterView of the Spinner. onItemSelectedListener {private Spinner mySpinner; public SpinnerListener (Spinner spinner) {// TODO Auto-generated constructor stubthis. mySpinner = spinner;} @ Overridepublic void onItemSelected (AdapterView
   Parent, View view, int position, long id) {if (mySpinner. getId () = R. id. BSTintervalSpinner) {bstInterval = intervalTime [position]; resultTextView2.setText (bstInterval);} else if (mySpinner. getId () = R. id. transRetrySpinner) {transRetryInterval = intervalTime [position]; resultTextView3.setText (transRetryInterval) ;}@ Overridepublic void onNothingSelected (AdapterView
   Parent ){}}}
  
 
 

 

 

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.