2017-02-07 116 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; 

     } 
    }