如何在创建和显示布局时将焦点设置在视图上?

时间:2010-01-27 22:09:21

标签: java android focus

目前,我的布局包含ButtonTextViewEditText。显示布局后,焦点将自动放在EditText上,这将触发键盘显示在Android手机上。这不是我想要的。有没有什么方法可以在显示布局时将焦点设置在TextView或什么都没有?

17 个答案:

答案 0 :(得分:102)

  

设置焦点:框架将被处理   移动焦点以响应用户   输入。强制关注特定的   查看,请致电requestFocus()

答案 1 :(得分:17)

这有效:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

答案 2 :(得分:5)

您可以尝试隐藏键盘。像这样:

InputMethodManager inputManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

答案 3 :(得分:5)

要设置焦点,请使用处理程序延迟requestFocus()。

private Handler mHandler= new Handler();

public class HelloAndroid extends Activity {
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);

     LinearLayout mainVw = (LinearLayout) findViewById(R.id.main_layout);

     LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( 
           LinearLayout.LayoutParams.FILL_PARENT,
           LinearLayout.LayoutParams.WRAP_CONTENT);

     EditText edit = new EditText(this);
     edit.setLayoutParams(params);
     mainVw.addView(edit);

     TextView titleTv = new TextView(this);
     titleTv.setText("test");
     titleTv.setLayoutParams(params);
     mainVw.addView(titleTv);

     mHandler.post(
       new Runnable() 
       {
          public void run() 
          {
            titleTv.requestFocus();
          } 
       }
     );
   }
}

答案 4 :(得分:4)

 android:focusable="true"

<EditText/>

答案 5 :(得分:3)

尝试

comp.requestFocusInWindow();

答案 6 :(得分:3)

将这些行设置为OnResume并确保在初始化控件时focusableInTouch设置为true

<controlName>.requestFocus();

<controlName>.requestFocusFromTouch();

答案 7 :(得分:3)

你应该添加:

android:focusableInTouchMode="true"

答案 8 :(得分:2)

上述答案都不适合我。唯一(比方说)解决方案是更改已禁用 EditText 中的第一个 TextView ,然后添加

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
onCreate 回调中的

以防止显示键盘。现在我的第一个EditText看起来像一个TextView,但最终可以获得最初的焦点。

答案 9 :(得分:2)

更改焦点使textView在xml中可聚焦

<TextView
            **android:focusable="true"**
            android:id="@+id/tv_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

并在java in on create

textView.requestFocus();

或只是隐藏键盘

public void hideKeyBoard(Activity act) {
    act.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    InputMethodManager imm = (InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);
}

答案 10 :(得分:1)

你可以在ur xml中添加一个大小为“0 dip”的编辑文本作为第一个控件,这样就可以专注于渲染。(确保它的焦点和所有......)

答案 11 :(得分:1)

在布局xml文件中设置android:focusable="true", 然后在代码中的视图上requestFocus()

不,它在这里不起作用。

答案 12 :(得分:1)

我认为文本视图不可调焦。例如,尝试将焦点设置在按钮上,或将属性focusable设置为true。

答案 13 :(得分:1)

最后一个建议是正确的解决方案。只是重复一遍,首先在布局android:focusable="true"文件中设置xml,然后在代码中的视图上设置requestFocus()

答案 14 :(得分:1)

您可以首先将android:windowSoftInputMode添加到AndroidManifest.xml文件中的活动。

<activity android:name="YourActivity"
          android:windowSoftInputMode="stateHidden" />

这会使键盘无法显示,但EditText仍然有焦点。要解决此问题,您可以在根视图上将android:focusableInTouchmodeandroid:focusable设置为true

<LinearLayout android:orientation="vertical"
              android:focusable="true"
              android:focusableInTouchMode="true"
              ...
              >
    <EditText
         ...
       />
    <TextView
         ...
       />
    <Button
         ...
       />
</LinearLayout>

上面的代码将确保RelativeLayout获得焦点而不是EditText

答案 15 :(得分:0)

重点是在使用除触摸之外的东西(例如,d-pad,键盘等)时选择UI组件。任何视图都可以获得焦点,但有些视图默认不可聚焦。 (您可以使用setFocusable(true)使视图具有焦点,并强制使用requestFocus()进行聚焦。)

但是,请务必注意,当您处于触控模式时, 会禁用焦点 。因此,如果您正在使用手指,则以编程方式更改焦点不会执行任何操作。例外情况是从输入编辑器接收输入的视图。 EditText就是这样一个例子。对于这种特殊情况,setFocusableInTouchMode(true)用于让软键盘知道发送输入的位置。默认情况下,EditText具有此设置。软键盘会自动弹出。

如果您不希望软键盘自动弹出,那么您可以暂时禁止它,因为@abeljus指出:

InputMethodManager inputManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

当用户点击EditText时,它仍应显示键盘。

进一步阅读:

答案 16 :(得分:0)

您可以添加

unset lista
while IFS= read -r line ; do lista+=($line)
done < <(printf "%s\n" '{"field": ["a b", 7, 8]}' | jq -r .field[])

$ python app_parsing_lists.py --lista ${lista[@]}
lista: ['a b', '7', '8']
$

到您的布局以强制对讲/可访问性首先转到那里。

您确实只需要第一行,但是,其他行会向操作系统强调您想要关注的内容。