开发者

getID for button returns -1

开发者 https://www.devze.com 2023-02-17 06:46 出处:网络
public class Test extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) {
public class Test extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
开发者_如何学Python        Button b = new Button(this);
        String id = Integer.toString(b.getId());
        Toast.makeText(getApplicationContext(), id, 1).show();
    }
}

Why does it return -1?


Because that's the ID views are given when you create them yourself.

Just set the ID with

b.setId(int)

http://developer.android.com/reference/android/widget/Button.html

0

精彩评论

暂无评论...
验证码 换一张
取 消