Android input control

Source: Internet
Author: User

Android input control
Today's weather is good, Xiami, to explain the input controls in Android, the input controls in Android are common everywhere. Today, I am writing an article about the input controls in Android to understand their similarities and differences., below are some of the input controls we commonly use in the Android system:
Android supports multiple input controls from users. Common input controls include: Buttons Text Fields Checkboxes Radio Buttons Toggle Buttons Spinners NumberPicker Date and Time Pickers
Android adds an input control to your user interface. It is very easy to add xml elements to the xml layout.
Buttons <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Response + rHt0ru49rC0xaWyv7z + oaO/ydLUsLTPwrC0xaUsu/LV37Xju/cs08nTw7unwLTWtNDQ0ru49rav1/response + response = "@ + id/button_id"
Android: layout_width = "10dp"
Android: layout_height = "8dp"
Android: layout_gravity = "center"
Android: layout_marginRight = "10dp"
Android: layout_weight = "1"
Android: background = "@ drawable/login_input_arrow"/>

 public class MyActivity extends Activity {     protected void onCreate(Bundle icicle) {         super.onCreate(icicle);         setContentView(R.layout.content_layout_id);         final Button button = (Button) findViewById(R.id.button_id);         button.setOnClickListener(new View.OnClickListener() {             public void onClick(View v) {                 // Perform action on click             }         });     } }
Text Fields A text field allows users to input text to your application. It can be one or multiple rows. Touch a text field, move the cursor, and automatically display the keyboard. In addition to typing, text fields allow a variety of other activities, such as text selection (CUT, copy, and paste) and data are automatically searched.
 
Android: inputType: the input type can be Text (string) TextEmailAddress (email address) texturi (URL) Number (number) phone (number) textCansentences () textcapwords () textautocurrect () textpassword () textmultiLine ()
For example, here's how you can collect a postal address, capitalize each word, and disable text suggestions:
 
 

Checkboxs Checkboxs allows you to select one or more checkboxs, which is usually a list in a vertical drop-down box.
 
     
      
  
 
Check whether checkbox is selected in an Activity
public void onCheckboxClicked(View view) {    // Is the view now checked?    boolean checked = ((CheckBox) view).isChecked();        // Check which checkbox was clicked    switch(view.getId()) {        case R.id.checkbox_meat:            if (checked)                // Put some meat on the sandwich            else                // Remove the meat            break;        case R.id.checkbox_cheese:            if (checked)                // Cheese me            else                // I'm lactose intolerant            break;    }}


Radio Butons The single-choice button allows you to select an option from a group. If it is not necessary to display all options side by side, use the fine-tuning control item.

Create each radio button option and create a RadioButton in your layout. However, since single-choice buttons are mutually exclusive, you must group them in the RadioGroup. By grouping them together, you can select the system to ensure that there is only one single-choice button.

 
     
      
  
 
 


public void onRadioButtonClicked(View view) {    // Is the button now checked?    boolean checked = ((RadioButton) view).isChecked();        // Check which radio button was clicked    switch(view.getId()) {        case R.id.radio_pirates:            if (checked)                // Pirates are the best            break;        case R.id.radio_ninjas:            if (checked)                // Ninjas rule            break;    }}

Toggle Buttons The switch button allows you to switch between the two statuses. You can add a basic switch button and Layout Switch button object. Android 4.0 (API Level 14) introduces another switch button, which provides a slider control that you can use to add switch objects. (Switch button) Switch (Android4.0 +) Response to click events
When you select a switch button and switch, the click event received by the object.

To define the Click event handler, add the onClick attribute of the Robot: <切换按钮> Or <开关> Element layout in XML. The value of this attribute must be the name of the method to be called in response to the click event. The layout of the event must be implemented accordingly.


For example, here is a switch button and The onClick attribute of Android:
 
  
Public void onToggleClicked (View view) {// Is the toggle on? Boolean on = (ToggleButton) view). isChecked (); if (on) {// Enable vibrate} else {// Disable vibrate }}
 

Spinners The Spinner provides a quick way to select a value from a group. In the default status, the Inspector displays the selected value. Touch the Spinner and display a drop-down menu with all other available values. You can select a new one. Spinner Layout
Create a spinner like any view and specify the android: entries attribute to specify the set of options:
 
The special string array entries are under res/values/planets_arrays.
 
 
  
   
    Mercury
   
   
    Venus
   
   
    Earth
   
   
    Mars
   
  
 
View more details in the Spinners guide. Note that you need to use the custom array adapter and layout file to customize the text of a fine-tuning control item.
Obtain and set multiple values String str_spinner = spinner. getseletedItem (). toString ();
public void setSpinnerToValue(Spinner spinner, String value) {int index = 0;SpinnerAdapter adapter = spinner.getAdapter();for (int i = 0; i < adapter.getCount(); i++) {if (adapter.getItem(i).equals(value)) {index = i;break; // terminate loop}}spinner.setSelection(index);}
Custom ArrayAdapter Resources

Spinner spinner = (Spinner) findViewById(R.id.spinner);// Create an ArrayAdapter using the string array and a default spinner layoutArrayAdapter
 
   adapter = ArrayAdapter.createFromResource(this,        R.array.planets_array, android.R.layout.simple_spinner_item);// Specify the layout to use when the list of choices appearsadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);// Apply the adapter to the spinnerspinner.setAdapter(adapter);
 
Multiple Select Spinner

By default, the spinner only allows the user to select one option from the list. Check out the following resources surrounding multiple selection spinners:

MultiSelectSpinner-Simple multi-select library
MultiSelect Tutorial 1
MultiSelect Tutorial 2
Simple Multi Dialog

Note that we can also use a ListView in this case instead to avoid a spinner altogether.
NumberPicker
This is a widget that enables the user to select a number from a predefined range. First, we put the NumberPicker within the layout XML:
 


Then we must define the desired range at runtime in the Activity:

public class DemoPickerActivity extends Activity {    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_demo_picker);        NumberPicker numberPicker =             (NumberPicker) findViewById(R.id.np_total);        numberPicker.setMinValue(0);         numberPicker.setMaxValue(100);            numberPicker.setWrapSelectorWheel(true);    }}

Note we set the range with setMinValue and setMaxValue and made the selector wrap withsetWrapSelectorWheel. If you want to listen as the value changes, we can use the OnValueChangeListener listener:
// within onCreatenumberPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {    @Override    public void onValueChange(NumberPicker picker, int oldVal, int newVal) {        Log.d("DEBUG", "Selected number in picker is " + newVal);    }});


You can also call getValue to retrieve the numeric value any time. See theNumberPicker docs for more details.

References Http://developer.android.com/guide/topics/ui/controls.html



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.