如何才能一键获得焦点? [英] How can a button get the focus?

查看:119
本文介绍了如何才能一键获得焦点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有相当多的职位触及这个话题。我想我应该问这个简单的问题,希望能够澄清这一点。

There are quite a few posts touching this topic. I thought I should ask this simple question hoping to clarify this.

我无法达到设置焦点上的一个按钮。我知道我可能会错过一些东西根本。下面是简单的布局:

I am unable to achieve setting the focus on a button. I know I probably miss something fundamental. Here is the simple layout:

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    android:focusable="true" />

 </LinearLayout>

下面是一个简单的code中的onCreate():

The following is the simple code in onCreate():

        Button button = (Button)findViewById(R.id.button1);
        button.setFocusable(true);
        button.requestFocus();
        button.setText("Debug");  //Just to show the code here has been executed

这根本不​​起作用(即按钮没有得到焦点)。

It simply does not work (i.e. the button does not get the focus).

我的错误或误解的任何修正将大大AP preciated。

Any correction of my error or misunderstanding will be greatly appreciated.

推荐答案

更​​新code:

        Button button = (Button)findViewById(R.id.button1);
        button.setFocusable(true);
        button.setFocusableInTouchMode(true);///add this line
        button.requestFocus();
        button.setText("Debug"); 

这篇关于如何才能一键获得焦点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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