Android basic entry control (4)

Source: Internet
Author: User

I. comprehensive use of buttons, TextView, EditText, RadioButton, RadioGroup, and CheckBox:

Layout file:

Activity_main.xml:

     
      
      
          
           
           
               
      
      
      
      
  
 


Strings. xml:

 
     
  
   
HelloWorld
      
  
   
Settings
      
  
   
Word concatenation
      
  
   
Submit
      
  
   
Name
      
  
   
Stars
      
  
   
Moon
      
  
   
Sun
      
  
   
Days
      
  
   
Location
      
  
   
Sea
  
 

MainActivity. java:

Package com. example. helloworld; import android. app. activity; import android. OS. bundle; import android. view. view; import android. widget. button; import android. widget. checkBox; import android. widget. compoundButton; import android. widget. compoundButton. onCheckedChangeListener; import android. widget. editText; import android. widget. radioButton; import android. widget. radioGroup; import android. widget. toast; public c Lass MainActivity extends Activity {// used to enter the name private EditText name; // create the RadioGroup object private RadioGroup rg; // create three RadioButton objects private RadioButton sun, moon, and stars; // create three CheckBox objects private CheckBox day, theearth, sea; // create the submit Button private Button submit; // The text String text = ""; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main );// The findViewById () method is used to obtain the control in the layout file and obtain the control by Id. Name = (EditText) findViewById (R. id. name); rg = (RadioGroup) findViewById (R. id. radioGroup); sun = (RadioButton) findViewById (R. id. sun); moon = (RadioButton) findViewById (R. id. moon); stars = (RadioButton) findViewById (R. id. stars); day = (CheckBox) findViewById (R. id. day); theearth = (CheckBox) findViewById (R. id. theearth); sea = (CheckBox) findViewById (R. id. sea); submit = (Button) findViewById (R. id. submit); // select Add event for a single item. Rg. setOnCheckedChangeListener (new RadioGroup. onCheckedChangeListener () {@ Overridepublic void onCheckedChanged (RadioGroup group, int checkedId) {if (checkedId = sun. getId () {text + = sun. getText (). toString ();} else if (checkedId = moon. getId () {text + = moon. getText (). toString ();} else {text + = stars. getText (). toString () ;}}); // write only one event for multiple selections. The two can be filled in according to your needs. Day. setOnCheckedChangeListener (new OnCheckedChangeListener () {@ Overridepublic void onCheckedChanged (CompoundButton buttonView, boolean isChecked) {if (day. isChecked () {displayToast ("You like" + day. getText () ;}}); // click the Add button to display the selected content with toast. Submit. setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {String str = "like"; if (day. isChecked () {str + = day. getText () + ",";} if (theearth. isChecked () {str + = theearth. getText () + ",";} if (sea. isChecked () {str + = sea. getText () + ",";} displayToast (name. getText (). toString () + str + text) ;}}) ;}// toast, information prompt. Public void displayToast (String text) {Toast. makeText (getApplicationContext (), text, Toast. LENGTH_SHORT). show ();}}


Running image:

When multiple selections are selected, the toast information will pop up.


When you enter the name and select a single item, click the submit button to trigger the event and prompt you for the selected information.



2. Click the event:

There are three common handling methods for click events:

1) Anonymous internal class as event listening:

Implementation:

Button:

 

Code:

Button button; button = (Button) findViewById (R. id. click); button. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {displayToast ("click ");}});

Running image:



2). Internal class as listener:

Button:

 

Code:

Public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); Button button; button = (Button) findViewById (R. id. click); button. setOnClickListener (new ButtonOnClick ();} private final class ButtonOnClick implements OnClickListener {@ Overridepublic void onClick (View v) {displayToast ("click ");}} public void displayToast (String text) {Toast. makeText (getApplicationContext (), text, Toast. LENGTH_SHORT ). show () ;}@ 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 ;}}

Running image:




3). Specify the listening method in the control:

Android: onClick = "specify method name"

Button:

 

Code:

Public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); Button button; button = (Button) findViewById (R. id. click);} // The method name and parameter name must be in this format and cannot be modified. Public void onClick (View v) {displayToast ("I have already clicked the button");} public void displayToast (String text) {Toast. makeText (getApplicationContext (), text, Toast. LENGTH_SHORT ). show () ;}@ 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 ;}}
Running Image



There are other event listening methods. For details, refer.

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.