简体   繁体   English

在edittext旁边添加+和-按钮的Android java代码

[英]Android java code to add + and - button beside edittext

 for(int i=0;i<j.size();i++)
        {

            TableLayout.LayoutParams tableRowParams=
                    new TableLayout.LayoutParams
                            (TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);


            JsonObject jb = (JsonObject) j.get(i);
            String item = jb.get("item").getAsString();
            String unit = jb.get("unit").getAsString();;
            String price = jb.get("price").getAsString();;

            TableRow tbrow1 = new TableRow(this);

            TextView tv01 = new TextView(this);
            tv01.setText(item);
            tv01.setTextColor(Color.BLACK);
            tbrow1.addView(tv01);

            TextView tv11 = new TextView(this);
            tv11.setText(unit);
            tv11.setTextColor(Color.BLACK);
            tbrow1.addView(tv11);

            TextView tv21 = new TextView(this);
            tv21.setText(price);
            tv21.setTextColor(Color.BLACK);
            tbrow1.addView(tv21);

            Button button = new Button(this);
            button.setText("+");
            button.setTag(item);
            tbrow1.addView(button);
            final String id_ = (String) button.getTag();


            EditText edText = new EditText(this);
            edText.setInputType(InputType.TYPE_CLASS_NUMBER);
            //edText.setText(0);
            tbrow1.addView(edText);


            Button button1 = new Button(this);
            button1.setText("-");
            button.setTag(item);
            tbrow1.addView(button1);
            final String id1_ = (String) button.getTag();



            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Toast.makeText(getBaseContext(), "button clicked for  "+id_, Toast.LENGTH_LONG).show();


                }
            });



            button1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getBaseContext(), "button clicked for  "+id1_, Toast.LENGTH_LONG).show();
                }
            })
            }

I'm developing Android app for grocery shop .我正在为杂货店开发 Android 应用程序。 I'm getting itms details in json Depending on items number I'm creating dynamic rows.我在 json 中获取 itms 详细信息根据项目编号我正在创建动态行。 Now I want to add + and - button in each row .现在我想在每一行中添加 + 和 - 按钮。 I want it programmatically How to to do it.我想要以编程方式如何去做。 Example: + - button in zomato while adding food to cart .示例:将食物添加到购物车时 zomato 中的 + - 按钮。 As i click + or - button that corresponding item number should increment/decrement当我单击 + 或 - 按钮时,相应的项目编号应增加/减少

You can use this library.你可以使用这个库。 It is a simple Android library to implement a number counter with increment and decrement buttons.它是一个简单的 Android 库,用于实现带有递增和递减按钮的数字计数器。

https://github.com/ashik94vc/ElegantNumberButton https://github.com/ashik94vc/ElegantNumberButton

try this,尝试这个,

MainActivity.java主活动.java

public class MainActivity extends AppCompatActivity {

TextView textViewCount;
int count=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textViewCount=findViewById(R.id.textViewCount);
    textViewCount.setText(""+count);
}

public void itemSubtractClick(View view) {
    if (count>0)
    textViewCount.setText(""+count--);
    else
        Toast.makeText(this, "not allowed", Toast.LENGTH_SHORT).show();
}

public void itemAddClick(View view) {
    textViewCount.setText(""+count++);

}

} }

activity_main.xml活动_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/shape"
    android:gravity="center"
    android:orientation="horizontal">
    <ImageView
        android:onClick="itemSubtractClick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/icon_subtract" />
    <TextView
        android:id="@+id/textViewCount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#17F44336"
        android:padding="10dp"
        android:text="2"
        android:textSize="18sp" />
    <ImageView
        android:onClick="itemAddClick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/icon_add" />

</LinearLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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