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