2017-02-07 6 views
-1

私は、ユーザーがいくつかの成分情報と追加ボタンを入力するアクティビティを持っています。バターナイフを使用してテキストビューを追加するにはどうすればよいですか?私は現在、何のエラーも画面上に何も得ていないので、私はこれを間違って実装しようとしているに違いありません。Android Butterknife add 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; 

     } 
    }