如何以编程方式将焦点设置到按钮小部件? [英] How to set focus to a button widget programmatically?

查看:28
本文介绍了如何以编程方式将焦点设置到按钮小部件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将焦点设置到位于布局中某处的按钮小部件?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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆