2016-05-10 10 views
0

私は、ボタンを動かすのに苦労しました。コードは次のとおりです。androidでボタンを動的に膨張させる方法

LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

View editBtn = layoutInflater.inflate(R.layout.list_item, null); 
Button editTaskBtn = (Button) editBtn.findViewById(R.id.editTaskbutton); 
editTaskBtn.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     Intent intent1 = new Intent(v.getContext(), Activity8.class); 
     startActivityForResult(intent1,0); 
    } 
}); 

ボタンを動作させる方法についてのアイデアはありますか?

+0

あなたがボタンを持っているところそれはそれは、いくつかのビューに添付されています...不明ですか?あなたはビューを膨らませますが、それを他のビューに追加することなく、クリックするとどうしますか?あなたは[mcve]を提供できますか? –

答えて

1

チェックアウトこの

public class InflateExActivity extends Activity { 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     LinearLayout lLayout; 
     final Button b = null; 

     final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     for(int i=0;i<3;i++){ 
      View v = inflater.inflate(R.layout.buttons, null); 
      b = v.findViewById(R.id.your_button_id); 
      // b = (Button) inflater.inflate(R.layout.buttons, null); 
      b.setTag(i); // you'll get 0,1,2 as tags 

      lLayout = (LinearLayout) findViewById(R.id.layout1); 
      lLayout.addView(b); 

      b.setOnClickListener(new OnClickListener() { 
       public void onClick(View v) { 
        int specificButton = (Integer)v.getTag();//Changed here....... 
        Toast.makeText(InflateExActivity.this, 
          "Button Clicked"+Integer.toString(specificButton), 
          Toast.LENGTH_LONG).show(); 
       } 
      }); 
     } 
    } 

} 
関連する問題