如何在创build和显示布局时将焦点置于视图上?
目前,我有一个布局,其中包含一个Button ,一个TextView和一个EditText 。 当显示布局时,焦点将自动放在EditText ,这将触发键盘在Android手机上显示。 这不是我想要的。 有没有什么办法可以将焦点放在TextView或者在布局显示时什么也不做? 
集中焦点:框架将响应用户input来处理移动焦点。 要将焦点强制到特定视图,请调用requestFocus ()
这工作:
 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
你可以尝试只是隐藏键盘。 像这样的东西:
 InputMethodManager inputManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); 
组
  android:focusable="true" 
 在你的<EditText/> 
你应该添加这个:
 android:focusableInTouchMode="true" 
尝试
 comp.requestFocusInWindow(); 
要设置焦点,请使用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(); } } ); } } 
上面的答案都不适用于我。 唯一的(比方说)解决scheme已经改变了第一个TextView的禁用 EditText接受焦点,然后添加
 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
在onCreatecallback,以防止显示键盘。 现在我的第一个EditText看起来像一个TextView,但最终可以获得初始焦点。
 在布局xml文件中设置android:focusable="true" ,然后在代码的视图中设置requestFocus() 
不,这里不行。
 将这些行设置为OnResume ,并确保在初始化控件时OnResume是否设置为true 
 <controlName>.requestFocus(); <controlName>.requestFocusFromTouch(); 
我认为文本视图是不可重点的。 尝试将焦点设置在button上,或将属性设置为true。
你可以添加一个大小为“0 dip”的编辑文本作为你的xml中的第一个控件,所以,这将把重点放在渲染上(确保它的焦点和所有…)
 最后的build议是正确的解决scheme。 只需要重复,首先在layout xml文件中设置android:focusable="true" ,然后在代码中的view上设置requestFocus() 。 
 您可以通过将android:windowSoftInputMode添加到您在AndroidManifest.xml文件中的活动开始。 
 <activity android:name="YourActivity" android:windowSoftInputMode="stateHidden" /> 
 这将使键盘不显示,但EditText仍然是焦点。 为了解决这个问题,你可以在你的根视图上设置android:focusableInTouchmode和android:focusable为true 。 
 <LinearLayout android:orientation="vertical" android:focusable="true" android:focusableInTouchMode="true" ... > <EditText ... /> <TextView ... /> <Button ... /> </LinearLayout> 
 上面的代码将确保RelativeLayout获得焦点,而不是EditText 
 重点是在使用除触摸以外的东西时(例如,d-pad,键盘等)selectUI组件。 任何视图都可以获得焦点,但有些视图默认情况下是不可焦点的。  (您可以使用setFocusable(true)使视图可聚焦,并强制使用requestFocus()进行聚焦。) 
 但是,请注意, 当您处于触摸模式时,焦点将被禁用 。 所以,如果你正在使用你的手指,以编程方式改变焦点不会做任何事情。 这个例外是从input编辑器接收input的视图。 一个EditText就是这样一个例子。 对于这种特殊情况, setFocusableInTouchMode(true)用来让软键盘知道在哪里发送input。 一个EditText默认有这个设置。 软键盘会自动popup。 
如果你不想自动popup软键盘,那么你可以暂时压制它@abeljus指出:
 InputMethodManager inputManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); 
 当用户点击EditText它应该会显示键盘。 
进一步阅读:
- 麻烦聚焦? 在Android的重点入门
- Android开发者博客:触摸模式