Android Butterknife添加Textview onClick

时间:2017-02-07 23:21:51

标签: android butterknife

我有一项活动,用户输入一些成分信息和一个添加按钮。如何使用butterknife添加textview?我目前没有错误,屏幕上没有任何错误,所以我必须尝试实现这个错误。

1 个答案:

答案 0 :(得分:0)

我明白了。这可能不是正确的方法,但这就是我完成它的方式。

我先添加绑定

@BindView(R.id.btnAddIngredient)
Button btnAdd;
@BindView(R.id.addNextBtn2)
Button btnNext;
@BindView(R.id.ingListLayout)
LinearLayout linearLayout;

然后我添加了这段代码

@OnClick({
            R.id.btnAddIngredient,
            R.id.addNextBtn2
    })
    public void onClick(Button button) {
        switch (button.getId()) {
            case R.id.btnAddIngredient:

                qty = edtxt1.getText().toString();
                measurement = edtxt2.getText().toString();
                Ingredient = edtxt3.getText().toString();

                inputString = qty+" "+measurement+" of "+Ingredient;

                TextView ing = new TextView(this);
                ing.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
                ing.setText(inputString);
                linearLayout.addView(ing);

                edtxt1.setText(null);
                edtxt2.setText(null);
                edtxt3.setText(null);

                break;
            case R.id.addNextBtn2:
                //What does this button do


                if (jsonArray != null) {
                    Intent i = new Intent(AddRecipeScreen2.this, AddRecipeScreen3.class);
                    startActivity(i);
                }else
                    Toast.makeText(mContext, "Please add Ingredients!",
                            Toast.LENGTH_LONG).show();
                break;

        }
    }