如何通过编程将焦点设置到按钮小部件? [英] How to set focus to a button widget programmatically?
本文介绍了如何通过编程将焦点设置到按钮小部件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以将焦点设置在布局中某处的按钮小部件上?我的控件/焦点的活动onCreate
应该以编程方式位于该按钮上.
Is it possible 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.
推荐答案
是的.
Button myBtn = (Button)findViewById(R.id.myButtonId);
myBtn.requestFocus();
或XML
<Button ...><requestFocus /></Button>
重要说明:按钮小部件必须为focusable
和 focusableInTouchMode
.默认情况下,大多数小部件为focusable
,但不是focusableInTouchMode
.因此,请确保在代码中进行设置
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);
或XML
android:focusableInTouchMode="true"
这篇关于如何通过编程将焦点设置到按钮小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文