In the purchase of goods, you can customize the number control, to determine the actual number of goods purchased, how to implement this control, please refer to the following:
1. Customize the requirements of the number plus and minus controls
Create Module-numberaddsubview
A_ input can only be a number, and can not enter through the keyboard
B_ the number by adding and subtraction buttons
C_ Monitor plus minus button
The D_ array has a minimum and maximum limit
E_ Custom Properties
2. Provide the interface, let the outside monitor the change of the number
1_ Settings interface
@Override public void OnClick (View v) {if (V.getid () = = R.id.btn_sub) {//toast.maketext (M
Context, "minus", Toast.length_short). Show ();
Subnum ();
if (onbuttonclicklistenter!= null) {Onbuttonclicklistenter.onbuttonsubclick (V, value);
} else if (v.getid () = = R.id.btn_add) {//toast.maketext (Mcontext, add, Toast.length_short). Show ();
Addnum ();
if (onbuttonclicklistenter!= null) {Onbuttonclicklistenter.onbuttonaddclick (V, value); }} public interface Onbuttonclicklistenter {/** * callback This method when the Add button is clicked * * @param view * @param value/PU
Blic void Onbuttonaddclick (view view, int value);
/** * Callback This method when the reduction button is clicked * * @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 Complete code: Https://github.com/ganchuanpu/NumberAddSubView
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.