简体   繁体   中英

Mixing increase/decrease buttons with Edit Text (numbers)

I have an online android app store and when a user tries to enter a large amount of Quantity he should keep clicking on increase buttons, (you can imagine how hard its if he wants to enter 100 items). So to resolve it I'm trying to add EditText to the increase and decrease buttons, the increase/decrease buttons work well. The problem is: When I enter a value by typing a quantity using EtideText, the value won't pass to the next activity

Layout:

                <LinearLayout
                android:id="@+id/ll_controller"
                android:layout_width="60dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_margin="@dimen/value_3"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:background="@drawable/cart_round_button"
                android:gravity="center"
                android:orientation="vertical"
                android:padding="@dimen/value_3">

                <ImageView
                    android:id="@+id/tvIncrement"
                    android:layout_width="45dp"
                    android:layout_height="45dp"
                    app:srcCompat="@drawable/ic_plus_sign" />


                <EditText
                    android:id="@+id/tvQuantity"
                    style="@style/txt_black_medium"
                    android:layout_width="45dp"
                    android:layout_height="45dp"
                    android:layout_marginTop="@dimen/value_1"
                    android:layout_marginBottom="@dimen/value_1"
                    android:gravity="center"
                    android:text="1"
                    android:textColor="@color/colorPrimary"
                    android:textSize="24sp"
                    android:ems="10"
                    android:inputType="number" />



                <ImageView
                    android:id="@+id/tvDecrement"
                    android:layout_width="45dp"
                    android:layout_height="45dp"
                    app:srcCompat="@drawable/ic_minus_sign" />

            </LinearLayout>

Activity:

        holder.tvQuantity.setTextColor(Color.parseColor(((BaseActivity) activity).getPreferences().getString(Constant.APP_COLOR, Constant.PRIMARY_COLOR)));
        holder.tvQuantity.setText(list.get(position).getQuantity() + "");

        holder.tvIncrement.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int quntity = Integer.parseInt(holder.tvQuantity.getText().toString());
                quntity = quntity + 1;

                if (list.get(position).isManageStock()) {
                    if (quntity > list.get(position).getStockQuantity()) {
                        Toast.makeText(activity, ((BaseActivity) activity).getString(R.string.only) + "" + list.get(position).getStockQuantity() + "" + ((BaseActivity) activity).getString(R.string.quntity_is_avilable), Toast.LENGTH_SHORT).show();
                    } else {

                        holder.tvQuantity.setText(quntity + "");
                        databaseHelper.updateQuantity(quntity, list.get(position).getProductid(), list.get(position).getVariationid() + "");
                        list.get(position).setQuantity(quntity);
                        onItemClickListner.onItemClick(position, RequestParamUtils.increment, quntity);
                    }
                } else {
                    holder.tvQuantity.setText(quntity + "");
                    databaseHelper.updateQuantity(quntity, list.get(position).getProductid(), list.get(position).getVariationid() + "");
                    list.get(position).setQuantity(quntity);
                    onItemClickListner.onItemClick(position, RequestParamUtils.increment, quntity);
                }

            }
        });

        holder.tvDecrement.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int quntity = Integer.parseInt(holder.tvQuantity.getText().toString());
                quntity = quntity - 1;
                if (quntity < 1) {
                    quntity = 1;
                }
                holder.tvQuantity.setText(quntity + "");
                databaseHelper.updateQuantity(quntity, list.get(position).getProductid(), list.get(position).getVariationid() + "");
                list.get(position).setQuantity(quntity);
                onItemClickListner.onItemClick(position, RequestParamUtils.decrement, quntity);
            }
        });
        
    }

}

you should implement it to your grade file

implementation 'com.github.travijuu:numberpicker:1.0.7'

in Xml Layout file.

<com.travijuu.numberpicker.library.NumberPicker
    android:id="@+id/number_picker"
    android:layout_width="130dp"
    android:layout_height="40dp"
    numberpicker:min="0"
    numberpicker:max="10"
    numberpicker:value="-5"
    numberpicker:unit="1"
    numberpicker:focusable="false"
    numberpicker:custom_layout="@layout/number_picker_custom_layout" />   

       

in java File.

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        NumberPicker numberPicker = (NumberPicker) findViewById(R.id.number_picker);
        numberPicker.setMax(15);
        numberPicker.setMin(5);
        numberPicker.setUnit(2);
        numberPicker.setValue(10);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM