Android textView在按下时删除动态内容

时间:2016-12-08 00:52:19

标签: java android android-layout

我试图实现一个项目计数器,我有两个按钮(+和 - ),在这两个按钮的中间我有一个textView来检查数量,同时增加或降低它,但因为这是一个textView它应该让我介绍数量,例如,如果我单击textView我想能够自己放置值,但是在引入值并单击Ok时,它重新启动到1这是默认值。另一件事是,如果我使用(+和 - )按钮增加值,当我单击textView时它会重置为1并且它不会使用按钮保存我增加的值。

XML代码:

            <EditText
            android:id="@+id/col_etSetQuantity"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|center_horizontal"
            android:paddingLeft="15dip"
            android:paddingRight="15dip"
            android:text="1"
            android:textSize="14dip"
            android:textStyle="bold"
            android:saveEnabled="false"
            android:inputType="number"
 />

这是我对此视图的适配器:

        final EditText edQuantity = (EditText) convertView.findViewById(R.id.col_etSetQuantity);
        int quantity = items.get(position).getQuantity();
        edQuantity.setText(String.valueOf(quantity));

        final TextView tvTotal = (TextView) convertView.findViewById(R.id.col_priceTotal);
        double total = cost * quantity;
        String formattedTotal;
        if(total == 0) {
            formattedTotal = "Total: " + Constants.DOM_CURRENCY + "0";
        } else {
            formattedTotal = String.format("%,.2f", total);
            formattedTotal = "Total: " + Constants.DOM_CURRENCY + formattedTotal;
        }

        tvTotal.setText(formattedTotal);

        Button btLower = (Button) convertView.findViewById(R.id.col_btnLowerQuantity);
        Button btIncrease = (Button) convertView.findViewById(R.id.col_btnIncreaseQuantity);

        itemsEdited.get(position).setQuantity(quantity);
        itemsEdited.get(position).setAmount(total);

        btIncrease.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int quantity = increaseQuantity(position, edQuantity);
                itemsEdited.get(position).setQuantity(quantity);
                double total = setTotalPrice(tvTotal,
                        products.get(position).getUnitPrice(), quantity);
                itemsEdited.get(position).setAmount(total);
            }
        });

        btLower.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int quantity = lowerQuantity(position, edQuantity);
                itemsEdited.get(position).setQuantity(quantity);
                double total = setTotalPrice(tvTotal,
                        products.get(position).getUnitPrice(), quantity);
                itemsEdited.get(position).setAmount(total);
            }
        });
        return convertView;
    }

    /**
     * Se ejecuta cuando se presiona el botón del (-), para disminuir la cantidad de un producto
     */
    public int lowerQuantity(int position, EditText setQuanitityBtn) {
        Integer currentQuantity = Integer.parseInt(setQuanitityBtn.getText().toString());
        int newQuantity = 1;
        if(currentQuantity > 1)
        {
            newQuantity = currentQuantity - 1;
            setQuanitityBtn.setText(String.valueOf(newQuantity));
        }
        return newQuantity;
    }

    /**
     * Se ejecuta cuando se presiona el botón del (+), para aumentar la cantidad de un producto
     */
    public int increaseQuantity(int position, EditText setQuanitityBtn) {
        Integer currentQuantity = Integer.parseInt(setQuanitityBtn.getText().toString());
        int newQuantity = 1;
        if(currentQuantity < products.get(position).getQuantityOnHand())
        {

            newQuantity = currentQuantity + 1;
            setQuanitityBtn.setText(String.valueOf(newQuantity));
        } else {
            Toast t = Toast.makeText(context, "Ya ha seleccionado la cantidad máxima en existencia", Toast.LENGTH_SHORT);
            t.show();
        }
        return newQuantity;
    }

    public double setTotalPrice(TextView column, double cost, int quantity) {
        double total = cost * quantity;
        String formattedTotal = String.format("%,.2f", total);
        formattedTotal = "Total: " + Constants.DOM_CURRENCY + formattedTotal;
        column.setText(formattedTotal);
        return total;
    }

}

1 个答案:

答案 0 :(得分:0)

我相信你需要在EditText上添加一个类似于这个答案的监听器:android on Text Change Listener