2017-02-07 50 views
-1

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

+2

显示一些代码,请。到目前为止,你有什么? –

+0

当我到达我的笔记本电脑时,我将不得不在几个小时内添加代码。我试图在我回家之前看到正确的方式来暗示这一点。 – Vahalaru

回答

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; 

     } 
    }