当多次dynamic扩展布局xml时,我该如何区分或识别Button小部件?

我膨胀一个有button的XML,多次,我可以做到这一点完美,但问题是当我点击button,我想显示哪个button被点击。

public class InflateExActivity extends Activity implements OnClickListener { /** Called when the activity is first created. */ Button b; LinearLayout lLayout; LayoutInflater inflater; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); for (int i = 0; i < 3; i++) { inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); b = (Button) inflater.inflate(R.layout.buttons, null); t = (TextView) inflater.inflate(R.layout.texts, null); b.setTag(i); // you'll get 0,1,2 as lLayout = (LinearLayout) findViewById(R.id.layout1); lLayout.addView(b); b.setOnClickListener(this); } } public void onClick(View v) { } } 

Solutions Collecting From Web of "当多次dynamic扩展布局xml时,我该如何区分或识别Button小部件?"

您正在以编程方式添加的项目,您必须为其分配ID。

 b.setId(1); 

编辑:

 public class DynamicLayoutActivity extends Activity implements OnClickListener{ private static final int MY_BUTTON = 9000; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); LinearLayout ll = (LinearLayout)findViewById(R.id.layout1); // add button Button b = new Button(this); b.setText("Button added dynamically!"); b.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); b.setId(MY_BUTTON); b.setOnClickListener(this); ll.addView(b); } public void onClick(View v) { Toast toast; Log.w("ANDROID DYNAMIC VIEWS:", "View Id: " + v.getId()); switch (v.getId()) { case MY_BUTTON: toast = Toast.makeText(this, "Clicked on my dynamically added button!", Toast.LENGTH_LONG); toast.setGravity(Gravity.TOP, 25, 400); toast.show(); } } 

最新:

 public class InflateExActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { LinearLayout lLayout; Button b = null; super.onCreate(savedInstanceState); setContentView(R.layout.main); for(int i=0;i<3;i++){ final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); b = (Button) inflater.inflate(R.layout.buttons, null); b.setId(i); lLayout = (LinearLayout) findViewById(R.id.layout1); lLayout.addView(b); b.setOnClickListener(new OnClickListener() { public void onClick(View v) { Toast.makeText(InflateExActivity.this, "Button Clicked :"+v.getId(), Toast.LENGTH_LONG).show(); } }); } } 

你可以使用setTag()给每个button。 在for循环中,您可以分配button.setTag()。 你可以使用getTag()来检索button的标签。 膨胀布局后,添加一个标签到您的button

编辑2:你应该膨胀的布局,然后查找您的buttonID。 见下文:

  public class InflateExActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { LinearLayout lLayout; final Button b = null; super.onCreate(savedInstanceState); setContentView(R.layout.main); for(int i=0;i<3;i++){ final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 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(); } }); } } } 

使用视图标签

View.setTag(Object tag);

您可以将一个string或一个复杂对象设置为标记类。