Some of the Android tips box

Source: Internet
Author: User

1. How to implement a hint when testing

can use

    1. Toast.maketext (This, "It's a hint", toast.length_short). Show ();
    2. Take a hint from the resource file String.xml
    3. Toast.maketext (This, getString (r.string.welcome), Toast.length_short). Show ();

This hint will disappear after a few seconds.

2. Alertdialogcan be used. Builder only generates a prompt box.

such as the MessageBox.

    1. new Alertdialog. Builder (this)
    2. . Settitle ("Android tips")
    3. . Setmessage ("This is a hint, make sure")
    4. . Show ();

With a definite dialog box

  1. New Alertdialog. Builder (this)
  2. . Setmessage ("This is a second hint")
  3. . Setpositivebutton ("OK",
  4. New Dialoginterface.onclicklistener () {
  5. public void OnClick (Dialoginterface dialoginterface, inti) {
  6. //Button event
  7. }
  8. })
  9. . Show ();

Alertdialog. Builder also has a lot of complicated usage, with OK and cancel dialogs

  1. New Alertdialog. Builder (this)
  2. . Settitle ("hint")
  3. . Setmessage ("OK exit?")
  4. . SetIcon (R.drawable.quit)
  5. . Setpositivebutton ("OK", new Dialoginterface.onclicklistener () {
  6. public void OnClick (Dialoginterface dialog, int whichbutton) {
  7. Setresult (RESULT_OK); //OK button event
  8. Finish ();
  9. }
  10. })
  11. . Setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {
  12. public void OnClick (Dialoginterface dialog, int whichbutton) {
  13. //Cancel button event
  14. }
  15. })
  16. . Show ();

The use of 3.menu.

  1. Public static final int item_1_id = Menu.first;
  2. Public static final int item_2_id = Menu.first + 1;
  3. Public static final int item_3_id = Menu.first + 2;
  4. public boolean Oncreateoptionsmenu (Menu menu) {
  5. Super.oncreateoptionsmenu (menu);
  6. Menu with no icon
  7. Menu.add (0, item_1_id, 0, "item-1");
  8. Menu with icon
  9. Menu.add (0, item_2_id, 1, "Item-2"). SetIcon (R.DRAWABLE.EDITBILLS2);
  10. Menu.add (0, item_3_id, 2, "item-3"). SetIcon (R.DRAWABLE.BILLSUM1);
  11. return true;
  12. }
  13. public boolean onoptionsitemselected (MenuItem item) {
  14. switch (Item.getitemid ()) {
  15. Case 1:
  16. Toast.maketext (This, "menu1", Toast.length_short). Show ();
  17. return true;
  18. Case 2:
  19. return true;
  20. Case 3:
  21. return true;
  22. }
  23. return false;
  24. }

4.switching of Activity

2 activity switching, no data transfer

    1. From A to B
    2. Intent Intent = new Intent ();
    3. Intent.setclass (A. This, B.class);
    4. StartActivity (Intent);

Transfer data between 2 activity

Some of the related functions
Startactivityforresult
Public final void Setresult (int resultcode, String data)
callback function

protected void Onactivityresult (int requestcode, int resultcode, Intent data)

For example A to B, get data from B

    1. A to B
    2. static final int rg_request = 0;
    3. Intent Intent = new Intent ();
    4. Intent.setclass (A. This, B.class);
    5. Startactivityforresult (intent,rg_request);
    6. Processing in B
    7. Bundle bundle = new bundle ();
      Bundle.putstring ("DataKey", Edittext.gettext (). toString ());//write data to the bundle
      Intent mintent = new Intent ();
      Mintent.putextras (bundle);
      Setresult (RESULT_OK, mintent);
      Finish ();
    8. Finally, the data is received in the callback function of a.
    9. if (Requestcode = = rg_request) {
      if (ResultCode = = result_canceled)
      Settitle ("Canceled ...");
      else if (ResultCode = = RESULT_OK) {
      Settitle (String) Data.getcharsequenceextra ("DataKey");
      }
      }

Some of the Android tips box

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.