【问题标题】:How to set focus on a view when a layout is created and displayed?创建和显示布局时如何将焦点设置在视图上?
【发布时间】:2011-01-10 04:12:33
【问题描述】:

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

【问题讨论】:

    标签: java android focus


    【解决方案1】:

    你可以添加

           android:importantForAccessibility="yes"
           android:focusable="true"
           android:focusableInTouchMode="true"
    

    到您的布局以强制对讲/辅助功能先到那里。

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

    【讨论】:

      【解决方案2】:

      Focus 用于在您使用除触摸之外的其他东西(即,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 时,它仍应显示键盘。

      延伸阅读:

      【讨论】:

      • 这个答案错了​​吗?否决票没有留下评论。
      【解决方案3】:

      改变焦点使xml中的textView成为焦点

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

      在java中创建

      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);
      }
      

      【讨论】:

      • android:focusable="true" 和 textView.requestFocus();为我工作。谢谢!
      【解决方案4】:

      您可以首先将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

      【讨论】:

        【解决方案5】:

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

        <controlName>.requestFocus();
        
        <controlName>.requestFocusFromTouch();
        

        【讨论】:

        • 感谢requestFocusFromTouch()。我在容器中添加了android:focusableInTouchMode="true"android:focusable="true"(API 24 中出现了问题)。
        【解决方案6】:

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

        【讨论】:

          【解决方案7】:

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

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

          【讨论】:

            【解决方案8】:

            以上答案都不适合我。唯一(假设)的解决方案是更改 disabled EditText 中的第一个 TextView 接收焦点然后添加

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

            onCreate 回调中防止显示键盘。现在我的第一个 EditText 看起来像一个 TextView 但最终可以得到初始焦点。

            【讨论】:

              【解决方案9】:

              要设置焦点,请使用 Handler 延迟 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();
                        } 
                     }
                   );
                 }
              }
              

              【讨论】:

                【解决方案10】:

                设置

                 android:focusable="true"
                

                在你的&lt;EditText/&gt;

                【讨论】:

                  【解决方案11】:

                  这行得通:

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

                  【讨论】:

                  • 它有什么作用?似乎它正在修改整个活动的某些状态,而不仅仅是一个视图或片段,所以我会对此保持谨慎,除非我了解它的所有含义,否则不要使用它。
                  • 确实,我不会根据原始发帖人的要求这样做。这可能会解决当前的问题,但使用它有副作用,可能会在以后引起问题。正是这样的 hack 进入了代码库,导致后来添加了其他 hack 来否定原始 hack ......恶性循环仍在继续......人们想知道为什么代码变得无法维护。是的,你猜对了......
                  【解决方案12】:

                  你应该添加这个:

                  android:focusableInTouchMode="true"
                  

                  【讨论】:

                    【解决方案13】:

                    您可以添加大小为“0 dip”的编辑文本作为您的 xml 中的第一个控件,因此,这将获得渲染的焦点。(确保它的焦点和所有...)

                    【讨论】:

                      【解决方案14】:

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

                      【讨论】:

                        【解决方案15】:

                        试试

                        comp.requestFocusInWindow();
                        

                        【讨论】:

                          【解决方案16】:

                          设置焦点:框架将处理 移动焦点以响应用户 输入。强制专注于特定的 查看,拨打requestFocus()

                          【讨论】:

                            猜你喜欢
                            • 1970-01-01
                            • 1970-01-01
                            • 1970-01-01
                            • 1970-01-01
                            • 1970-01-01
                            • 1970-01-01
                            • 1970-01-01
                            • 1970-01-01
                            • 1970-01-01
                            相关资源
                            最近更新 更多