我想创建一个动态按钮,并且必须在动作侦听器中访问它。我的意思是说它的ID并且必须执行必要的操作。任何人都可以帮助我
package com.check.widget;
import android.app.Activity;
import android.widget.*;
import android.os.Bundle;
public class DynamicWidgetActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
int size=10;
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
TextView tv = new TextView(this);
tv.setText("Dynamic layouts ftw!");
ll.addView(tv);
EditText et = new EditText(this);
et.setText("weeeeeeeeeee~!");
ll.addView(et);
Button buttons[] = new Button[size];
for(int i=0;i<size;i++)
{
buttons[i]=new Button(this);
ll.addView(buttons[i]);
}
}
}
答案 0 :(得分:1)
看看这个:Android: How to programmatically add button to view
访问Button Id。除非你设置它,否则它可能没有。