public class Test extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button b = new Button(this);
String id = Integer.toString(b.getId());
Toast.makeText(getApplicationContext(), id, 1).show();
}
}
为什么它会返回-1?
答案 0 :(得分:3)
因为这是您自己创建ID视图时所给出的。
只需使用
设置ID即可b.setId(int)
http://developer.android.com/reference/android/widget/Button.html