开发者

EditText selects by default?

Whenever I have an EditText field in my android application, it is highlighted with the blinking cursor for input as soon as the activity is started (though the keyboard doesn't 开发者_Python百科pop up). How can I disable this?


EditText and ListView are focusable in touch mode so when you launch your application by tapping on its icon (putting you into touch mode) the first one in your application will likely get focus. Had you entered your application by using the D-pad (moving you out of touch mode) the first Button, Spinner, EditText, or ListView would get the focus since they are all focusable.

I wouldn't get too hung up on the a View being in focus when your application starts but if you really can't stand the default way of handling focus, you could try giving focus to a TextView since it doesn't look different when it is in focus. Keep in mind that it isn't normally focusable in touch mode so you will need to enable it prior to requesting focus. Since this is a bit hacky, others (myself included) will caution you against going down this route and likely encourage you to accept the normal behavior.

    View tv = findViewById(R.id.MyTextView);
    tv.setFocusableInTouchMode(true);
    tv.requestFocus();

PS: You may also want to check out this other SO question as it is nearly identical to yours.


You could call setFocusable(false) to avoid this. But if it should become focus later then maybe you have to call setFocusable(true) again later.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜