1_ requirements for custom digital plus and minus controls
Create Module-numberaddsubview
A_ input can only be a number, and cannot be entered by the keyboard
B_ manipulating numbers with the Add and subtract buttons
C_ Monitor plus minus button
D_ array with minimum and maximum limits
E_ Custom Properties
2. Provide an interface to allow external monitoring to hear the changes in numbers
1_ Setting the interface
@Overridepublic void OnClick (View v) {if (V.getid () = = r.id.btn_sub) { //toast.maketext (mcontext, "minus", toast.length_ Short). Show (); Subnum (); if (onbuttonclicklistenter! = null) {Onbuttonclicklistenter.onbuttonsubclick (V, Value);}} else if (v.getid () = = R.id.btn_add) { //toast.maketext (mcontext, "plus", Toast.length_short). Show (); Addnum (); if ( Onbuttonclicklistenter! = null) {Onbuttonclicklistenter.onbuttonaddclick (V, Value);}}} Public interface Onbuttonclicklistenter {/** * When the Add button is clicked, callback the method * * @param view * @param value */public void ONBUTTONADDCL Ick (view view, int value);/** * When the decrease button is clicked, the callback method * * @param view * @param value */public void Onbuttonsubclick (view view, int value);} Private Onbuttonclicklistenter onbuttonclicklistenter;public void Setonbuttonclicklistenter (OnButtonClickListenter Onbuttonclicklistenter) {this.onbuttonclicklistenter = Onbuttonclicklistenter;}
2_ Monitoring Changes
public class Mainactivity extends Appcompatactivity { private numberaddsubview nb_addsub_view; @Override protected void onCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); Nb_addsub_view = (Numberaddsubview) Findviewbyid (R.id.nb_addsub_view); Nb_addsub_view.setonbuttonclicklistenter (New Numberaddsubview.onbuttonclicklistenter () { @Override public void Onbuttonaddclick (view view, int value) { toast.maketext (mainactivity.this, "AddClick vaule==" +value, Toast.length_short). Show (); } @Override public void Onbuttonsubclick (view view, int value) { toast.maketext (mainactivity.this, "Subclick vaule== "+value,toast.length_short). Show (); }} );} }
GitHub Full code: Https://github.com/ganchuanpu/NumberAddSubView
Custom number plus minus controls