开发者

How to set focus to a button widget programmatically?

开发者 https://www.devze.com 2023-01-07 14:14 出处:网络
Is it po开发者_Python百科ssible to set a focus to a button widget which lies somewhere down in my layout? onCreate of the activity my control/focus should be on that button programmatically.Yeah it\'s

Is it po开发者_Python百科ssible to set a focus to a button widget which lies somewhere down in my layout? onCreate of the activity my control/focus should be on that button programmatically.


Yeah it's possible.

Button myBtn = (Button)findViewById(R.id.myButtonId);
myBtn.requestFocus();

or in XML

<Button ...><requestFocus /></Button>

Important Note: The button widget needs to be focusable and focusableInTouchMode. Most widgets are focusable but not focusableInTouchMode by default. So make sure to either set it in code

myBtn.setFocusableInTouchMode(true);

or in XML

android:focusableInTouchMode="true"


Try this:

btn.requestFocusFromTouch();
0

精彩评论

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