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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2150656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:09:04  来源:igfitidea点击:

How to set focus on a view when a layout is created and displayed?

javaandroidfocus

提问by user256239

Currently, I have a layout which contains a Button, a TextViewand an EditText. When the layout is displayed, the focus will be automatically put on the EditText, which will trigger the keyboard to show up on Android phone. It is not what I want. Is there any way that I can set the focus on TextViewor on nothing when a layout is displayed?

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

回答by Mark B

Set focus: The framework will handled moving focus in response to user input. To force focus to a specific view, call requestFocus()

设置焦点:框架将处理移动焦点以响应用户输入。要强制聚焦到特定视图,请调用requestFocus()

回答by JARC

Try

尝试

comp.requestFocusInWindow();

回答by ravi

Set

 android:focusable="true"

in your <EditText/>

在你的 <EditText/>

回答by Reza Nezami

The last suggestion is the correct solution. Just to repeat, first set android:focusable="true"in the layout xmlfile, then requestFocus()on the view in your code.

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

回答by lapakrette

i think a text view is not focusable. Try to set the focus on a button for example, or to set the property focusable to true.

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

回答by Charles Mosndup

This works:

这有效:

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

回答by con_9

you can add an edit text of size "0 dip" as the first control in ur xml, so, that will get the focus on render.(make sure its focusable and all...)

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

回答by arif jusoh

You should add this:

你应该添加这个:

android:focusableInTouchMode="true"

回答by Pawan Maheshwari

Set these lines to OnResumeas well and make sure if focusableInTouchis set to true while you initialize your controls

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

<controlName>.requestFocus();

<controlName>.requestFocusFromTouch();

回答by Dan McNerthney

To set focus, delay the requestFocus() using a 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();
          } 
       }
     );
   }
}