"Side do project side learn Android" mobile Security defender 05_2: Program main interface, add events for each entry

Source: Internet
Author: User
Tags event listener

Add a click event listener for each entry

Gv_main.setonitemclicklistener (this);

The current activity is required to implement the Onitemclicklistener interface while implementing the public void Onitemclick (adapterview<?> parent, view view, int position, Long id) method

/**     * When the GridView entry is clicked the corresponding callback     * Parent:gridview     * View: The currently clicked Entry LinearLayout     * Position: Click on the entry corresponding to the location 
   * ID: The line number represented by     *    /@Override public    void Onitemclick (adapterview<?> parent, view view, int position,< C8/>long ID) {        log.i (TAG, "click position" + position);        Switch (position) {case        0:            log.i (TAG, "Enter mobile phone anti-theft");            break;        }    }


When setting long press "phone anti-theft", pop up the edit window (Knowledge point: Sharedpreferences)
Gv_main.setonitemlongclicklistener (New Onitemlongclicklistener () {@Override public boolean Onitemlo Ngclick (adapterview<?> parent, final view view, int position, long id) {if (Positi                    on = = 0) {Builder builder = new Builder (mainactivity.this);                    Builder.settitle ("Settings");                                       Builder.setmessage ("Please enter what you want to change");                    Final EditText et = new EditText (mainactivity.this);                    Et.sethint ("Please input content, length between 0-8");                                       Builder.setview (ET);                        Builder.setpositivebutton ("OK", new Onclicklistener () {@Override public void OnClick (Dialoginterface dialog, int which) {String name = Et.gettext ( ). toString ();//Gets the input if ("". Equals (name)) {Toast.maketext (GetappLicationcontext (), "Content cannot be empty", Toast.length_long). Show ();                            Return }else if (Name.length () > 8) {toast.maketext (Getapplicationcontext (), "input too long", toast.lengt                                H_long). Show ();                            Return                                }else{Editor editor = Sp.edit ();                                Editor.putstring ("Lost_name", name);                                Complete the submission of data editor.commit ();                                TextView TV = (TextView) View.findviewbyid (r.id.tv_main_name);                            Tv.settext (name);                                       }                        }                    });                        Builder.setnegativebutton ("Cancel", new Onclicklistener () {@Override  public void OnClick (Dialoginterface dialog, int which) {                      }                    });                Builder.create (). Show ();            } return false;           }                   }); }


After long press the effect of pop-up interface and after setting:

"Side do project side learn Android" mobile Security defender 05_2: Program main interface, add events for each entry

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.